-
Notifications
You must be signed in to change notification settings - Fork 0
/
12852_dp(TD).cpp
54 lines (53 loc) · 950 Bytes
/
12852_dp(TD).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
// 210102 #12852 1·Î ¸¸µé±â2 Silver I
// dp + trace
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 1000001;
int N, d[MAX], p[MAX];
vector<int> ans;
int dp(int cur) {
int& ret = d[cur];
if (ret != -1)
return ret;
if (cur == 1)
return ret = 0;
ret = dp(cur - 1) + 1;
if (!(cur % 3) && ret > dp(cur / 3) + 1) {
ret = dp(cur / 3) + 1;
p[cur] = 3; // update
}
if (!(cur % 2) && ret > dp(cur / 2) + 1) {
ret = dp(cur / 2) + 1;
p[cur] = 2; // update
}
return ret;
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> N;
fill(d, d + MAX, -1);
cout << dp(N) << "\n"; // normal dp
ans.push_back(N);
// tracing
while (1) {
if (N == 1)
break;
if (!p[N]) {
ans.push_back(N - 1);
N--;
}
else if (p[N] == 2) {
ans.push_back(N / 2);
N /= 2;
}
else {
ans.push_back(N / 3);
N /= 3;
}
}
for (int i = 0; i < ans.size(); i++)
cout << ans[i] << " ";
}