-
Notifications
You must be signed in to change notification settings - Fork 0
/
650.cpp
51 lines (44 loc) · 1.15 KB
/
650.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
//copy也算作一次操作
class Solution {
public:
int minSteps(int n) {
if(n==1)return 0;
bool su[1001];
int dp[1001];
for(int i = 2; i <= 1000; ++i)
{
su[i] = true;
dp[i] = i;
}
for(int i = 2; i <= 1000; ++i)
{
for(int j = 2; j <= 1000; ++j)
{
if(i*j>1000)break;
su[i*j] = false;
}
}
if(su[n])return n;
int ret = n;
for(int i = 2; i <= n; ++i)
{
if(su[i])dp[i] = i;
else
{
int jend = (int)sqrt(i);
for(int j = 2; j <= jend; ++j)
{
if(i%j == 0)
{
if(dp[j] + i/j < dp[i])
dp[i] = dp[j] + i/j;
if(dp[i/j] + j < dp[i])
dp[i] = dp[i/j] + j;
}
}
}
cout << i << ":" << dp[i] << endl;
}
return dp[n];
}
};