#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);
s++;
assert(s <= 500 * mxN);
}
}
sort(all[i].begin(), all[i].end());
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 |
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 |
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 |
3 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 |
# |
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 |
2 ms |
3156 KB |
Output is correct |
11 |
Correct |
3 ms |
3156 KB |
Output is correct |
12 |
Correct |
2 ms |
3072 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 |
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 |
3 ms |
3284 KB |
Output is correct |
23 |
Correct |
3 ms |
3284 KB |
Output is correct |
24 |
Correct |
4 ms |
3412 KB |
Output is correct |
25 |
Correct |
6 ms |
3388 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 |
3668 KB |
Output is correct |
30 |
Correct |
5 ms |
3344 KB |
Output is correct |
31 |
Correct |
5 ms |
3540 KB |
Output is correct |
32 |
Correct |
5 ms |
3420 KB |
Output is correct |
33 |
Correct |
20 ms |
4316 KB |
Output is correct |
34 |
Correct |
19 ms |
4312 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 |
3104 KB |
Output is correct |
7 |
Correct |
2 ms |
3028 KB |
Output is correct |
8 |
Correct |
3 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 |
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 |
3288 KB |
Output is correct |
16 |
Correct |
3 ms |
3156 KB |
Output is correct |
17 |
Correct |
5 ms |
3392 KB |
Output is correct |
18 |
Correct |
2 ms |
3284 KB |
Output is correct |
19 |
Correct |
3 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 |
2 ms |
3284 KB |
Output is correct |
23 |
Correct |
3 ms |
3284 KB |
Output is correct |
24 |
Correct |
4 ms |
3428 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 |
10 ms |
3756 KB |
Output is correct |
30 |
Correct |
4 ms |
3284 KB |
Output is correct |
31 |
Correct |
5 ms |
3552 KB |
Output is correct |
32 |
Correct |
4 ms |
3412 KB |
Output is correct |
33 |
Correct |
20 ms |
4308 KB |
Output is correct |
34 |
Correct |
21 ms |
4300 KB |
Output is correct |
35 |
Correct |
25 ms |
5148 KB |
Output is correct |
36 |
Correct |
5 ms |
3536 KB |
Output is correct |
37 |
Correct |
40 ms |
5596 KB |
Output is correct |
38 |
Correct |
39 ms |
5844 KB |
Output is correct |
39 |
Correct |
38 ms |
5884 KB |
Output is correct |
40 |
Correct |
41 ms |
6044 KB |
Output is correct |
41 |
Correct |
43 ms |
5880 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 |
92 ms |
8276 KB |
Output is correct |
46 |
Correct |
93 ms |
8256 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 |
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 |
3156 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 |
4 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 |
2 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 |
4 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 |
3 ms |
3284 KB |
Output is correct |
28 |
Correct |
4 ms |
3540 KB |
Output is correct |
29 |
Correct |
9 ms |
3756 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 |
3464 KB |
Output is correct |
33 |
Correct |
19 ms |
4308 KB |
Output is correct |
34 |
Correct |
24 ms |
4308 KB |
Output is correct |
35 |
Correct |
29 ms |
5148 KB |
Output is correct |
36 |
Correct |
7 ms |
3540 KB |
Output is correct |
37 |
Correct |
41 ms |
5684 KB |
Output is correct |
38 |
Correct |
39 ms |
5912 KB |
Output is correct |
39 |
Correct |
38 ms |
5920 KB |
Output is correct |
40 |
Correct |
39 ms |
5892 KB |
Output is correct |
41 |
Correct |
38 ms |
5844 KB |
Output is correct |
42 |
Correct |
7 ms |
3548 KB |
Output is correct |
43 |
Correct |
7 ms |
3628 KB |
Output is correct |
44 |
Correct |
7 ms |
3668 KB |
Output is correct |
45 |
Correct |
96 ms |
8272 KB |
Output is correct |
46 |
Correct |
97 ms |
8360 KB |
Output is correct |
47 |
Correct |
184 ms |
11596 KB |
Output is correct |
48 |
Correct |
29 ms |
7060 KB |
Output is correct |
49 |
Correct |
20 ms |
6388 KB |
Output is correct |
50 |
Correct |
23 ms |
6184 KB |
Output is correct |
51 |
Correct |
70 ms |
8572 KB |
Output is correct |
52 |
Correct |
84 ms |
8896 KB |
Output is correct |
53 |
Correct |
22 ms |
6632 KB |
Output is correct |
54 |
Correct |
5 ms |
4948 KB |
Output is correct |
55 |
Correct |
7 ms |
5040 KB |
Output is correct |
56 |
Correct |
12 ms |
6228 KB |
Output is correct |
57 |
Correct |
60 ms |
8012 KB |
Output is correct |
58 |
Correct |
13 ms |
5460 KB |
Output is correct |
59 |
Correct |
19 ms |
5460 KB |
Output is correct |
60 |
Correct |
21 ms |
5588 KB |
Output is correct |
61 |
Correct |
21 ms |
5608 KB |
Output is correct |
62 |
Correct |
104 ms |
10836 KB |
Output is correct |
63 |
Correct |
767 ms |
28996 KB |
Output is correct |
64 |
Correct |
935 ms |
32028 KB |
Output is correct |
65 |
Execution timed out |
1090 ms |
48984 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |