#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 30'000;
vector<int> all[mxN], init[mxN], l[mxN];
vector<int> dist[mxN];
bool vis[mxN];
int idx(int i, int j) {
return lower_bound(all[i].begin(), all[i].end(), j) - all[i].begin();
}
void testCase() {
int n, m;
cin >> n >> m;
vector<int> v;
while (m--) {
int b, p;
cin >> b >> p;
v.push_back(b);
init[b].push_back(p);
l[b % p].push_back(p);
}
for (int i = 0; i < n; i++) {
sort(l[i].begin(), l[i].end());
sort(init[i].begin(), init[i].end());
init[i].erase(unique(init[i].begin(), init[i].end()), init[i].end());
l[i].erase(unique(l[i].begin(), l[i].end()), l[i].end());
all[i] = l[i];
}
int s = 0;
for (int i = 0; i < n; i++) {
for (int j : l[i]) {
for (int k = i; k < n; k += j) all[k].push_back(j);
}
sort(all[i].begin(), all[i].end());
s += all[i].size();
dist[i].assign(all[i].size(), 1e9);
}
assert(s <= 500 * mxN);
queue<pair<int, int>> q;
for (auto p : init[v[0]]) {
vis[v[0]] = true;
q.push({v[0], p});
dist[v[0]][idx(v[0], p)] = 0;
}
while (!q.empty()) {
auto [u, p] = q.front();
int id = idx(u, p);
q.pop();
if (u + p < n && dist[u + p][idx(u + p, p)] == 1e9) {
dist[u + p][idx(u + p, p)] = dist[u][id] + 1;
q.push({u + p, p});
}
if (u + p < n && !vis[u + p]) {
vis[u + p] = true;
for (int pp : init[u + p]) {
int i = idx(u + p, pp);
if (dist[u + p][i] == 1e9) {
dist[u + p][i] = dist[u][id] + 1;
q.push({u + p, pp});
}
}
}
if (u - p >= 0 && dist[u - p][idx(u - p, p)] == 1e9) {
dist[u - p][idx(u - p, p)] = dist[u][id] + 1;
q.push({u - p, p});
}
if (u - p >= 0 && !vis[u - p]) {
vis[u - p] = true;
for (int pp : init[u - p]) {
int i = idx(u - p, pp);
if (dist[u - p][i] == 1e9) {
dist[u - p][i] = dist[u][id] + 1;
q.push({u - p, pp});
}
}
}
}
int ans = *min_element(dist[v[1]].begin(), dist[v[1]].end());
cout << (ans == 1e9 ? -1 : ans) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
2 ms |
3028 KB |
Output is correct |
5 |
Correct |
2 ms |
3028 KB |
Output is correct |
6 |
Correct |
2 ms |
3028 KB |
Output is correct |
7 |
Correct |
2 ms |
3028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3148 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
3 ms |
3028 KB |
Output is correct |
5 |
Correct |
3 ms |
3028 KB |
Output is correct |
6 |
Correct |
2 ms |
3028 KB |
Output is correct |
7 |
Correct |
2 ms |
3028 KB |
Output is correct |
8 |
Correct |
2 ms |
3084 KB |
Output is correct |
9 |
Correct |
3 ms |
3156 KB |
Output is correct |
10 |
Correct |
2 ms |
3156 KB |
Output is correct |
11 |
Correct |
3 ms |
3156 KB |
Output is correct |
12 |
Correct |
2 ms |
3156 KB |
Output is correct |
13 |
Correct |
2 ms |
3156 KB |
Output is correct |
14 |
Correct |
4 ms |
3284 KB |
Output is correct |
15 |
Correct |
3 ms |
3284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
2 ms |
3028 KB |
Output is correct |
5 |
Correct |
2 ms |
3028 KB |
Output is correct |
6 |
Correct |
2 ms |
3028 KB |
Output is correct |
7 |
Correct |
2 ms |
3028 KB |
Output is correct |
8 |
Correct |
2 ms |
3028 KB |
Output is correct |
9 |
Correct |
2 ms |
3156 KB |
Output is correct |
10 |
Correct |
3 ms |
3156 KB |
Output is correct |
11 |
Correct |
3 ms |
3156 KB |
Output is correct |
12 |
Correct |
2 ms |
3156 KB |
Output is correct |
13 |
Correct |
2 ms |
3156 KB |
Output is correct |
14 |
Correct |
3 ms |
3284 KB |
Output is correct |
15 |
Correct |
3 ms |
3284 KB |
Output is correct |
16 |
Correct |
2 ms |
3156 KB |
Output is correct |
17 |
Correct |
5 ms |
3412 KB |
Output is correct |
18 |
Correct |
3 ms |
3284 KB |
Output is correct |
19 |
Correct |
2 ms |
3284 KB |
Output is correct |
20 |
Correct |
3 ms |
3284 KB |
Output is correct |
21 |
Correct |
2 ms |
3156 KB |
Output is correct |
22 |
Correct |
2 ms |
3280 KB |
Output is correct |
23 |
Correct |
3 ms |
3284 KB |
Output is correct |
24 |
Correct |
5 ms |
3412 KB |
Output is correct |
25 |
Correct |
3 ms |
3332 KB |
Output is correct |
26 |
Correct |
3 ms |
3284 KB |
Output is correct |
27 |
Correct |
3 ms |
3284 KB |
Output is correct |
28 |
Correct |
5 ms |
3540 KB |
Output is correct |
29 |
Correct |
11 ms |
3764 KB |
Output is correct |
30 |
Correct |
4 ms |
3284 KB |
Output is correct |
31 |
Correct |
5 ms |
3532 KB |
Output is correct |
32 |
Correct |
4 ms |
3412 KB |
Output is correct |
33 |
Correct |
20 ms |
4308 KB |
Output is correct |
34 |
Correct |
19 ms |
4308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
2 ms |
3028 KB |
Output is correct |
5 |
Correct |
2 ms |
3028 KB |
Output is correct |
6 |
Correct |
2 ms |
3028 KB |
Output is correct |
7 |
Correct |
2 ms |
3028 KB |
Output is correct |
8 |
Correct |
2 ms |
3148 KB |
Output is correct |
9 |
Correct |
2 ms |
3156 KB |
Output is correct |
10 |
Correct |
2 ms |
3144 KB |
Output is correct |
11 |
Correct |
3 ms |
3284 KB |
Output is correct |
12 |
Correct |
2 ms |
3160 KB |
Output is correct |
13 |
Correct |
2 ms |
3156 KB |
Output is correct |
14 |
Correct |
3 ms |
3284 KB |
Output is correct |
15 |
Correct |
3 ms |
3284 KB |
Output is correct |
16 |
Correct |
2 ms |
3156 KB |
Output is correct |
17 |
Correct |
4 ms |
3284 KB |
Output is correct |
18 |
Correct |
3 ms |
3284 KB |
Output is correct |
19 |
Correct |
3 ms |
3284 KB |
Output is correct |
20 |
Correct |
3 ms |
3284 KB |
Output is correct |
21 |
Correct |
2 ms |
3156 KB |
Output is correct |
22 |
Correct |
3 ms |
3284 KB |
Output is correct |
23 |
Correct |
3 ms |
3284 KB |
Output is correct |
24 |
Correct |
5 ms |
3416 KB |
Output is correct |
25 |
Correct |
4 ms |
3284 KB |
Output is correct |
26 |
Correct |
4 ms |
3284 KB |
Output is correct |
27 |
Correct |
4 ms |
3284 KB |
Output is correct |
28 |
Correct |
5 ms |
3540 KB |
Output is correct |
29 |
Correct |
10 ms |
3668 KB |
Output is correct |
30 |
Correct |
4 ms |
3284 KB |
Output is correct |
31 |
Correct |
5 ms |
3540 KB |
Output is correct |
32 |
Correct |
4 ms |
3400 KB |
Output is correct |
33 |
Correct |
22 ms |
4308 KB |
Output is correct |
34 |
Correct |
19 ms |
4320 KB |
Output is correct |
35 |
Correct |
26 ms |
5380 KB |
Output is correct |
36 |
Correct |
5 ms |
3540 KB |
Output is correct |
37 |
Correct |
39 ms |
5844 KB |
Output is correct |
38 |
Correct |
39 ms |
6100 KB |
Output is correct |
39 |
Correct |
39 ms |
6184 KB |
Output is correct |
40 |
Correct |
40 ms |
6228 KB |
Output is correct |
41 |
Correct |
40 ms |
6132 KB |
Output is correct |
42 |
Correct |
8 ms |
3876 KB |
Output is correct |
43 |
Correct |
7 ms |
3796 KB |
Output is correct |
44 |
Correct |
8 ms |
3864 KB |
Output is correct |
45 |
Correct |
96 ms |
8480 KB |
Output is correct |
46 |
Correct |
96 ms |
8532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
2 ms |
3028 KB |
Output is correct |
5 |
Correct |
2 ms |
3028 KB |
Output is correct |
6 |
Correct |
2 ms |
3148 KB |
Output is correct |
7 |
Correct |
2 ms |
3028 KB |
Output is correct |
8 |
Correct |
2 ms |
3028 KB |
Output is correct |
9 |
Correct |
2 ms |
3148 KB |
Output is correct |
10 |
Correct |
2 ms |
3156 KB |
Output is correct |
11 |
Correct |
3 ms |
3152 KB |
Output is correct |
12 |
Correct |
2 ms |
3156 KB |
Output is correct |
13 |
Correct |
3 ms |
3156 KB |
Output is correct |
14 |
Correct |
3 ms |
3284 KB |
Output is correct |
15 |
Correct |
3 ms |
3284 KB |
Output is correct |
16 |
Correct |
2 ms |
3156 KB |
Output is correct |
17 |
Correct |
5 ms |
3284 KB |
Output is correct |
18 |
Correct |
3 ms |
3332 KB |
Output is correct |
19 |
Correct |
2 ms |
3284 KB |
Output is correct |
20 |
Correct |
2 ms |
3284 KB |
Output is correct |
21 |
Correct |
2 ms |
3156 KB |
Output is correct |
22 |
Correct |
4 ms |
3284 KB |
Output is correct |
23 |
Correct |
3 ms |
3284 KB |
Output is correct |
24 |
Correct |
5 ms |
3360 KB |
Output is correct |
25 |
Correct |
3 ms |
3284 KB |
Output is correct |
26 |
Correct |
3 ms |
3284 KB |
Output is correct |
27 |
Correct |
3 ms |
3284 KB |
Output is correct |
28 |
Correct |
5 ms |
3540 KB |
Output is correct |
29 |
Correct |
9 ms |
3764 KB |
Output is correct |
30 |
Correct |
3 ms |
3284 KB |
Output is correct |
31 |
Correct |
5 ms |
3540 KB |
Output is correct |
32 |
Correct |
4 ms |
3408 KB |
Output is correct |
33 |
Correct |
19 ms |
4308 KB |
Output is correct |
34 |
Correct |
20 ms |
4324 KB |
Output is correct |
35 |
Correct |
26 ms |
5348 KB |
Output is correct |
36 |
Correct |
5 ms |
3540 KB |
Output is correct |
37 |
Correct |
39 ms |
5844 KB |
Output is correct |
38 |
Correct |
39 ms |
6100 KB |
Output is correct |
39 |
Correct |
39 ms |
6108 KB |
Output is correct |
40 |
Correct |
40 ms |
6100 KB |
Output is correct |
41 |
Correct |
39 ms |
6108 KB |
Output is correct |
42 |
Correct |
7 ms |
3796 KB |
Output is correct |
43 |
Correct |
8 ms |
3796 KB |
Output is correct |
44 |
Correct |
7 ms |
3924 KB |
Output is correct |
45 |
Correct |
91 ms |
8532 KB |
Output is correct |
46 |
Correct |
90 ms |
8524 KB |
Output is correct |
47 |
Correct |
204 ms |
11984 KB |
Output is correct |
48 |
Correct |
23 ms |
7324 KB |
Output is correct |
49 |
Correct |
18 ms |
6564 KB |
Output is correct |
50 |
Correct |
23 ms |
6348 KB |
Output is correct |
51 |
Correct |
79 ms |
8904 KB |
Output is correct |
52 |
Correct |
73 ms |
9188 KB |
Output is correct |
53 |
Correct |
22 ms |
6872 KB |
Output is correct |
54 |
Correct |
5 ms |
4940 KB |
Output is correct |
55 |
Correct |
6 ms |
5052 KB |
Output is correct |
56 |
Correct |
13 ms |
6356 KB |
Output is correct |
57 |
Correct |
66 ms |
8032 KB |
Output is correct |
58 |
Correct |
13 ms |
5588 KB |
Output is correct |
59 |
Correct |
17 ms |
5716 KB |
Output is correct |
60 |
Correct |
23 ms |
5800 KB |
Output is correct |
61 |
Correct |
24 ms |
5844 KB |
Output is correct |
62 |
Correct |
99 ms |
11212 KB |
Output is correct |
63 |
Correct |
783 ms |
29120 KB |
Output is correct |
64 |
Execution timed out |
1020 ms |
32256 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |