Skip to content

Commit b47fee8

Browse files
authored
Merge pull request #1797 from AlgorithmWithGod/Ukj0ng
[20260116] BOJ / G5 / 무한 수열 / 한종욱
2 parents 96f8928 + 2adbd00 commit b47fee8

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
```
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
private static Map<Long, Long> map;
9+
private static int P, Q;
10+
private static long N;
11+
12+
public static void main(String[] args) throws IOException {
13+
init();
14+
15+
long answer = backTracking(N);
16+
bw.write(answer + "\n");
17+
bw.flush();
18+
bw.close();
19+
br.close();
20+
}
21+
22+
private static void init() throws IOException {
23+
StringTokenizer st = new StringTokenizer(br.readLine());
24+
N = Long.parseLong(st.nextToken());
25+
P = Integer.parseInt(st.nextToken());
26+
Q = Integer.parseInt(st.nextToken());
27+
28+
map = new HashMap<>();
29+
}
30+
31+
private static long backTracking(long n) {
32+
if (n == 0) return 1;
33+
if (map.containsKey(n)) return map.get(n);
34+
35+
long temp = backTracking(n/P) + backTracking(n/Q);
36+
map.put(n, temp);
37+
return temp;
38+
}
39+
}
40+
```

0 commit comments

Comments
 (0)