fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long long cost(long long x){
  5. return 3*x*x + 2*x + 1;
  6. }
  7.  
  8. int main(){
  9. ios::sync_with_stdio(false); cin.tie(0);
  10. int t; cin >> t;
  11. while(t--){
  12. long long n; cin >> n;
  13. long long ans = 0;
  14. for(long long x = 1; n > 0; x *= 3){
  15. long long k = n % 3; // how many deals of size 3*x
  16. ans += k * cost(x);
  17. n /= 3;
  18. }
  19. cout << ans << "\n";
  20. }
  21. }
  22.  
Success #stdin #stdout 0.01s 5288KB
stdin
7
1
3
8
2
10
20
260010000
stdout
6
34
80
12
268
536
100070737512382992