#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];
}
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());
assert(all[i].size() <= 700);
dist[i].assign(all[i].size(), 1e9);
}
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 |
3 ms |
3076 KB |
Output is correct |
7 |
Correct |
3 ms |
3028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
3 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
2 ms |
3140 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 |
3028 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 |
4 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 |
3048 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 |
3028 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 |
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 |
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 |
3 ms |
3284 KB |
Output is correct |
23 |
Correct |
5 ms |
3284 KB |
Output is correct |
24 |
Correct |
4 ms |
3412 KB |
Output is correct |
25 |
Correct |
3 ms |
3284 KB |
Output is correct |
26 |
Correct |
5 ms |
3284 KB |
Output is correct |
27 |
Correct |
3 ms |
3284 KB |
Output is correct |
28 |
Correct |
7 ms |
3508 KB |
Output is correct |
29 |
Correct |
10 ms |
3632 KB |
Output is correct |
30 |
Correct |
5 ms |
3284 KB |
Output is correct |
31 |
Correct |
6 ms |
3540 KB |
Output is correct |
32 |
Correct |
6 ms |
3412 KB |
Output is correct |
33 |
Correct |
20 ms |
4308 KB |
Output is correct |
34 |
Correct |
19 ms |
4180 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 |
3028 KB |
Output is correct |
10 |
Correct |
3 ms |
3156 KB |
Output is correct |
11 |
Correct |
3 ms |
3156 KB |
Output is correct |
12 |
Correct |
3 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 |
3284 KB |
Output is correct |
18 |
Correct |
2 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 |
3284 KB |
Output is correct |
23 |
Correct |
5 ms |
3284 KB |
Output is correct |
24 |
Correct |
5 ms |
3412 KB |
Output is correct |
25 |
Correct |
4 ms |
3284 KB |
Output is correct |
26 |
Correct |
3 ms |
3284 KB |
Output is correct |
27 |
Correct |
4 ms |
3300 KB |
Output is correct |
28 |
Correct |
5 ms |
3540 KB |
Output is correct |
29 |
Correct |
11 ms |
3668 KB |
Output is correct |
30 |
Correct |
3 ms |
3284 KB |
Output is correct |
31 |
Correct |
6 ms |
3540 KB |
Output is correct |
32 |
Correct |
4 ms |
3464 KB |
Output is correct |
33 |
Correct |
21 ms |
4224 KB |
Output is correct |
34 |
Correct |
19 ms |
4308 KB |
Output is correct |
35 |
Correct |
24 ms |
5076 KB |
Output is correct |
36 |
Correct |
6 ms |
3540 KB |
Output is correct |
37 |
Correct |
47 ms |
5588 KB |
Output is correct |
38 |
Correct |
41 ms |
5876 KB |
Output is correct |
39 |
Correct |
42 ms |
5856 KB |
Output is correct |
40 |
Correct |
40 ms |
5844 KB |
Output is correct |
41 |
Correct |
45 ms |
5976 KB |
Output is correct |
42 |
Correct |
10 ms |
3556 KB |
Output is correct |
43 |
Correct |
11 ms |
3664 KB |
Output is correct |
44 |
Correct |
7 ms |
3668 KB |
Output is correct |
45 |
Correct |
98 ms |
8288 KB |
Output is correct |
46 |
Correct |
94 ms |
8404 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 |
3 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 |
3028 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 |
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 |
3300 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 |
3 ms |
3156 KB |
Output is correct |
22 |
Correct |
3 ms |
3284 KB |
Output is correct |
23 |
Correct |
4 ms |
3328 KB |
Output is correct |
24 |
Correct |
6 ms |
3412 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 |
6 ms |
3556 KB |
Output is correct |
29 |
Correct |
11 ms |
3636 KB |
Output is correct |
30 |
Correct |
4 ms |
3284 KB |
Output is correct |
31 |
Correct |
5 ms |
3540 KB |
Output is correct |
32 |
Correct |
5 ms |
3464 KB |
Output is correct |
33 |
Correct |
19 ms |
4220 KB |
Output is correct |
34 |
Correct |
19 ms |
4180 KB |
Output is correct |
35 |
Correct |
26 ms |
5168 KB |
Output is correct |
36 |
Correct |
6 ms |
3540 KB |
Output is correct |
37 |
Correct |
43 ms |
5612 KB |
Output is correct |
38 |
Correct |
45 ms |
5844 KB |
Output is correct |
39 |
Correct |
53 ms |
5844 KB |
Output is correct |
40 |
Correct |
43 ms |
5892 KB |
Output is correct |
41 |
Correct |
40 ms |
5936 KB |
Output is correct |
42 |
Correct |
7 ms |
3548 KB |
Output is correct |
43 |
Correct |
7 ms |
3668 KB |
Output is correct |
44 |
Correct |
7 ms |
3668 KB |
Output is correct |
45 |
Correct |
98 ms |
8320 KB |
Output is correct |
46 |
Correct |
103 ms |
8380 KB |
Output is correct |
47 |
Correct |
224 ms |
11676 KB |
Output is correct |
48 |
Correct |
24 ms |
7124 KB |
Output is correct |
49 |
Correct |
19 ms |
6308 KB |
Output is correct |
50 |
Correct |
18 ms |
6208 KB |
Output is correct |
51 |
Correct |
81 ms |
8568 KB |
Output is correct |
52 |
Correct |
92 ms |
8884 KB |
Output is correct |
53 |
Correct |
28 ms |
6612 KB |
Output is correct |
54 |
Correct |
6 ms |
4948 KB |
Output is correct |
55 |
Correct |
7 ms |
4948 KB |
Output is correct |
56 |
Correct |
13 ms |
6236 KB |
Output is correct |
57 |
Correct |
60 ms |
8008 KB |
Output is correct |
58 |
Correct |
14 ms |
5456 KB |
Output is correct |
59 |
Correct |
16 ms |
5460 KB |
Output is correct |
60 |
Correct |
21 ms |
5588 KB |
Output is correct |
61 |
Runtime error |
19 ms |
11000 KB |
Execution killed with signal 6 |
62 |
Halted |
0 ms |
0 KB |
- |