-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12852.cpp
More file actions
39 lines (38 loc) · 857 Bytes
/
12852.cpp
File metadata and controls
39 lines (38 loc) · 857 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
#include <bits/stdc++.h>
using namespace std;
int n, MAX = 123456789;
int memo[1000001];
int solve(int x) {
if(x == 1) return 0;
int &ret = memo[x];
if(ret != 0) return ret;
ret = MAX;
if(x % 3 == 0) ret = min(ret, solve(x / 3) + 1);
if(x % 2 == 0) ret = min(ret, solve(x / 2) + 1);
ret = min(ret, solve(x - 1) + 1);
return ret;
}
void tracking(int x) {
if(x == 1) return;
int ret = MAX, cur;
if(x % 3 == 0 && ret > solve(x / 3)) {
ret = solve(x / 3);
cur = x / 3;
}
if(x % 2 == 0 && ret > solve(x / 2)) {
ret = solve(x / 2);
cur = x / 2;
}
if(ret > solve(x - 1)) {
ret = solve(x - 1);
cur = x - 1;
}
cout << cur << " ";
tracking(cur);
}
int main () {
cin >> n;
cout << solve(n) << "\n";
cout << n << " ";
tracking(n);
}