-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12852.cpp
63 lines (59 loc) · 1.7 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
#include <cstdio>
#include <climits>
#include <algorithm>
using namespace std;
pair<int, int> dp[1000010]; // first에는 횟수, second에는 그 이전 수
int main(){
int n;
scanf("%d", &n);
dp[0].first=INT_MAX;
dp[1].first=0;
dp[1].second=INT_MAX;
dp[2].first=1;
dp[2].second=1;
for(int i=3;i<=n;i++){
if(i%6==0){
if(dp[i/2].first<dp[i/3].first){
dp[i].first=dp[i/2].first+1;
dp[i].second=i/2;
}
else{
dp[i].first=dp[i/3].first+1;
dp[i].second=i/3;
}
}
else{
if(i%2==0){ //3으로는 안나눠지고 2로만 나눠질때
if(dp[i/2].first<dp[i-1].first){
dp[i].first=dp[i/2].first+1;
dp[i].second=i/2;
}
else{
dp[i].first=dp[i-1].first+1;
dp[i].second=i-1;
}
}
else if(i%3==0){ //2로는 안나눠지고 3으로만 나눠질 때
if(dp[i/3].first<dp[i-1].first){
dp[i].first=dp[i/3].first+1;
dp[i].second=i/3;
}
else{
dp[i].first=dp[i-1].first+1;
dp[i].second=i-1;
}
}
else { //2, 3모두로 다 안나눠질때
dp[i].first=dp[i-1].first+1;
dp[i].second=i-1;
}
}
}
printf("%d\n%d ", dp[n].first, n);
while(n!=1){
if(dp[n].second!=1) printf("%d ",dp[n].second);
else printf("%d",dp[n].second);
n=dp[n].second;
}
printf("\n");
}