-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy path0650.cpp
More file actions
37 lines (36 loc) · 693 Bytes
/
0650.cpp
File metadata and controls
37 lines (36 loc) · 693 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
class Solution {
public:
int minSteps(int n) {
vector<int> t{primers(n)};
int res = 0;
while (n > 1) {
for (auto& x : t) {
if (n % x == 0) {
n /= x;
res += x;
break;
}
}
}
return res;
}
vector<int> primers(int n) {
vector<int> res;
if (n < 2) {
return res;
}
vector<bool> dp(n + 1, true);
for (int i = 2; i < dp.size(); ++i) {
if (dp[i]) {
res.emplace_back(i);
}
for (int j = 0; j < res.size() && i * res[j] < dp.size(); ++j) {
dp[i * res[j]] = false;
if (i % res[j] == 0) {
break;
}
}
}
return res;
}
};