#include "bits/stdc++.h"
using namespace std;
bool isPrime(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return n > 1;
}
int INF = 1e9;
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
clock_t startTime = clock();
// int N = 1e4;
// vector<int> g(N + 1);
// vector<int> dp(N + 1, (int)1e9);
// primes = {5, 11, 19};
// dp[0] = 0;
// for (int i = 1; i <= N; i++) {
// for (int p: primes) {
// dp[i] = min(dp[i], dp[i - i % p] + 1);
// }
// }
// for (int i = 1; i <= 100; i++) {
// cout << i << " -> " << dp[i] << endl;
// }
int n, q, N = 1e7;
cin >> n >> q;
vector<int> primes(n), g(N + 1), dp(N + 1, INF);
iota(g.begin(), g.end(), 0);
for (int &p: primes) {
cin >> p;
}
reverse(primes.begin(), primes.end());
for (int i = 0; i < n; i++) {
int p = primes[i];
for (int j = 0; j <= N; j += p) {
for (int k = min(N, j + p - 1); k > j and j < g[k]; k--) {
g[k] = j;
}
}
}
dp[0] = 0;
for (int i = 1; i <= N; i++) {
dp[i] = dp[g[i]] + 1;
}
while (q--) {
int x;
cin >> x;
if (dp[x] >= INF) {
cout << "oo\n";
} else {
cout << dp[x] << '\n';
}
}
#ifdef sunnitov
cerr << "\nTime: " << (int)((double)(clock() - startTime) / CLOCKS_PER_SEC * 1000);
#endif
}
Compilation message
brunhilda.cpp: In function 'int main()':
brunhilda.cpp:18:13: warning: unused variable 'startTime' [-Wunused-variable]
18 | clock_t startTime = clock();
| ^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
78608 KB |
Output is correct |
2 |
Correct |
124 ms |
78596 KB |
Output is correct |
3 |
Correct |
86 ms |
78588 KB |
Output is correct |
4 |
Correct |
76 ms |
78640 KB |
Output is correct |
5 |
Correct |
108 ms |
78660 KB |
Output is correct |
6 |
Correct |
66 ms |
78548 KB |
Output is correct |
7 |
Correct |
72 ms |
78540 KB |
Output is correct |
8 |
Correct |
80 ms |
78596 KB |
Output is correct |
9 |
Correct |
93 ms |
78480 KB |
Output is correct |
10 |
Correct |
133 ms |
78568 KB |
Output is correct |
11 |
Correct |
125 ms |
78608 KB |
Output is correct |
12 |
Correct |
85 ms |
78620 KB |
Output is correct |
13 |
Correct |
217 ms |
78608 KB |
Output is correct |
14 |
Correct |
228 ms |
78544 KB |
Output is correct |
15 |
Correct |
118 ms |
78588 KB |
Output is correct |
16 |
Correct |
130 ms |
78540 KB |
Output is correct |
17 |
Correct |
108 ms |
78652 KB |
Output is correct |
18 |
Correct |
73 ms |
78620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1074 ms |
78704 KB |
Time limit exceeded |
2 |
Execution timed out |
1068 ms |
79032 KB |
Time limit exceeded |
3 |
Execution timed out |
1077 ms |
78960 KB |
Time limit exceeded |
4 |
Correct |
478 ms |
78620 KB |
Output is correct |
5 |
Execution timed out |
1070 ms |
79052 KB |
Time limit exceeded |
6 |
Correct |
168 ms |
78488 KB |
Output is correct |
7 |
Execution timed out |
1088 ms |
78728 KB |
Time limit exceeded |
8 |
Correct |
165 ms |
78608 KB |
Output is correct |
9 |
Execution timed out |
1089 ms |
79120 KB |
Time limit exceeded |
10 |
Execution timed out |
1072 ms |
78924 KB |
Time limit exceeded |
11 |
Execution timed out |
1070 ms |
78840 KB |
Time limit exceeded |
12 |
Correct |
269 ms |
78540 KB |
Output is correct |
13 |
Execution timed out |
1055 ms |
78632 KB |
Time limit exceeded |
14 |
Correct |
476 ms |
78568 KB |
Output is correct |
15 |
Execution timed out |
1083 ms |
78828 KB |
Time limit exceeded |
16 |
Execution timed out |
1054 ms |
79052 KB |
Time limit exceeded |
17 |
Correct |
228 ms |
78624 KB |
Output is correct |
18 |
Execution timed out |
1090 ms |
79104 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1053 ms |
78796 KB |
Time limit exceeded |
2 |
Execution timed out |
1075 ms |
78924 KB |
Time limit exceeded |
3 |
Execution timed out |
1077 ms |
78856 KB |
Time limit exceeded |
4 |
Correct |
460 ms |
79032 KB |
Output is correct |
5 |
Execution timed out |
1046 ms |
79180 KB |
Time limit exceeded |
6 |
Execution timed out |
1085 ms |
78688 KB |
Time limit exceeded |
7 |
Execution timed out |
1058 ms |
79044 KB |
Time limit exceeded |
8 |
Execution timed out |
1081 ms |
78796 KB |
Time limit exceeded |
9 |
Execution timed out |
1082 ms |
78904 KB |
Time limit exceeded |
10 |
Correct |
805 ms |
78728 KB |
Output is correct |
11 |
Correct |
548 ms |
78832 KB |
Output is correct |
12 |
Correct |
733 ms |
78712 KB |
Output is correct |
13 |
Execution timed out |
1064 ms |
78784 KB |
Time limit exceeded |
14 |
Correct |
173 ms |
79284 KB |
Output is correct |
15 |
Correct |
787 ms |
78792 KB |
Output is correct |
16 |
Correct |
808 ms |
78748 KB |
Output is correct |
17 |
Execution timed out |
1076 ms |
78820 KB |
Time limit exceeded |
18 |
Execution timed out |
1079 ms |
78880 KB |
Time limit exceeded |
19 |
Execution timed out |
1072 ms |
78616 KB |
Time limit exceeded |
20 |
Execution timed out |
1081 ms |
78800 KB |
Time limit exceeded |
21 |
Correct |
171 ms |
79308 KB |
Output is correct |
22 |
Execution timed out |
1091 ms |
78972 KB |
Time limit exceeded |
23 |
Execution timed out |
1081 ms |
78652 KB |
Time limit exceeded |
24 |
Correct |
197 ms |
78788 KB |
Output is correct |
25 |
Correct |
346 ms |
78860 KB |
Output is correct |
26 |
Correct |
511 ms |
78856 KB |
Output is correct |
27 |
Execution timed out |
1087 ms |
78932 KB |
Time limit exceeded |
28 |
Correct |
123 ms |
78796 KB |
Output is correct |
29 |
Execution timed out |
1079 ms |
78932 KB |
Time limit exceeded |
30 |
Execution timed out |
1069 ms |
78748 KB |
Time limit exceeded |
31 |
Execution timed out |
1067 ms |
78868 KB |
Time limit exceeded |
32 |
Correct |
345 ms |
78856 KB |
Output is correct |
33 |
Correct |
300 ms |
78724 KB |
Output is correct |
34 |
Execution timed out |
1055 ms |
78952 KB |
Time limit exceeded |
35 |
Correct |
199 ms |
78856 KB |
Output is correct |
36 |
Execution timed out |
1046 ms |
78820 KB |
Time limit exceeded |
37 |
Execution timed out |
1084 ms |
79052 KB |
Time limit exceeded |
38 |
Execution timed out |
1080 ms |
78540 KB |
Time limit exceeded |
39 |
Correct |
349 ms |
78864 KB |
Output is correct |
40 |
Execution timed out |
1084 ms |
78584 KB |
Time limit exceeded |
41 |
Execution timed out |
1083 ms |
79052 KB |
Time limit exceeded |
42 |
Correct |
295 ms |
78980 KB |
Output is correct |