-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj12852.cpp
More file actions
49 lines (43 loc) · 812 Bytes
/
boj12852.cpp
File metadata and controls
49 lines (43 loc) · 812 Bytes
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
#include <iostream>
#include <algorithm>
using namespace std;
inline void quick_IO(){
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
int dp[1000001];
int parent[1000001];
int calc(int x){
if(x==1) return 0;
if(dp[x]!=0) return dp[x];
dp[x] = calc(x-1)+1;
parent[x] = x-1;
if(x%3==0){
int temp =calc(x/3)+1;
if(dp[x]>temp){
dp[x] = temp;
parent[x] = x/3;
}
}
if(x%2==0){
int temp =calc(x/2)+1;
if(dp[x]>temp){
dp[x] = temp;
parent[x] = x/2;
}
}
return dp[x];
}
int main(){
quick_IO();
int x;
cin>>x;
cout<<calc(x)<<"\n";
while(x>0){
cout<<x<<" ";
x = parent[x];
}
cout<<endl;
return 0;
}