Advertisement
Guest User

C2 Div3

a guest
Oct 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. long long how[50];
  8.  
  9. int main()
  10. {
  11.     #ifndef ONLINE_JUDGE
  12.     freopen("input.txt","r",stdin);
  13.     #endif // ONLINE_JUDGE
  14.  
  15.     int Q;
  16.     cin >> Q;
  17.  
  18.     while(Q--)
  19.     {
  20.         long long x;
  21.         cin >> x;
  22.  
  23.         memset(how, 0, sizeof(how));
  24.  
  25.         long long   pw = 1;
  26.         int         pz = 0;
  27.  
  28.         while(pw <= x)
  29.         {
  30.             pw *= 3;
  31.             pz++;
  32.         }
  33.  
  34.         pw /= 3;
  35.         pz--;
  36.  
  37.         int maxpz = pz;
  38.  
  39.         while(x)
  40.         {
  41.             long long cat = x / pw;
  42.             x %= pw;
  43.             how[pz] = cat;
  44.             pw /= 3;
  45.             pz--;
  46.         }
  47.  
  48.         pw = 1;
  49.  
  50.         for(int i = 0; i <= maxpz; i++, pw *= 3)
  51.         {
  52.             if(how[i] > 1)
  53.             {
  54.                 long long cat = how[i] / 3;
  55.                 how[i + 1] += max(cat, 1LL);
  56.                 maxpz = max(maxpz, i + 1);
  57.                 how[i] = 0;
  58.                 x = 0;
  59.             }
  60.  
  61.             x += how[i] * pw;
  62.         }
  63.  
  64.         cout << x << '\n';
  65.     }
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement