#include <iostream> #include <vector> #include <algorithm> using namespace std; long long comb(long long n, int k) { if (n < k) return 0; if (k == 3) return n * (n - 1) * (n - 2) / 6; if (k == 2) return n * (n - 1) / 2; return 1; } long long find_s_min(long long sum_a) { long long s = 1; while (comb(s, 3) < sum_a) { s++; } return s; } void generate_partitions(int n, long long S, vector<long long>& current, vector<vector<long long>>& partitions) { if (n == 1) { current.push_back(S); partitions.push_back(current); current.pop_back(); return; } for (long long first = 0; first <= S; first++) { current.push_back(first); generate_partitions(n - 1, S - first, current, partitions); current.pop_back(); } } // Function to solve a single test case long long solve_case(int n, const vector<long long>& a) { long long sum_a = 0; for (long long ai : a) { sum_a += ai; } if (sum_a == 0) { return 0; } long long s_min = find_s_min(sum_a); for (long long S = s_min; ; S++) { vector<vector<long long>> partitions; vector<long long> current; generate_partitions(n, S, current, partitions); for (const vector<long long>& k : partitions) { bool valid = true; for (int i = 0; i < n; i++) { long long ai = a[i]; long long ki = k[i]; long long L = 0, R = 0; for (int j = 0; j < i; j++) { L += k[j]; } for (int j = i + 1; j < n; j++) { R += k[j]; } long long total = comb(ki, 3) + comb(ki, 2) * (S - ki) + L * ki * R; if (total < ai) { valid = false; break; } } if (valid) { return S; } } } return -1; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } cout << solve_case(n, a) << '\n'; } 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 87 88 89 90 | #include <iostream> #include <vector> #include <algorithm> using namespace std; long long comb(long long n, int k) { if (n < k) return 0; if (k == 3) return n * (n - 1) * (n - 2) / 6; if (k == 2) return n * (n - 1) / 2; return 1; } long long find_s_min(long long sum_a) { long long s = 1; while (comb(s, 3) < sum_a) { s++; } return s; } void generate_partitions(int n, long long S, vector<long long>& current, vector<vector<long long>>& partitions) { if (n == 1) { current.push_back(S); partitions.push_back(current); current.pop_back(); return; } for (long long first = 0; first <= S; first++) { current.push_back(first); generate_partitions(n - 1, S - first, current, partitions); current.pop_back(); } } // Function to solve a single test case long long solve_case(int n, const vector<long long>& a) { long long sum_a = 0; for (long long ai : a) { sum_a += ai; } if (sum_a == 0) { return 0; } long long s_min = find_s_min(sum_a); for (long long S = s_min; ; S++) { vector<vector<long long>> partitions; vector<long long> current; generate_partitions(n, S, current, partitions); for (const vector<long long>& k : partitions) { bool valid = true; for (int i = 0; i < n; i++) { long long ai = a[i]; long long ki = k[i]; long long L = 0, R = 0; for (int j = 0; j < i; j++) { L += k[j]; } for (int j = i + 1; j < n; j++) { R += k[j]; } long long total = comb(ki, 3) + comb(ki, 2) * (S - ki) + L * ki * R; if (total < ai) { valid = false; break; } } if (valid) { return S; } } } return -1; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } cout << solve_case(n, a) << '\n'; } return 0; } |