#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e7+10;
int stepptr = 1;
int step[MAXN], dist[MAXN];
void build_gp(int *prs, int m){
int *gp = dist;
fill(gp, gp+MAXN, 1);
int *lp = step;
for (int i=0; i<m; i++){
gp[prs[i]] = prs[i];
}
vector<int> pr;
for (int i=2; i<MAXN; i++){
if (lp[i] == 0){
lp[i] = i;
pr.push_back(i);
}
for (int j=0; j<pr.size() && pr[j] <= lp[i] && pr[j] * i < MAXN; j++){
lp[pr[j] * i] = pr[j];
gp[pr[j] * i] = max(gp[pr[j]], gp[i]);
}
}
for (int i=0; i<MAXN-1; i++){
dist[i] = gp[i+1] - 1;
}
}
void build_dist(int m){
int prs[m];
for (int i=0; i<m; i++) cin >> prs[i];
sort(prs, prs + m);
m = unique(prs, prs + m) - prs;
build_gp(prs, m);
for (int i=0; i<m; i++){
dist[MAXN-1] = max(dist[MAXN-1], (MAXN-1) % prs[i]);
}
for (int i=MAXN-2; i>=0; i--){
dist[i] = max(dist[i], dist[i+1]-1);
}
}
void build_step(){
step[0] = 0;
for (; stepptr<MAXN && dist[stepptr]; stepptr++){
step[stepptr] = step[stepptr - dist[stepptr]] + 1;
}
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int m, q; cin >> m >> q;
build_dist(m);
build_step();
for (int i=0; i<q; i++){
int val; cin >> val;
if (val >= stepptr){
cout << "oo\n";
}
else{
cout << step[val] << "\n";
}
}
}
Compilation message
brunhilda.cpp: In function 'void build_gp(int*, int)':
brunhilda.cpp:22:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int j=0; j<pr.size() && pr[j] <= lp[i] && pr[j] * i < MAXN; j++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
82884 KB |
Output is correct |
2 |
Correct |
174 ms |
82760 KB |
Output is correct |
3 |
Correct |
168 ms |
82736 KB |
Output is correct |
4 |
Correct |
190 ms |
82808 KB |
Output is correct |
5 |
Correct |
171 ms |
82876 KB |
Output is correct |
6 |
Correct |
166 ms |
82856 KB |
Output is correct |
7 |
Correct |
167 ms |
82752 KB |
Output is correct |
8 |
Correct |
165 ms |
82752 KB |
Output is correct |
9 |
Correct |
177 ms |
82816 KB |
Output is correct |
10 |
Correct |
192 ms |
82732 KB |
Output is correct |
11 |
Correct |
176 ms |
82720 KB |
Output is correct |
12 |
Correct |
182 ms |
82772 KB |
Output is correct |
13 |
Correct |
182 ms |
82728 KB |
Output is correct |
14 |
Correct |
175 ms |
82852 KB |
Output is correct |
15 |
Correct |
174 ms |
82796 KB |
Output is correct |
16 |
Correct |
176 ms |
82756 KB |
Output is correct |
17 |
Correct |
178 ms |
82980 KB |
Output is correct |
18 |
Correct |
178 ms |
82808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
82772 KB |
Output is correct |
2 |
Correct |
186 ms |
83132 KB |
Output is correct |
3 |
Correct |
185 ms |
83052 KB |
Output is correct |
4 |
Correct |
175 ms |
82764 KB |
Output is correct |
5 |
Correct |
180 ms |
83068 KB |
Output is correct |
6 |
Correct |
191 ms |
82820 KB |
Output is correct |
7 |
Correct |
174 ms |
82908 KB |
Output is correct |
8 |
Correct |
172 ms |
82744 KB |
Output is correct |
9 |
Correct |
184 ms |
83044 KB |
Output is correct |
10 |
Correct |
180 ms |
83120 KB |
Output is correct |
11 |
Correct |
178 ms |
82900 KB |
Output is correct |
12 |
Correct |
178 ms |
82764 KB |
Output is correct |
13 |
Correct |
174 ms |
82836 KB |
Output is correct |
14 |
Correct |
177 ms |
82824 KB |
Output is correct |
15 |
Correct |
180 ms |
83108 KB |
Output is correct |
16 |
Correct |
201 ms |
83140 KB |
Output is correct |
17 |
Correct |
176 ms |
82768 KB |
Output is correct |
18 |
Correct |
181 ms |
83100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
82972 KB |
Output is correct |
2 |
Correct |
198 ms |
83040 KB |
Output is correct |
3 |
Correct |
183 ms |
83028 KB |
Output is correct |
4 |
Correct |
195 ms |
82812 KB |
Output is correct |
5 |
Correct |
203 ms |
83204 KB |
Output is correct |
6 |
Correct |
195 ms |
82784 KB |
Output is correct |
7 |
Correct |
191 ms |
83124 KB |
Output is correct |
8 |
Correct |
188 ms |
82940 KB |
Output is correct |
9 |
Correct |
187 ms |
83136 KB |
Output is correct |
10 |
Correct |
179 ms |
82948 KB |
Output is correct |
11 |
Correct |
179 ms |
82792 KB |
Output is correct |
12 |
Correct |
192 ms |
82840 KB |
Output is correct |
13 |
Correct |
192 ms |
82916 KB |
Output is correct |
14 |
Correct |
196 ms |
82808 KB |
Output is correct |
15 |
Correct |
184 ms |
82756 KB |
Output is correct |
16 |
Correct |
181 ms |
82752 KB |
Output is correct |
17 |
Correct |
183 ms |
83008 KB |
Output is correct |
18 |
Correct |
184 ms |
83040 KB |
Output is correct |
19 |
Correct |
182 ms |
82816 KB |
Output is correct |
20 |
Correct |
186 ms |
83016 KB |
Output is correct |
21 |
Correct |
185 ms |
82772 KB |
Output is correct |
22 |
Correct |
207 ms |
83136 KB |
Output is correct |
23 |
Correct |
201 ms |
82848 KB |
Output is correct |
24 |
Correct |
195 ms |
82756 KB |
Output is correct |
25 |
Correct |
213 ms |
82828 KB |
Output is correct |
26 |
Correct |
195 ms |
82732 KB |
Output is correct |
27 |
Correct |
194 ms |
83260 KB |
Output is correct |
28 |
Correct |
193 ms |
82844 KB |
Output is correct |
29 |
Correct |
213 ms |
83192 KB |
Output is correct |
30 |
Correct |
206 ms |
83008 KB |
Output is correct |
31 |
Correct |
196 ms |
82812 KB |
Output is correct |
32 |
Correct |
196 ms |
82780 KB |
Output is correct |
33 |
Correct |
195 ms |
82756 KB |
Output is correct |
34 |
Correct |
196 ms |
83200 KB |
Output is correct |
35 |
Correct |
188 ms |
82780 KB |
Output is correct |
36 |
Correct |
208 ms |
83152 KB |
Output is correct |
37 |
Correct |
196 ms |
83096 KB |
Output is correct |
38 |
Correct |
195 ms |
82832 KB |
Output is correct |
39 |
Correct |
197 ms |
82800 KB |
Output is correct |
40 |
Correct |
195 ms |
82812 KB |
Output is correct |
41 |
Correct |
195 ms |
83164 KB |
Output is correct |
42 |
Correct |
186 ms |
82820 KB |
Output is correct |