Advertisement
NAbdulla

3xBinary Type Multiple

Mar 29th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4.  
  5. using namespace std;
  6.  
  7. long long k;            ///finding the least multiple of this number
  8.  
  9. int divisibility(string n, int l)
  10. {
  11.     long long a;
  12.     a = 0;
  13.     for(int i = 0; i < l; i++){
  14.         a = a*10 + (n[i] - '0');
  15.         a = a % k;
  16.     }
  17.     return !a;          ///return 1 if a == 0, return 0 if a != 0
  18. }
  19.  
  20. int main()
  21. {
  22.     int i, l, n3, n0, j, ind_of_3;
  23.     string num;
  24.  
  25.     while(scanf("%lld", &k) == 1){
  26.         num.push_back('3');
  27.         n3 = 1;         ///number of 3s
  28.         n0 = 0;         ///number of 0s
  29.         l = 1;          ///length of number
  30.         ind_of_3 = 0;   ///index of last 3
  31.         while(1){
  32.             if(divisibility(num, l)){
  33.                 printf("%d %d %d\n", l, n3, n0);
  34.                 break;
  35.             }
  36.  
  37.             if(l == n3){
  38.                 num.push_back('0');
  39.                 l++;
  40.                 n0++;
  41.                 j = l-1;
  42.                 for(i = 1; i < j; i++){
  43.                     num[i] = '0';
  44.                     n0++;
  45.                     n3--;
  46.                 }
  47.                 ind_of_3 = 0;
  48.             }
  49.             else{
  50.                 ind_of_3++;
  51.                 num[ind_of_3] = '3';
  52.                 n3++;
  53.                 n0--;
  54.             }
  55.         }
  56.         num.erase(num.begin(), num.end());
  57.     }
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement