Skip to content

Commit 37eab77

Browse files
authored
Merge pull request #463 from ishanrajsingh/binary-search-day06-1846e2
Add Python solution for Codeforces 1846E2 (Rudolf and Snowflakes)
2 parents f517913 + aa76841 commit 37eab77

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

  • Problems/Binary-Search/Day-06/sol/Ishan_Raj_Singh
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Approach:
3+
For each power r (2 to 64):
4+
- Calculate k ≈ n^(1/r)
5+
- Check if 1 + k + k² + ... + k^r = n
6+
- Verify k-1, k, k+1 for floating point precision
7+
8+
Submission Link: https://codeforces.com/contest/1846/submission/356143831
9+
"""
10+
11+
def solve():
12+
n = int(input())
13+
14+
if n < 3:
15+
print("NO")
16+
return
17+
18+
for r in range(2, 65):
19+
k = int(n ** (1.0 / r))
20+
21+
for candidate_k in [max(2, k - 1), k, k + 1]:
22+
if candidate_k < 2:
23+
continue
24+
25+
total = 1
26+
power = 1
27+
28+
for i in range(r):
29+
power *= candidate_k
30+
total += power
31+
if total > n:
32+
break
33+
34+
if total == n:
35+
print("YES")
36+
return
37+
38+
if k < 2:
39+
break
40+
41+
print("NO")
42+
43+
t = int(input())
44+
for _ in range(t):
45+
solve()

0 commit comments

Comments
 (0)