-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
36 lines (30 loc) Β· 1.01 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package DP.P12852;
import java.io.*;
public class Main {
static int N;
static int[] cnt = new int[1000001], memo = new int[1000001];
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/DP/P12852/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
for (int i = 2; i <= N; i++) {
cnt[i] = cnt[i-1] + 1;
memo[i] = i-1;
if (i % 3 == 0 && cnt[i] > cnt[i/3] + 1) {
cnt[i] = cnt[i/3] + 1;
memo[i] = i/3;
}
if (i % 2 == 0 && cnt[i] > cnt[i/2] + 1) {
cnt[i] = cnt[i/2] + 1;
memo[i] = i/2;
}
}
StringBuilder sb = new StringBuilder();
sb.append(cnt[N]).append("\n");
while (N >= 1) {
sb.append(N).append(" ");
N = memo[N];
}
System.out.println(sb.toString());
}
}