#include <vector>
#include <map>
#include <array>
#include <bitset>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <cstdio>
#include <iostream>
#include <ostream>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned char ubyte;
constexpr ld EPS = 1e-9;
constexpr ld PI = 3.141592653589793238462643383279502884L;
constexpr int INF = (int)1e9 + 5;
constexpr ll LINF = (ll)1e18 + 5;
bool eq(ld a, ld b) {
return fabs(a - b) < EPS;
}
template<class T>
T sqr(const T & x) { return x * x; }
template<class T>
ll abs(const T & x) { return x < 0 ? -x : x; }
template<class T>
ll myround(const T & x) { return x < 0 ? x - 0.5 : x + 0.5; }
template<class T>
bool chmin(T & x, const T & y) {
if (y < x) {
x = y;
return true;
}
return false;
}
template<class T>
ostream & operator <<(ostream & os, const vector<T> & v) {
os << "{";
for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
os << *it;
if ((it + 1) != v.end()) os << ", ";
}
os << "}";
return os;
}
template<class P, class Q>
ostream & operator <<(ostream & os, const pair<P, Q> & p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template<class T>
bool chmax(T & x, const T & y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template<class IntegerType = int>
IntegerType nextInt() {
IntegerType x = 0;
bool p = false;
char c;
do {
c = getchar();
} while (c <= 32);
if (c == '-') {
p = true;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return (p ? -x : x);
}
string nextToken() {
static const size_t MAX_LEN = 500500;
static char str[MAX_LEN];
scanf("%s", str);
return str;
}
//template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
//template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
#define all(a) (a).begin(), (a).end()
void no_solution()
{
puts("-1");
exit(0);
}
#define NIEE { puts("NIE"); return; }
#define TAKK { puts("TAK"); return; }
const int N = 200 * 1000 + 5;
int n;
int a[N];
bool check(long long touse)
{
long long used = 0;
for ( int i = 0; i < n; i++ )
{
if ( a[i] == 0 )
continue;
int x = a[i];
int m = 1;
auto check2 = [&](int c) -> bool
{
long long A = used;
long long B = touse - c;
if ( B < 0 )
return true;
if ( A >= 1e9 ) A = 1e6;
if ( B >= 1e9 ) B = 1e6;
long long tot = A * B * c;
if ( tot >= x )
return true;
tot += A * c * (c - 1) / 2;
if ( tot >= x )
return true;
tot += B * c * (c - 1) / 2;
if ( tot >= x )
return true;
tot += c * (c - 1) * (c - 2) / 6;
if ( tot >= x )
return true;
return false;
};
while ( !check2(m) ) m *= 2;
int l = 0, r = m;
while ( l + 1 < r )
{
int m = (l + r) / 2;
if ( check2(m) )
r = m;
else
l = m;
}
if ( r > touse || !check2(r) )
{
return false;
}
touse -= r;
used += r;
}
return true;
}
void testcase()
{
n = nextInt<int>();
for ( int i = 0; i < n; i++ )
{
a[i] = nextInt<int>();
}
long long m = 1;
while ( !check(m) ) m *= 2;
long long l = 0, r = m;
while ( l + 1 < r )
{
long long m = (l + r) / 2;
if ( check(m) )
r = m;
else
l = m;
}
std::cout << r << std::endl;
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int t = nextInt<int>();
while (t--)
testcase();
}
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | #include <vector> #include <map> #include <array> #include <bitset> #include <string> #include <set> #include <map> #include <unordered_map> #include <cstdio> #include <iostream> #include <ostream> #include <algorithm> #include <cmath> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef unsigned char ubyte; constexpr ld EPS = 1e-9; constexpr ld PI = 3.141592653589793238462643383279502884L; constexpr int INF = (int)1e9 + 5; constexpr ll LINF = (ll)1e18 + 5; bool eq(ld a, ld b) { return fabs(a - b) < EPS; } template<class T> T sqr(const T & x) { return x * x; } template<class T> ll abs(const T & x) { return x < 0 ? -x : x; } template<class T> ll myround(const T & x) { return x < 0 ? x - 0.5 : x + 0.5; } template<class T> bool chmin(T & x, const T & y) { if (y < x) { x = y; return true; } return false; } template<class T> ostream & operator <<(ostream & os, const vector<T> & v) { os << "{"; for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) { os << *it; if ((it + 1) != v.end()) os << ", "; } os << "}"; return os; } template<class P, class Q> ostream & operator <<(ostream & os, const pair<P, Q> & p) { return os << "(" << p.first << ", " << p.second << ")"; } template<class T> bool chmax(T & x, const T & y) { if (x < y) { x = y; return true; } return false; } template<class IntegerType = int> IntegerType nextInt() { IntegerType x = 0; bool p = false; char c; do { c = getchar(); } while (c <= 32); if (c == '-') { p = true; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return (p ? -x : x); } string nextToken() { static const size_t MAX_LEN = 500500; static char str[MAX_LEN]; scanf("%s", str); return str; } //template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; //template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; #define all(a) (a).begin(), (a).end() void no_solution() { puts("-1"); exit(0); } #define NIEE { puts("NIE"); return; } #define TAKK { puts("TAK"); return; } const int N = 200 * 1000 + 5; int n; int a[N]; bool check(long long touse) { long long used = 0; for ( int i = 0; i < n; i++ ) { if ( a[i] == 0 ) continue; int x = a[i]; int m = 1; auto check2 = [&](int c) -> bool { long long A = used; long long B = touse - c; if ( B < 0 ) return true; if ( A >= 1e9 ) A = 1e6; if ( B >= 1e9 ) B = 1e6; long long tot = A * B * c; if ( tot >= x ) return true; tot += A * c * (c - 1) / 2; if ( tot >= x ) return true; tot += B * c * (c - 1) / 2; if ( tot >= x ) return true; tot += c * (c - 1) * (c - 2) / 6; if ( tot >= x ) return true; return false; }; while ( !check2(m) ) m *= 2; int l = 0, r = m; while ( l + 1 < r ) { int m = (l + r) / 2; if ( check2(m) ) r = m; else l = m; } if ( r > touse || !check2(r) ) { return false; } touse -= r; used += r; } return true; } void testcase() { n = nextInt<int>(); for ( int i = 0; i < n; i++ ) { a[i] = nextInt<int>(); } long long m = 1; while ( !check(m) ) m *= 2; long long l = 0, r = m; while ( l + 1 < r ) { long long m = (l + r) / 2; if ( check(m) ) r = m; else l = m; } std::cout << r << std::endl; } int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); #endif int t = nextInt<int>(); while (t--) testcase(); } |
English