#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using i32 = int;
using u32 = unsigned;
using i16 = short;
using u16 = unsigned short;
using ld = long double;
using ii = pair<int, int>;
const int N = 3e4 + 100, INF = 1e9;
vector<int> P[N];
map<int, int> jumps[N];
void solve() {
int n, m;
cin >> n >> m;
vector<int> cnt(n, INF);
queue<ii> q;
int x0 = -1, x1 = -1;
for(int i = 0; i < m; ++i) {
int b, p;
cin >> b >> p;
if(!i) x0 = b;
else if(i == 1) x1 = b;
for(int x = b % p; x < n; x += p) jumps[x][p] = -1;
P[b].push_back(p);
}
while(!P[x0].empty()) {
int k = P[x0].back();
P[x0].pop_back();
if(jumps[x0][k] == -1) {
jumps[x0][k] = 0;
q.emplace(x0, k);
}
}
while(!q.empty()) {
auto [x, p] = q.front();
q.pop();
int j = jumps[x][p];
cnt[x] = min(cnt[x], j);
if(x == x1) break;
if(x >= p && jumps[x - p][p] == -1) {
jumps[x - p][p] = 1 + j;
q.emplace(x - p, p);
}
if(x + p < n && jumps[x + p][p] == -1) {
jumps[x + p][p] = 1 + j;
q.emplace(x + p, p);
}
while(!P[x].empty()) {
int k = P[x].back();
P[x].pop_back();
if(x >= k && jumps[x - k][k] == -1) {
jumps[x - k][k] = 1 + j;
q.emplace(x - k, k);
}
if(x + k < n && jumps[x + k][k] == -1) {
jumps[x + k][k] = 1 + j;
q.emplace(x + k, k);
}
}
}
if(cnt[x1] == INF) cout << "-1\n";
else cout << cnt[x1] << '\n';
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2380 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
1 ms |
2380 KB |
Output is correct |
4 |
Correct |
1 ms |
2380 KB |
Output is correct |
5 |
Correct |
1 ms |
2380 KB |
Output is correct |
6 |
Correct |
1 ms |
2380 KB |
Output is correct |
7 |
Correct |
1 ms |
2380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2380 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
2 ms |
2380 KB |
Output is correct |
4 |
Correct |
1 ms |
2380 KB |
Output is correct |
5 |
Correct |
1 ms |
2380 KB |
Output is correct |
6 |
Correct |
1 ms |
2380 KB |
Output is correct |
7 |
Correct |
1 ms |
2380 KB |
Output is correct |
8 |
Correct |
1 ms |
2380 KB |
Output is correct |
9 |
Correct |
1 ms |
2380 KB |
Output is correct |
10 |
Correct |
2 ms |
2508 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2380 KB |
Output is correct |
13 |
Correct |
3 ms |
2380 KB |
Output is correct |
14 |
Correct |
3 ms |
2636 KB |
Output is correct |
15 |
Correct |
3 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2380 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
1 ms |
2380 KB |
Output is correct |
4 |
Correct |
1 ms |
2380 KB |
Output is correct |
5 |
Correct |
2 ms |
2380 KB |
Output is correct |
6 |
Correct |
1 ms |
2380 KB |
Output is correct |
7 |
Correct |
1 ms |
2380 KB |
Output is correct |
8 |
Correct |
1 ms |
2380 KB |
Output is correct |
9 |
Correct |
1 ms |
2380 KB |
Output is correct |
10 |
Correct |
1 ms |
2380 KB |
Output is correct |
11 |
Correct |
3 ms |
2636 KB |
Output is correct |
12 |
Correct |
3 ms |
2380 KB |
Output is correct |
13 |
Correct |
2 ms |
2380 KB |
Output is correct |
14 |
Correct |
3 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
2 ms |
2508 KB |
Output is correct |
17 |
Correct |
4 ms |
3148 KB |
Output is correct |
18 |
Correct |
2 ms |
2764 KB |
Output is correct |
19 |
Correct |
2 ms |
2636 KB |
Output is correct |
20 |
Correct |
19 ms |
2572 KB |
Output is correct |
21 |
Correct |
2 ms |
2508 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
2 ms |
2636 KB |
Output is correct |
24 |
Correct |
4 ms |
3020 KB |
Output is correct |
25 |
Correct |
2 ms |
2764 KB |
Output is correct |
26 |
Correct |
27 ms |
2812 KB |
Output is correct |
27 |
Correct |
24 ms |
2764 KB |
Output is correct |
28 |
Correct |
6 ms |
3532 KB |
Output is correct |
29 |
Correct |
25 ms |
5308 KB |
Output is correct |
30 |
Correct |
5 ms |
3148 KB |
Output is correct |
31 |
Correct |
11 ms |
3916 KB |
Output is correct |
32 |
Correct |
7 ms |
3548 KB |
Output is correct |
33 |
Correct |
55 ms |
8060 KB |
Output is correct |
34 |
Correct |
48 ms |
8012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2380 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
1 ms |
2380 KB |
Output is correct |
4 |
Correct |
1 ms |
2380 KB |
Output is correct |
5 |
Correct |
2 ms |
2380 KB |
Output is correct |
6 |
Correct |
1 ms |
2380 KB |
Output is correct |
7 |
Correct |
1 ms |
2380 KB |
Output is correct |
8 |
Correct |
1 ms |
2380 KB |
Output is correct |
9 |
Correct |
1 ms |
2380 KB |
Output is correct |
10 |
Correct |
2 ms |
2380 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2380 KB |
Output is correct |
13 |
Correct |
2 ms |
2380 KB |
Output is correct |
14 |
Correct |
3 ms |
2636 KB |
Output is correct |
15 |
Correct |
4 ms |
2696 KB |
Output is correct |
16 |
Correct |
2 ms |
2508 KB |
Output is correct |
17 |
Correct |
4 ms |
3148 KB |
Output is correct |
18 |
Correct |
2 ms |
2764 KB |
Output is correct |
19 |
Correct |
2 ms |
2636 KB |
Output is correct |
20 |
Correct |
17 ms |
2508 KB |
Output is correct |
21 |
Correct |
2 ms |
2508 KB |
Output is correct |
22 |
Correct |
3 ms |
2636 KB |
Output is correct |
23 |
Correct |
3 ms |
2764 KB |
Output is correct |
24 |
Correct |
4 ms |
3020 KB |
Output is correct |
25 |
Correct |
2 ms |
2764 KB |
Output is correct |
26 |
Correct |
31 ms |
2800 KB |
Output is correct |
27 |
Correct |
24 ms |
2772 KB |
Output is correct |
28 |
Correct |
7 ms |
3572 KB |
Output is correct |
29 |
Correct |
25 ms |
5316 KB |
Output is correct |
30 |
Correct |
5 ms |
3148 KB |
Output is correct |
31 |
Correct |
10 ms |
4008 KB |
Output is correct |
32 |
Correct |
7 ms |
3560 KB |
Output is correct |
33 |
Correct |
63 ms |
8040 KB |
Output is correct |
34 |
Correct |
40 ms |
8012 KB |
Output is correct |
35 |
Correct |
34 ms |
7504 KB |
Output is correct |
36 |
Correct |
4 ms |
3276 KB |
Output is correct |
37 |
Correct |
71 ms |
10820 KB |
Output is correct |
38 |
Correct |
61 ms |
10284 KB |
Output is correct |
39 |
Correct |
49 ms |
10120 KB |
Output is correct |
40 |
Correct |
47 ms |
10180 KB |
Output is correct |
41 |
Correct |
55 ms |
10340 KB |
Output is correct |
42 |
Correct |
379 ms |
3004 KB |
Output is correct |
43 |
Correct |
393 ms |
3096 KB |
Output is correct |
44 |
Correct |
221 ms |
2672 KB |
Output is correct |
45 |
Correct |
527 ms |
25676 KB |
Output is correct |
46 |
Correct |
379 ms |
25648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2380 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
1 ms |
2380 KB |
Output is correct |
4 |
Correct |
1 ms |
2380 KB |
Output is correct |
5 |
Correct |
1 ms |
2380 KB |
Output is correct |
6 |
Correct |
1 ms |
2380 KB |
Output is correct |
7 |
Correct |
1 ms |
2380 KB |
Output is correct |
8 |
Correct |
1 ms |
2380 KB |
Output is correct |
9 |
Correct |
2 ms |
2380 KB |
Output is correct |
10 |
Correct |
2 ms |
2508 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
3 ms |
2380 KB |
Output is correct |
13 |
Correct |
2 ms |
2380 KB |
Output is correct |
14 |
Correct |
3 ms |
2636 KB |
Output is correct |
15 |
Correct |
3 ms |
2636 KB |
Output is correct |
16 |
Correct |
2 ms |
2508 KB |
Output is correct |
17 |
Correct |
4 ms |
3148 KB |
Output is correct |
18 |
Correct |
2 ms |
2764 KB |
Output is correct |
19 |
Correct |
3 ms |
2656 KB |
Output is correct |
20 |
Correct |
21 ms |
2580 KB |
Output is correct |
21 |
Correct |
2 ms |
2508 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
4 ms |
2764 KB |
Output is correct |
24 |
Correct |
4 ms |
3020 KB |
Output is correct |
25 |
Correct |
3 ms |
2764 KB |
Output is correct |
26 |
Correct |
27 ms |
2808 KB |
Output is correct |
27 |
Correct |
24 ms |
2764 KB |
Output is correct |
28 |
Correct |
7 ms |
3532 KB |
Output is correct |
29 |
Correct |
24 ms |
5248 KB |
Output is correct |
30 |
Correct |
5 ms |
3236 KB |
Output is correct |
31 |
Correct |
11 ms |
3916 KB |
Output is correct |
32 |
Correct |
10 ms |
3532 KB |
Output is correct |
33 |
Correct |
57 ms |
8108 KB |
Output is correct |
34 |
Correct |
42 ms |
8012 KB |
Output is correct |
35 |
Correct |
33 ms |
7596 KB |
Output is correct |
36 |
Correct |
5 ms |
3276 KB |
Output is correct |
37 |
Correct |
87 ms |
10948 KB |
Output is correct |
38 |
Correct |
66 ms |
10252 KB |
Output is correct |
39 |
Correct |
76 ms |
10112 KB |
Output is correct |
40 |
Correct |
51 ms |
10216 KB |
Output is correct |
41 |
Correct |
67 ms |
10312 KB |
Output is correct |
42 |
Correct |
406 ms |
3016 KB |
Output is correct |
43 |
Correct |
431 ms |
2904 KB |
Output is correct |
44 |
Correct |
230 ms |
2740 KB |
Output is correct |
45 |
Correct |
543 ms |
25672 KB |
Output is correct |
46 |
Correct |
448 ms |
25668 KB |
Output is correct |
47 |
Correct |
440 ms |
37448 KB |
Output is correct |
48 |
Correct |
61 ms |
13844 KB |
Output is correct |
49 |
Correct |
49 ms |
9748 KB |
Output is correct |
50 |
Correct |
27 ms |
9632 KB |
Output is correct |
51 |
Correct |
116 ms |
18080 KB |
Output is correct |
52 |
Correct |
166 ms |
19436 KB |
Output is correct |
53 |
Correct |
25 ms |
7308 KB |
Output is correct |
54 |
Correct |
4 ms |
3916 KB |
Output is correct |
55 |
Correct |
5 ms |
5068 KB |
Output is correct |
56 |
Execution timed out |
1089 ms |
4220 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |