-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_one.cpp
More file actions
48 lines (35 loc) · 745 Bytes
/
make_one.cpp
File metadata and controls
48 lines (35 loc) · 745 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
#include <iostream>
using namespace std;
int min_1(int a, int b){
return a > b ? b : a;
}
int min_2(int a, int b, int c){
int min;
if(a > b){
min = b;
} else {
min = a;
}
return c > min ? min : c;
}
int d[1000001];
int main(void){
int n;
int i, j;
cin >> n;
d[1] = 0;
d[2] = d[3] = 1;
for(i = 4; i <= n; i++){
if(i % 3 == 0 && i % 2 == 0){
d[i] = min_2(d[i/3], d[i/2], d[i-1]) + 1;
} else if(i % 3 == 0){
d[i] = min_1(d[i/3], d[i-1]) + 1;
} else if(i % 2 == 0){
d[i] = min_1(d[i/2], d[i-1]) + 1;
} else {
d[i] = d[i-1] + 1;
}
}
cout << d[n] << endl;
return 0;
}