fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. ios::sync_with_stdio(false); cin.tie(0);
  6. int t; cin >> t;
  7. while(t--){
  8. long long n; cin >> n;
  9. long long ans = 0;
  10. while(n){
  11. // find largest x s.t. 3*x <= n
  12. long long x = n/3;
  13. if(x==0) x = 1; // for n=1 or 2, we still need a deal
  14. long long w = 3*x;
  15. long long c = 3*x*x + 2*x + 1;
  16. long long d = n / w;
  17. if(d==0){ // if largest deal bigger than n
  18. d = 1;
  19. w = n; // buy remaining watermelons
  20. c = 3*d*d + 2*d + 1; // cost formula, works for small d
  21. }
  22. ans += d * c;
  23. n -= d * w;
  24. }
  25. cout << ans << "\n";
  26. }
  27. }
  28.  
Success #stdin #stdout 0.01s 5304KB
stdin
7
1
3
8
2
10
20
260010000
stdout
6
6
23
6
40
127
22535066873340001