fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main() {
  6. const int MAX_I = 25;
  7. long long costo[MAX_I + 1];
  8. long long p3[MAX_I + 2]; // we need up to index MAX_I+1
  9.  
  10. // Precompute powers of 3
  11. p3[0] = 1;
  12. for (int i = 1; i <= MAX_I + 1; i++) {
  13. p3[i] = p3[i - 1] * 3;
  14. }
  15.  
  16. // Precompute costo for i from 0 to MAX_I
  17. costo[0] = 3;
  18. for (int i = 1; i <= MAX_I; i++) {
  19. costo[i] = p3[i + 1] + i * p3[i - 1];
  20. }
  21.  
  22. int t;
  23. cin >> t;
  24. while (t--) {
  25. long long n;
  26. cin >> n;
  27. vector<int> digits;
  28. long long temp = n;
  29. while (temp) {
  30. digits.push_back(temp % 3);
  31. temp /= 3;
  32. }
  33. long long ans = 0;
  34. for (int i = 0; i < digits.size(); i++) {
  35. ans += digits[i] * costo[i];
  36. }
  37. cout << ans << endl;
  38. }
  39. return 0;
  40. }
Success #stdin #stdout 0s 5316KB
stdin
3
10
26
6
36
72
2250964728
stdout
36
92
20