We need to solve problem: given x up to 1e12, check if there exist positive integers a, b such that a^3 + b^3 = x.
We need to process up to t=100 test cases.
Brute force: iterate a from 1 to cbrt(x) maybe. For each a, compute b^3 = x - a^3, check if it's a perfect cube and b >= 1. Complexity: O( t * cbrt(x) ), cbrt(1e12) = 1e4. For each test case up to 10k iterations, t=100 => 1e6 total, which is fine. However we must ensure 64-bit integers and double precision for cube root. But we can also do binary search for b.
Better approach: precompute all cubes up to 1e4 (since 1e4^3 = 1e12) and store in a set. Then for each x, for each a^3 in the set check if x - a^3 is also in set and also ensure that the cube root (b) is >=1. Since we want both positive integers, a and b, order doesn't matter. We can iterate a from 1 to floor(cbrt(x)), compute b^3 = x - a^3, if b^3 > 0, compute b = round(cbrt(b^3)) and check b^3 == x - a^3. That's straightforward.
Potential pitfalls: floating point errors. We can avoid floating point by using integer binary search to compute cube root: while lo<=hi, compute mid^3 and compare. Or we can precompute cubes up to 1e4 as an array and then use binary search.
Given constraints, simplest: for each test case, loop a from 1 to cbrt(x). Compute a3 = aaa. Let remaining = x - a3. If remaining <= 0 break. Then compute b = round(pow(remaining, 1/3.0)). But we can use integer cube root: b = floor(cbrt(remaining)) using integer binary search or using cbrt from cmath with careful rounding. Since x up to 1e12, its cube root is about 10000, so binary search per a would be log2(10000) ~14 steps, resulting in about 140k operations per test case worst-case, which is fine. But we can also just compute b = round(pow(remaining, 1.0/3)), then compute b^3 and check. However, pow with double for large integers might give errors because double has 53-bit mantissa, which can represent integers up to 2^53 ~9e15 exactly, so for 1e12 it's fine. But the cube root may not be exact; but if remaining is a perfect cube, then pow with proper rounding should give exact integer, but it's safer to compute b = floor(pow(remaining, 1/3.0) + 1e-10) and check cube. Or better use integer approach.
Simplest: precompute cubes up to 10000 (actually up to 100000? 1e4^3=1e12, but maybe up to 1e6? No, cbrt(1e12)=10000, so cubes for a up to 10000. However note x=1e12 exactly, a could be 10000, b=0? b must be positive, so a max is less than cbrt(x) because b>=1 => a^3 <= x-1, so a <= floor(cbrt(x-1)). But for simplicity iterate up to floor(cbrt(x)). Use integer cube root.
Given the example: x=703657519796 = 5779^3+7993^3. Both are less than 10000? 7993 < 10000 yes. So iteration works.
So algorithm:
For each test case:
Set found = false.
For a from 1 to while aaa <= x (or a <= 10000, but better dynamic):
a3 = aaa
remaining = x - a3
if remaining <= 0: break
b = integer_cuberoot(remaining)
if bbb == remaining and b >= 1:
found = true; break
Print YES/NO.
Time: O( t * max_cuberoot ) ~ 1e6.
We can also speed up by early break when a3 > x/2? Not needed.
Potential improvement: store cubes in an unordered_set for fast lookup, then for each x we could iterate through cube list and check membership. Since cube list size is 10k, membership O(1). So for each test case we can: For each a^3 in cubes, if x - a^3 is in cubes and also the index (b) is >= a? Actually positive integers, not requiring ordering. But we also need to ensure that b^3 is positive and that we don't double count, but it's fine. However, we need to handle the case where a and b could be the same? That's fine. Also note that if we iterate all a^3 from cubes, we might check both a and b, but we can break when found. This reduces need to compute integer cube root each time. But we need to compute cube root for membership? Actually we can store cubes in a set: unordered_set cubes; for i=1..10000 cubes.insert(iii). Then for each x, for each a in 1..10000, compute a3; if a3 >= x break; then if cubes.find(x - a3) != cubes.end(), then YES. That's simple, no need to compute cube root. We need to ensure we only consider positive b, which is guaranteed because x - a3 > 0 and if it's in cubes, it's some i^3 for i>=1. So that works.
But careful: a3 may be up to 1e12, but if we break when a3 >= x, then a will iterate up to cbrt(x). So the loop for a is still O(cbrt(x)). Since we precompute cubes, we can simply use a variable for a up to 10000, but we can break early based on a3.
Since t <= 100, precomputing cubes up to 10000 is trivial.
But note: if x is small, we don't need to go up to 10000, but it's fine.
Implementation details:
-
Precompute an array cube[i] = (long long)iii for i from 0 to 10000 (or 10000 inclusive). Actually i=0 is not positive, but we might include for simplicity but we will only iterate a from 1.
-
Use a set (maybe unordered_set) for O(1) lookup. However, we also need to ensure that we don't consider b=0. Since cube[0]=0, but we only have positive b, so we must ensure that x - a3 != 0? Actually if x - a3 = 0, then b=0 which is not positive. So we need to exclude b=0. So we can either not include 0 in set or check b != 0. Let's include i from 1 to 10000. So set contains cubes for i>=1. Then we can simply check set.contains(x - a3). That automatically ensures b>=1 because only positive cubes present.
-
However, we must be careful with overflow: iii for i up to 10000: 10000^3 = 1e12 fits in long long (which is typically 64-bit). In C++ long long can hold up to 9e18. So fine.
-
For each test case, loop a from 1; compute a3 = cube[a]; if a3 >= x break; if cubes.count(x - a3) then YES.
-
Edge Cases: x = 1: a starts at 1, a3=1, a3 >= x, break? Actually condition if a3 >= x: we need to consider if a3 == x, then remaining = 0, which is not in set because b must be positive. So we break (since further a will be larger) but we haven't found a solution. So output NO. Good.
-
x = 2: a=1, a3=1, a3<2, x - a3 = 1, cubes.count(1) true, so YES.
-
x = 16: a=1, a3=1, remaining=15 not cube; a=2, a3=8, remaining=8, cube? 2^3=8, yes, set contains 8, so YES. Note a=2, b=2.
-
x = 35: a=1, remaining=34 not cube; a=2, remaining=27, cube (3), YES.
-
Large case: works.
Thus the solution is straightforward.
Complexities: O( t * min(cbrt(x), 10000) ) ~ 1e6.
Now implement in C++.
Implementation steps:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
// precompute cubes for i=1 to 10000
const int MAX = 10000; // since 10000^3 = 1e12
vector<ll> cube(MAX+1);
unordered_set<ll> cubes_set;
for (ll i = 1; i <= MAX; ++i) {
cube[i] = i*i*i;
cubes_set.insert(cube[i]);
}
int t;
cin >> t;
while (t--) {
ll x;
cin >> x;
bool found = false;
for (int a = 1; a <= MAX; ++a) {
ll a3 = cube[a];
if (a3 >= x) break;
ll rem = x - a3;
if (cubes_set.find(rem) != cubes_set.end()) {
found = true;
break;
}
}
cout << (found ? "YES" : "NO") << endl;
}
return 0;
}
But we need to be careful: a3 may be equal to x? Then rem=0, not in set, break. So break condition can be a3 > x? Actually if a3 == x, we break as further a will be larger, but we also didn't find a solution with a smaller a? Could there be solution where a3 > x? No, because a3 must be <= x - 1^3? Actually if a3 == x, then b^3=0 not allowed. So no solution. So break if a3 >= x. That's fine.
But consider if a3 < x but rem might be less than cube[1]? Actually rem could be <1, but since a3 < x, rem >=1. So fine.
Potential optimization: we can also break if a3 > x/2? Not needed.
Edge case: x might be exactly 1e12. Then a up to 10000: cube[10000]=1e12, a3 == x, break before checking? Actually a=10000, a3 = 1e12, which is >= x, so we break before checking a=10000. However, could there be solution with a=10000 and b positive? That would require b^3 positive so a3 < x, so no. So it's correct. If there were solution with a=10000 and b>0, then a3 + b3 > a3 = 1e12, so impossible. So okay.
But what about x slightly less than 1e12? For a up to 10000 maybe we need to consider a=10000 only if a3 <= x? Since 10000^3=1e12, if x is 1e12 - 1, then a=10000 yields a3=1e12 > x, so we break before a=10000. But could a=10000 be used as b? That is, maybe solution has a less than 10000 and b=10000? That would be captured when a is the smaller one (say a= some number) and we check rem = x - a3. If b=10000 is part of solution, then rem = 10000^3 = 1e12, but x is less than that, so impossible. So no issue. So we only need a up to floor(cbrt(x)). But if x is near 1e12, floor(cbrt(x)) may be 10000, but if x is exactly 1e12, floor(cbrt(x))=10000, but we break because a3 == x; we still need to check a=10000? Actually if x = 1e12, a=10000 gives a3 = x, rem=0 not allowed, so it's not a solution. So it's fine to not check a=10000 because it's not a solution anyway. However, could there be a solution where a < 10000 and b = 10000? That would sum > 1e12? b=10000^3 = 1e12, a>=1 => sum >1e12, so not equal to 1e12. So fine.
But what about x less than 10000^3 but greater than 9999^3? For x = 999,999,999,999? Actually 9999^3 = 9999^3 = (10000-1)^3 = 1e12 - 310^8 + ... roughly 999,700,029,999? Let's compute precisely: 9999^3 = 999999999999 = (10000-1)^3 = 1e12 - 31e8 + 3*1e4 -1 = 999,700,029,999? Actually 1e12 = 1,000,000,000,000. Subtract 300,000,000 => 999,700,000,000, add 30,000 => 999,700,030,000, subtract 1 => 999,700,029,999. So around 9.997e11. So for x between 999,700,029,999 and 1e12, floor(cbrt(x)) could be 9999 or 10000 depending. Actually if x < 1e12, cbrt(x) < 10000, so floor is at most 9999. So we don't need to check a=10000. So our loop up to 10000 with break when a3 >= x will never reach a=10000 unless x >= 1e12. So it's fine.
Thus solution is correct.
Now implement in the final answer.
However, note the problem's example input includes 703657519796. Check: 5779^3? 5779^3 =? 57795779=333,992,41? Let's compute: 5779^2 = 57795779 = 57795000=28,895,000; 5779700=4,045,300; 5779*79=456,541; sum? Actually easier: we trust it's within range.
Now we need to produce final answer. Probably also we can precompute cubes as a vector and use binary search instead of set, but set is simpler.
But note: unordered_set might have overhead, but with only 10k elements, it's fine. We could also use a sorted vector and binary search, but set is okay.
Now check edge cases: x = 1: loop a=1, a3=1, a3 >= x (1>=1) => break, found false => NO. Good.
x=2: a=1, a3=1<2, rem=1, set contains 1 => YES.
x=4: a=1, a3=1<4, rem=3, not cube; a=2, a3=8>=4? 8>=4, break, so NO.
x=34: a=1: rem=33 not cube; a=2: a3=8, rem=26; a=3: a3=27, rem=7; a=4: a3=64 >34? Actually 4^3=64>34, break. So NO.
x=35: a=1: rem=34 no; a=2: rem=27, set contains 27 (3^3) => YES.
x=16: a=1: rem=15 no; a=2: rem=8 yes => YES.
x=703657519796: a loop from 1 to maybe ~9000? Should find.
Thus answer.
But we must ensure we include cubes for indices up to 10000 inclusive. However, note that for x up to 1e12, the maximum possible a is floor(cbrt(x)) <= 10000. But if x is exactly 1e12, we break before a=10000 as argued. But what if there is a solution where a=10000 and b= something? But then a^3 = 1e12, b^3 must be >0, sum >1e12. So no. So safe.
Thus we can set MAX = 10000.
But could there be a scenario where a > 10000? For x up to 1e12, a^3 <= x implies a <= cbrt(1e12) = 10000. So a cannot exceed 10000. So MAX = 10000 is sufficient. However, what about b? When we check rem, we look up in set that includes cubes up to 10000. But if rem is a perfect cube, its cube root must be <= cbrt(x) as well because rem <= x, so its cube root <= 10000. So it's covered.
Thus all good.
Now the final answer: Provide solution description and code.
But wait: The problem input includes x up to 10^12, but we must handle up to 100 test cases. Our algorithm is O(100 * 10^4) = 10^6 operations, trivial.
Now about using unordered_set: In some judges, unordered_set may be slower due to hashing, but with 10k elements it's fine. Alternatively, we can use a boolean array? But cubes are up to 1e12, can't directly index. So set is fine.
Edge case: memory? Small.
Now let's write final solution in C++.
But I'd like to consider using vector and binary search: sort cubes, for each a3, do binary_search(cubes.begin(), cubes.end(), x - a3). That would be O(log n) per a, total O(t * sqrt[3]{x} * log(sqrt[3]{x})) ~ 1e6 * log(1e4) ~ 1e6*14 = 14e6, also fine. But set is simpler.
However, using unordered_set might have collisions; we can use a simple array for cubes and then use find on set. It's okay.
Now think about overflow: iii: i up to 10000, 10000*10000=100,000,000, times 10000 = 1,000,000,000,000 = 1e12, fits in 64-bit. So fine.
Now implement:
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;
int main() {
const int MAX = 10000;
vector<long long> cubes(MAX+1);
unordered_set<long long> cubeSet;
for (long long i = 1; i <= MAX; ++i) {
cubes[i] = i*i*i;
cubeSet.insert(cubes[i]);
}
int t;
cin >> t;
while (t--) {
long long x;
cin >> x;
bool found = false;
for (int i = 1; i <= MAX; ++i) {
long long a3 = cubes[i];
if (a3 >= x) break;
if (cubeSet.count(x - a3)) {
found = true;
break;
}
}
cout << (found ? "YES" : "NO") << "\n";
}
return 0;
}
Test with sample.
Sample Input:
7
1
2
4
34
35
16
703657519796
Our code:
- x=1: loop i=1, a3=1, a3>=x => break, found false => NO.
- x=2: i=1, a3=1<2, cubeSet.count(1) true => YES.
- x=4: i=1, a3=1<4, count(3) false; i=2, a3=8>=4 break => NO.
- x=34: i=1 a3=1 count(33) false; i=2 a3=8 count(26) false; i=3 a3=27 count(7) false; i=4 a3=64>=34 break => NO.
- x=35: i=1 count(34) false; i=2 count(27) true => YES.
- x=16: i=1 count(15) false; i=2 count(8) true => YES.
- x=703657519796: should find.
Thus outputs match.
Now, check potential integer type issues: long long is at least 64-bit.
Now, compile and run: fine.
But we might consider that the unordered_set could be omitted and we can compute b via integer cube root. Another approach: precompute cubes in a vector and for each a, compute remaining, then binary search in cubes vector. That might be even simpler without using unordered_set. Let's do that: We'll have vector cubes from 1 to MAX. Then for each a, compute rem, and do binary_search(cubes.begin(), cubes.end(), rem). Since cubes is sorted, binary_search works. Complexity O(log(MAX)) per a. That's also fine.
But using unordered_set might be faster in practice because O(1) average. Either is okay.
However, for competitive programming, sometimes unordered_set may be slower due to hashing overhead for many lookups? With only up to 10k lookups per test case (100 test cases => 1M lookups), it's fine.
But to be safe, we can also use a boolean array to mark cubes? Not feasible because cube values up to 1e12, but we could use a bitset? Not needed.
Now, I'll propose final solution.
But there is a nuance: The problem statement says "positive integers", so a,b >= 1. Our check ensures b>=1 because we only have cubes for i>=1. So okay.
Now, think about correctness for x that can be represented as a^3 + b^3 where a and b are both <= 10000. That's always true because max cube root of x is 10000. So if there is representation, both a and b must be <= 10000. So our set includes all possible cubes. So it's correct.
Thus final answer is ready.
Now, output in required format.
Now, for completeness, we can also note alternative solution using two pointers: sort cubes and use two-pointer technique for each x? But we have many test cases; precomputing cubes and then for each x two-pointer would be O(MAX) per test case anyway. Our loop is O(MAX). So fine.
Now, final answer: Provide explanation and code.
Let me also think about time: For each test case, we loop a from 1 to at most 10000. That's 10k iterations, times 100 = 1M, fine.
Now, is there any potential for optimization? Not needed.
Now, test with extreme: x = 10^12 - 1 = 999,999,999,999. That is less than 1e12. Then a max is floor(cbrt(999,999,999,999)) = 9999 because 10000^3 > x. So loop goes to about 9999. So it's fine.
Now, confirm that we break when a3 >= x. For a=9999, a3 = 9999^3 ≈ 9.997e11, which is < x? Actually x = 9.999...e11, so a3 < x, we continue; a=10000? But we would break because a3 > x? Actually for a=10000, a3=1e12 > x, so break before checking a=10000 because we check condition if (a3 >= x) break; at start of loop? In our code we compute a3 from cubes[i] and then check if a3 >= x, break. So for i=10000, a3 = 1e12, which is >= x (since x < 1e12), so break, so we never check i=10000. That's correct.
Now, what about x = 1? i=1, a3=1, a3>=x, break; we never check i>1. So fine.
Thus solution is correct.
Now, produce final answer.
But wait: The problem constraints say t up to 100, x up to 10^12. So our method passes.
One more thing: The example output for x=16 is YES because 2^3+2^3. Our code says YES.
Now, check x=8: 2^3+? Actually 2^3+? 8 = 2^3+0? Not allowed. 1^3+? 1+? =7, not cube. So NO. Our code: a=1, a3=1, rem=7 not cube; a=2, a3=8>=8? a3==x, break, so NO. Good.
Now, x=27: 3^3+0? Not allowed; 1^3+? 26 not cube; 2^3+? 19 not cube; a=3 break; NO. Good.
x=9: 2^3+1^3=9? 8+1=9 => YES! Actually 1^3+2^3=9. So 9 should be YES. Let's test: a=1, a3=1, rem=8, cube? 2^3=8 => YES. So output YES. Good.
Thus code works.
Now, consider integer overflow when computing iii. i up to 10000, ii = 100,000,000, within 32-bit signed? 100,000,000 < 2^31-1 (2.147e9). So ii fits in 32-bit signed. Then multiply by i again: 100,000,00010000 = 1,000,000,000,000, which fits in 64-bit. So we need to use long long for the product. In C++, if i is int, ii*i will be computed as int, and could overflow because int on some compilers is 32-bit, max ~2e9. So we must cast to long long: either use 1LL*i*i*i or declare i as long long, or use long long i. In the precomputation loop, we can define i as long long to avoid overflow. In my code, I used long long i. So safe.
Alternatively, we can compute cubes[i] = (long long)iii; with i as int, but the product is computed using int multiplication before assignment to long long, which would overflow. So better to use (long long)i*i*i. In my code I had for (long long i = 1; i <= MAX; ++i), so safe.
Now, if I want to use unordered_set with int keys, it's fine.
Now, final code:
#include <bits/stdc++.h>
using namespace std;
int main() {
const int MAX = 10000;
vector<long long> cubes(MAX + 1);
unordered_set<long long> cube_set;
for (long long i = 1; i <= MAX; ++i) {
cubes[i] = i * i * i;
cube_set.insert(cubes[i]);
}
int t;
cin >> t;
while (t--) {
long long x;
cin >> x;
bool ok = false;
for (int i = 1; i <= MAX; ++i) {
long long a3 = cubes[i];
if (a3 >= x) break;
if (cube_set.find(x - a3) != cube_set.end()) {
ok = true;
break;
}
}
cout << (ok ? "YES" : "NO") << "\n";
}
return 0;
}
That's the solution.
But wait: Could there be a scenario where a^3 is large, and we break early because a3 >= x, but there exists a representation where a > b? Since we are iterating a from 1 upwards, we only consider a as the smaller cube? Actually if a is larger than b, we would still find it because we check both orders: when a is the larger, b would be the smaller. In our loop, we only check pairs where the first component (a) is the one we iterate, and second component b is any positive integer. So if a is the larger, we will encounter that when our loop reaches that larger a, and then b would be the smaller, which is present in set. But note, if a is larger than b, then a3 > b3, so when we iterate a from 1 upward, we will first encounter the smaller (b) as a and find the larger as b? Actually if representation exists with a=5, b=3, then when i=3, a3=27, rem = x - 27 = 125? Actually 5^3+3^3 = 125+27=152. When i=3, a3=27, rem=125, which is a cube (5). So we find it. When i=5, a3=125, rem=27, also cube, but by then we would have already found at i=3, but if we haven't, at i=5 we would find it too unless we broke earlier because a3 >= x? For x=152, a3 at i=5 is 125 < x, so we would check it. So both orders are covered.
Now, the break condition: we break when a3 >= x. This means when a becomes so large that a3 is not less than x, we stop. That is correct because if a3 >= x, then rem <= 0, and we can't have positive b. So no need to check further.
Thus algorithm correct.
Now, I will produce final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
// Precompute cubes of numbers from 1 to 10000 (since 10000^3 = 10^12)
const int MAX = 10000;
vector cubes(MAX + 1);
unordered_set cube_set;
for (long long i = 1; i <= MAX; ++i) {
cubes[i] = i * i * i;
cube_set.insert(cubes[i]);
}
int t;
cin >> t;
while (t--) {
long long x;
cin >> x;
bool ok = false;
for (int i = 1; i <= MAX; ++i) {
long long a3 = cubes[i];
if (a3 >= x) break; // further a^3 would only be larger, no possible b > 0
if (cube_set.find(x - a3) != cube_set.end()) {
ok = true;
break;
}
}
cout << (ok ? "YES" : "NO") << "\n";
}
return 0;
}