Skip to content

Commit bdf222b

Browse files
authored
Create day06sol02.cpp
1 parent de2567b commit bdf222b

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//submission- https://codeforces.com/contest/1846/submission/356011684
2+
3+
/*
4+
Approach:
5+
- Precompute all valid snowflake sizes of the form:
6+
1 + k + k^2 + k^3 + ... (k > 1, at least 3 terms)
7+
- Use __int128 to safely handle values up to 10^18
8+
- Store all generated values in a set for fast lookup
9+
- For large x, additionally check the special case:
10+
x = 1 + k + k^2 using quadratic formula
11+
- Answer YES if x matches any valid construction
12+
13+
Complexity:
14+
Precomputation: ~O(K * log n)
15+
Query: O(log n)
16+
Space: O(N)
17+
*/
18+
19+
#include <bits/stdc++.h>
20+
using namespace std;
21+
22+
set <__int128> s;
23+
24+
const __int128 INF = 1000000000000000000ll;
25+
26+
int main() {
27+
for(long long i = 2; i <= 1000000; ++i) {
28+
__int128 j = i + 1;
29+
__int128 l = i * i;
30+
while(j + l <= INF) {
31+
j += l;
32+
l *= i;
33+
s.insert(j);
34+
}
35+
}
36+
int tt;
37+
cin >> tt;
38+
while(tt--) {
39+
long long x;
40+
cin >> x;
41+
if(s.count(x)) {
42+
cout << "YES" << endl;
43+
}
44+
else if(x >= 1000000000000ll){
45+
long long delta = 1 - 4 * (1 - x);
46+
long long x1 = 0.5 * (-1 + sqrt(delta));
47+
if(x1 * x1 + x1 + 1 == x) {
48+
cout << "YES" << endl;
49+
}
50+
else {
51+
cout << "NO" << endl;
52+
}
53+
}
54+
else {
55+
cout << "NO" << endl;
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)