#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, 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;
}
sort(primes.rbegin(), primes.rend());
for (int i = 0; i < n; i++) {
int p = primes[i], nextp;
if (i + 1 < n) {
nextp = primes[i + 1];
}
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;
if (i + 1 < n and j > k - k % nextp) {
break;
}
}
}
}
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();
| ^~~~~~~~~
brunhilda.cpp:35:45: warning: 'nextp' may be used uninitialized in this function [-Wmaybe-uninitialized]
35 | if (i + 1 < n and j > k - k % nextp) {
| ~~^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
137 ms |
78528 KB |
Output is correct |
2 |
Correct |
216 ms |
78596 KB |
Output is correct |
3 |
Correct |
144 ms |
78592 KB |
Output is correct |
4 |
Correct |
175 ms |
78636 KB |
Output is correct |
5 |
Correct |
184 ms |
78616 KB |
Output is correct |
6 |
Correct |
134 ms |
78600 KB |
Output is correct |
7 |
Correct |
144 ms |
78492 KB |
Output is correct |
8 |
Correct |
149 ms |
78592 KB |
Output is correct |
9 |
Correct |
170 ms |
78472 KB |
Output is correct |
10 |
Correct |
175 ms |
78596 KB |
Output is correct |
11 |
Correct |
172 ms |
78556 KB |
Output is correct |
12 |
Correct |
225 ms |
78588 KB |
Output is correct |
13 |
Correct |
343 ms |
78608 KB |
Output is correct |
14 |
Correct |
328 ms |
78588 KB |
Output is correct |
15 |
Correct |
193 ms |
78592 KB |
Output is correct |
16 |
Correct |
220 ms |
78592 KB |
Output is correct |
17 |
Correct |
181 ms |
78636 KB |
Output is correct |
18 |
Correct |
170 ms |
78640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
78644 KB |
Output is correct |
2 |
Correct |
136 ms |
79212 KB |
Output is correct |
3 |
Correct |
268 ms |
79172 KB |
Output is correct |
4 |
Correct |
150 ms |
78624 KB |
Output is correct |
5 |
Correct |
192 ms |
79052 KB |
Output is correct |
6 |
Correct |
205 ms |
78604 KB |
Output is correct |
7 |
Correct |
115 ms |
78728 KB |
Output is correct |
8 |
Correct |
219 ms |
78616 KB |
Output is correct |
9 |
Correct |
207 ms |
79108 KB |
Output is correct |
10 |
Correct |
381 ms |
79124 KB |
Output is correct |
11 |
Correct |
249 ms |
78984 KB |
Output is correct |
12 |
Correct |
194 ms |
78616 KB |
Output is correct |
13 |
Correct |
115 ms |
78548 KB |
Output is correct |
14 |
Correct |
146 ms |
78516 KB |
Output is correct |
15 |
Correct |
215 ms |
79016 KB |
Output is correct |
16 |
Correct |
126 ms |
79180 KB |
Output is correct |
17 |
Correct |
248 ms |
78624 KB |
Output is correct |
18 |
Correct |
231 ms |
79236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
79328 KB |
Output is correct |
2 |
Correct |
237 ms |
79288 KB |
Output is correct |
3 |
Correct |
256 ms |
79620 KB |
Output is correct |
4 |
Correct |
187 ms |
79220 KB |
Output is correct |
5 |
Correct |
145 ms |
80204 KB |
Output is correct |
6 |
Correct |
216 ms |
79608 KB |
Output is correct |
7 |
Correct |
193 ms |
79824 KB |
Output is correct |
8 |
Correct |
207 ms |
79352 KB |
Output is correct |
9 |
Correct |
212 ms |
79344 KB |
Output is correct |
10 |
Correct |
180 ms |
78724 KB |
Output is correct |
11 |
Correct |
175 ms |
78844 KB |
Output is correct |
12 |
Correct |
221 ms |
78860 KB |
Output is correct |
13 |
Correct |
310 ms |
79848 KB |
Output is correct |
14 |
Correct |
220 ms |
79408 KB |
Output is correct |
15 |
Correct |
205 ms |
78852 KB |
Output is correct |
16 |
Correct |
223 ms |
78828 KB |
Output is correct |
17 |
Correct |
241 ms |
79120 KB |
Output is correct |
18 |
Correct |
239 ms |
79196 KB |
Output is correct |
19 |
Correct |
120 ms |
78852 KB |
Output is correct |
20 |
Correct |
268 ms |
79604 KB |
Output is correct |
21 |
Correct |
224 ms |
79408 KB |
Output is correct |
22 |
Correct |
252 ms |
80016 KB |
Output is correct |
23 |
Correct |
152 ms |
79720 KB |
Output is correct |
24 |
Correct |
166 ms |
78924 KB |
Output is correct |
25 |
Correct |
224 ms |
78972 KB |
Output is correct |
26 |
Correct |
189 ms |
79088 KB |
Output is correct |
27 |
Correct |
264 ms |
79624 KB |
Output is correct |
28 |
Correct |
159 ms |
78904 KB |
Output is correct |
29 |
Correct |
244 ms |
80108 KB |
Output is correct |
30 |
Correct |
233 ms |
79916 KB |
Output is correct |
31 |
Correct |
144 ms |
79040 KB |
Output is correct |
32 |
Correct |
194 ms |
78916 KB |
Output is correct |
33 |
Correct |
139 ms |
79028 KB |
Output is correct |
34 |
Correct |
209 ms |
79624 KB |
Output is correct |
35 |
Correct |
165 ms |
78984 KB |
Output is correct |
36 |
Correct |
248 ms |
79956 KB |
Output is correct |
37 |
Correct |
155 ms |
79996 KB |
Output is correct |
38 |
Correct |
223 ms |
79636 KB |
Output is correct |
39 |
Correct |
157 ms |
78992 KB |
Output is correct |
40 |
Correct |
199 ms |
79564 KB |
Output is correct |
41 |
Correct |
174 ms |
79552 KB |
Output is correct |
42 |
Correct |
276 ms |
79124 KB |
Output is correct |