File tree Expand file tree Collapse file tree
Problems/Binary-Search/Day-06/sol/Ishan_Raj_Singh Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ()
You can’t perform that action at this time.
0 commit comments