Skip to content

Commit ff761d5

Browse files
authored
Merge pull request #425 from dwivediprashant/code/day-6-q-2
code(java) : Solved Rudolf and Snowflakes (Hard Version)
2 parents 355af65 + febd945 commit ff761d5

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// -------------------- Problem --------------------
2+
/*
3+
Rudolf looks at snowflakes and imagines building one using a graph.
4+
5+
Start with a single vertex.
6+
Connect it to k new vertices (k must be greater than 1).
7+
After that, every vertex that has only one connection grows k more vertices.
8+
This growing step must happen at least once.
9+
10+
Given a number n, we need to check whether it’s possible to end up
11+
with exactly n vertices using some value of k.
12+
*/
13+
14+
// -------------------- What’s actually going on --------------------
15+
/*
16+
After each growth step, the total number of vertices becomes:
17+
18+
1 + k + k^2 + k^3 + ... + k^d
19+
20+
So the problem is just checking whether n can be written like this
21+
for some k >= 2 and d >= 2.
22+
*/
23+
24+
// -------------------- Main idea --------------------
25+
/*
26+
Trying every k directly is too slow, especially in Java.
27+
28+
But the number of growth layers (d) is small.
29+
Even with k = 2, values blow past 10^18 after about 60 layers.
30+
31+
So we loop over all possible depths d (from 2 to ~60),
32+
and for each depth we binary search the value of k.
33+
*/
34+
35+
// -------------------- How the checking works --------------------
36+
/*
37+
For a fixed d:
38+
- Pick a k using binary search
39+
- Compute 1 + k + k^2 + ... + k^d carefully
40+
- Stop early if the value becomes too big or overflows
41+
42+
If we ever hit exactly n, we print YES.
43+
Otherwise, after all tries, we print NO.
44+
*/
45+
46+
// -------------------- Complexity --------------------
47+
/*
48+
For each test case:
49+
- We try at most 60 values of d
50+
- For each d, we do a binary search on k
51+
52+
Time complexity: O(60 * log n)
53+
Space complexity: O(1)
54+
*/
55+
56+
// -------------------- Submission --------------------
57+
/*
58+
https://codeforces.com/contest/1846/submission/356040210
59+
*/
60+
61+
// -------------------- Code -------------------------
62+
63+
import java.io.BufferedReader;
64+
import java.io.InputStreamReader;
65+
66+
public class Solution2 {
67+
68+
static final long LIMIT = (long) 1e18;
69+
70+
public static void main(String[] args) throws Exception {
71+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
72+
int t = Integer.parseInt(br.readLine().trim());
73+
74+
StringBuilder out = new StringBuilder();
75+
76+
while (t-- > 0) {
77+
long n = Long.parseLong(br.readLine().trim());
78+
out.append(canMakeSnowflake(n) ? "YES\n" : "NO\n");
79+
}
80+
81+
System.out.print(out.toString());
82+
}
83+
84+
static boolean canMakeSnowflake(long n) {
85+
for (int d = 2; d <= 60; d++) {
86+
long left = 2;
87+
long right = (long) Math.pow(n, 1.0 / d) + 1;
88+
89+
while (left <= right) {
90+
long mid = (left + right) >>> 1;
91+
long value = calcSum(mid, d);
92+
93+
if (value == n) return true;
94+
if (value < 0 || value > n) right = mid - 1;
95+
else left = mid + 1;
96+
}
97+
}
98+
return false;
99+
}
100+
101+
static long calcSum(long k, int d) {
102+
long sum = 1;
103+
long cur = 1;
104+
105+
for (int i = 1; i <= d; i++) {
106+
if (cur > LIMIT / k) return -1;
107+
cur *= k;
108+
if (sum > LIMIT - cur) return -1;
109+
sum += cur;
110+
}
111+
112+
return sum;
113+
}
114+
}

0 commit comments

Comments
 (0)