fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4.  
  5. ll coins(int x){
  6. if(x==0) return 3;
  7. return (ll)pow(3,x+1) + (ll)x * (ll)pow(3,x-1);
  8. }
  9.  
  10. int main() {
  11. // your code goes here
  12. int t=1;
  13. cin>>t;
  14. while(t--){
  15. ll n;
  16. cin>>n;
  17. ll ans = 0;
  18.  
  19. for(int i=20;i>=0;i--){
  20. ll temp = pow(3,i);
  21. while(n >= temp){
  22. n -= temp;
  23. ans += coins(i);
  24. }
  25. }
  26.  
  27. ans += n*3;
  28. cout<<ans<<"\n";
  29. }
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5276KB
stdin
7
1
3
8
2
10
20
260010000
stdout
3
10
26
6
36
72
2250964728