-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdp_12852.cpp
51 lines (47 loc) · 867 Bytes
/
dp_12852.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<cstdio>
#include<algorithm>
using namespace std;
int ary[1000001];
int output[1000001];
int N;
int dp_cases(int num){
int min = numeric_limits<int>::max();
int temp, temp_output;
if (num <= 1)
return 0;
if (num % 3 == 0){
temp = dp_cases(num / 3);
if (temp < min){
temp_output = num / 3;
min = temp;
}
}
if (num % 2 == 0){
temp = dp_cases(num / 2);
if (temp < min){
temp_output = num / 2;
min = temp;
}
}
temp = dp_cases(num - 1);
if (temp < min){
temp_output = num - 1;
min = temp;
}
output[num] = temp_output;
return ary[num] = min + 1;
}
void printPath(){
int temp = N;
printf("%d ", N);
while (output[temp]){
printf("%d ", output[temp]);
temp = output[temp];
}
}
int main(){
scanf("%d", &N);
printf("%d\n", dp_cases(N));
printPath();
return 0;
}