SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5.  
  6. ll ara[40];
  7.  
  8. void make()
  9. {
  10.     ara[0]=1LL;
  11.     for(int i=1;i<39;i++) ara[i]=ara[i-1]*3LL;
  12. }
  13.  
  14. int ans[40];
  15.  
  16. int main()
  17. {
  18.     make();
  19.     int q;
  20.     cin>>q;
  21.     while(q--)
  22.     {
  23.         ll n;
  24.         cin>>n;
  25.         ll m=n;
  26.         memset(ans,0,sizeof ans);
  27.  
  28.         for(int i=38;i>=0;i--)
  29.         {
  30.             while(n>=ara[i])
  31.             {
  32.                 n-=ara[i];
  33.                 ans[i]++;
  34.             }
  35.         }
  36.  
  37.         int j=-1;
  38.         for(int i=0;i<40;i++) if(ans[i]==2) j=i;
  39.  
  40.         if(j==-1)
  41.         {
  42.             cout<<m<<"\n";
  43.             continue;
  44.         }
  45.  
  46.         while(ans[j]) j++;
  47.         ans[j]=1;
  48.         for(int i=0;i<j;i++) ans[i]=0;
  49.  
  50.         ll tot=0;
  51.  
  52.         for(int i=0;i<39;i++) if(ans[i]) tot+=ara[i];
  53.  
  54.         cout<<tot<<"\n";
  55.     }
  56.  
  57.     return 0;
  58.  
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top