Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- ll ara[40];
- void make()
- {
- ara[0]=1LL;
- for(int i=1;i<39;i++) ara[i]=ara[i-1]*3LL;
- }
- int ans[40];
- int main()
- {
- make();
- int q;
- cin>>q;
- while(q--)
- {
- ll n;
- cin>>n;
- ll m=n;
- memset(ans,0,sizeof ans);
- for(int i=38;i>=0;i--)
- {
- while(n>=ara[i])
- {
- n-=ara[i];
- ans[i]++;
- }
- }
- int j=-1;
- for(int i=0;i<40;i++) if(ans[i]==2) j=i;
- if(j==-1)
- {
- cout<<m<<"\n";
- continue;
- }
- while(ans[j]) j++;
- ans[j]=1;
- for(int i=0;i<j;i++) ans[i]=0;
- ll tot=0;
- for(int i=0;i<39;i++) if(ans[i]) tot+=ara[i];
- cout<<tot<<"\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement