#include <bits/stdc++.h> using namespace std; typedef unsigned long long ll; #define rep(i, n) for (int i = 0; i < (n); i++) #define pb push_back #define st first #define nd second const int MAXN = 2e5 + 7; ll T[MAXN]; ll policz(ll l, ll x, ll r) { ll A = l * x * r; ll B = (l + r) * ((x * (x - 1))/2); ll C = (x * (x - 1) * (x - 2))/6; ll W = A + B + C; return W; } ll znajdz(ll ile, ll potrzeba, ll r) { ll poc = 0; ll kon = ile; // cout << "potrzeba = " << potrzeba << " policz = " << policz(0, ile, r) << '\n'; if (policz(0, ile, r) < potrzeba) { return ile + 1; } ll odp = ile; while (poc < kon) { ll sr = (poc + kon)/2; if (policz(ile - sr, sr, r) >= potrzeba) { odp = sr; kon = sr; } else { poc = sr + 1; } } return odp; } bool spr(int n, ll ile) { ll r = 0; // cout << "ile = " << ile << '\n'; for (int i = n - 1; i >= 0; i--) { ll x = znajdz(ile, T[i], r); // cout << "x = " << x << '\n'; if (x > ile) { return false; } ile -= x; r += x; } return true; } void solve() { int n; cin >> n; rep(i, n) { cin >> T[i]; } ll poc = 0; ll kon = 3e6; ll odp = 0; while (poc < kon) { ll sr = (poc + kon)/2; if (spr(n, sr)) { odp = sr; kon = sr; } else { poc = sr + 1; } } cout << odp << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #include <bits/stdc++.h> using namespace std; typedef unsigned long long ll; #define rep(i, n) for (int i = 0; i < (n); i++) #define pb push_back #define st first #define nd second const int MAXN = 2e5 + 7; ll T[MAXN]; ll policz(ll l, ll x, ll r) { ll A = l * x * r; ll B = (l + r) * ((x * (x - 1))/2); ll C = (x * (x - 1) * (x - 2))/6; ll W = A + B + C; return W; } ll znajdz(ll ile, ll potrzeba, ll r) { ll poc = 0; ll kon = ile; // cout << "potrzeba = " << potrzeba << " policz = " << policz(0, ile, r) << '\n'; if (policz(0, ile, r) < potrzeba) { return ile + 1; } ll odp = ile; while (poc < kon) { ll sr = (poc + kon)/2; if (policz(ile - sr, sr, r) >= potrzeba) { odp = sr; kon = sr; } else { poc = sr + 1; } } return odp; } bool spr(int n, ll ile) { ll r = 0; // cout << "ile = " << ile << '\n'; for (int i = n - 1; i >= 0; i--) { ll x = znajdz(ile, T[i], r); // cout << "x = " << x << '\n'; if (x > ile) { return false; } ile -= x; r += x; } return true; } void solve() { int n; cin >> n; rep(i, n) { cin >> T[i]; } ll poc = 0; ll kon = 3e6; ll odp = 0; while (poc < kon) { ll sr = (poc + kon)/2; if (spr(n, sr)) { odp = sr; kon = sr; } else { poc = sr + 1; } } cout << odp << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; } |