#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];
unordered_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 |
2672 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2672 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
1 ms |
2668 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2568 KB |
Output is correct |
10 |
Correct |
1 ms |
2636 KB |
Output is correct |
11 |
Correct |
2 ms |
2764 KB |
Output is correct |
12 |
Correct |
4 ms |
2636 KB |
Output is correct |
13 |
Correct |
5 ms |
2676 KB |
Output is correct |
14 |
Correct |
3 ms |
2892 KB |
Output is correct |
15 |
Correct |
3 ms |
2892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2676 KB |
Output is correct |
2 |
Correct |
3 ms |
2672 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2660 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2636 KB |
Output is correct |
11 |
Correct |
2 ms |
2764 KB |
Output is correct |
12 |
Correct |
4 ms |
2636 KB |
Output is correct |
13 |
Correct |
5 ms |
2636 KB |
Output is correct |
14 |
Correct |
3 ms |
2936 KB |
Output is correct |
15 |
Correct |
2 ms |
2936 KB |
Output is correct |
16 |
Correct |
2 ms |
2764 KB |
Output is correct |
17 |
Correct |
3 ms |
3276 KB |
Output is correct |
18 |
Correct |
2 ms |
3060 KB |
Output is correct |
19 |
Correct |
2 ms |
3020 KB |
Output is correct |
20 |
Correct |
54 ms |
2996 KB |
Output is correct |
21 |
Correct |
3 ms |
2764 KB |
Output is correct |
22 |
Correct |
2 ms |
3064 KB |
Output is correct |
23 |
Correct |
4 ms |
3020 KB |
Output is correct |
24 |
Correct |
3 ms |
3276 KB |
Output is correct |
25 |
Correct |
4 ms |
3148 KB |
Output is correct |
26 |
Correct |
65 ms |
3164 KB |
Output is correct |
27 |
Correct |
55 ms |
3020 KB |
Output is correct |
28 |
Correct |
5 ms |
3660 KB |
Output is correct |
29 |
Correct |
13 ms |
5488 KB |
Output is correct |
30 |
Correct |
4 ms |
3304 KB |
Output is correct |
31 |
Correct |
7 ms |
4248 KB |
Output is correct |
32 |
Correct |
4 ms |
3584 KB |
Output is correct |
33 |
Correct |
31 ms |
8296 KB |
Output is correct |
34 |
Correct |
26 ms |
8268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
1 ms |
2672 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
1 ms |
2672 KB |
Output is correct |
5 |
Correct |
2 ms |
2748 KB |
Output is correct |
6 |
Correct |
2 ms |
2672 KB |
Output is correct |
7 |
Correct |
1 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2672 KB |
Output is correct |
11 |
Correct |
2 ms |
2764 KB |
Output is correct |
12 |
Correct |
4 ms |
2636 KB |
Output is correct |
13 |
Correct |
4 ms |
2636 KB |
Output is correct |
14 |
Correct |
3 ms |
2892 KB |
Output is correct |
15 |
Correct |
3 ms |
2892 KB |
Output is correct |
16 |
Correct |
2 ms |
2764 KB |
Output is correct |
17 |
Correct |
4 ms |
3276 KB |
Output is correct |
18 |
Correct |
3 ms |
3020 KB |
Output is correct |
19 |
Correct |
2 ms |
3020 KB |
Output is correct |
20 |
Correct |
53 ms |
3016 KB |
Output is correct |
21 |
Correct |
2 ms |
2764 KB |
Output is correct |
22 |
Correct |
2 ms |
3020 KB |
Output is correct |
23 |
Correct |
3 ms |
3020 KB |
Output is correct |
24 |
Correct |
5 ms |
3404 KB |
Output is correct |
25 |
Correct |
3 ms |
3148 KB |
Output is correct |
26 |
Correct |
61 ms |
3156 KB |
Output is correct |
27 |
Correct |
61 ms |
3020 KB |
Output is correct |
28 |
Correct |
6 ms |
3660 KB |
Output is correct |
29 |
Correct |
13 ms |
5540 KB |
Output is correct |
30 |
Correct |
4 ms |
3404 KB |
Output is correct |
31 |
Correct |
8 ms |
4248 KB |
Output is correct |
32 |
Correct |
5 ms |
3560 KB |
Output is correct |
33 |
Correct |
29 ms |
8316 KB |
Output is correct |
34 |
Correct |
26 ms |
8320 KB |
Output is correct |
35 |
Correct |
27 ms |
7808 KB |
Output is correct |
36 |
Correct |
4 ms |
3532 KB |
Output is correct |
37 |
Correct |
55 ms |
10488 KB |
Output is correct |
38 |
Correct |
40 ms |
10308 KB |
Output is correct |
39 |
Correct |
54 ms |
10148 KB |
Output is correct |
40 |
Correct |
40 ms |
10180 KB |
Output is correct |
41 |
Correct |
43 ms |
10384 KB |
Output is correct |
42 |
Correct |
956 ms |
3568 KB |
Output is correct |
43 |
Correct |
912 ms |
3840 KB |
Output is correct |
44 |
Correct |
766 ms |
3392 KB |
Output is correct |
45 |
Correct |
231 ms |
22596 KB |
Output is correct |
46 |
Correct |
216 ms |
22596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
1 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2672 KB |
Output is correct |
4 |
Correct |
1 ms |
2636 KB |
Output is correct |
5 |
Correct |
1 ms |
2668 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
1 ms |
2636 KB |
Output is correct |
9 |
Correct |
1 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2636 KB |
Output is correct |
11 |
Correct |
3 ms |
2892 KB |
Output is correct |
12 |
Correct |
4 ms |
2636 KB |
Output is correct |
13 |
Correct |
4 ms |
2636 KB |
Output is correct |
14 |
Correct |
3 ms |
2932 KB |
Output is correct |
15 |
Correct |
2 ms |
2892 KB |
Output is correct |
16 |
Correct |
2 ms |
2764 KB |
Output is correct |
17 |
Correct |
4 ms |
3320 KB |
Output is correct |
18 |
Correct |
2 ms |
3020 KB |
Output is correct |
19 |
Correct |
2 ms |
2928 KB |
Output is correct |
20 |
Correct |
52 ms |
3000 KB |
Output is correct |
21 |
Correct |
2 ms |
2812 KB |
Output is correct |
22 |
Correct |
2 ms |
3048 KB |
Output is correct |
23 |
Correct |
3 ms |
3016 KB |
Output is correct |
24 |
Correct |
3 ms |
3276 KB |
Output is correct |
25 |
Correct |
3 ms |
3108 KB |
Output is correct |
26 |
Correct |
61 ms |
3160 KB |
Output is correct |
27 |
Correct |
56 ms |
3136 KB |
Output is correct |
28 |
Correct |
6 ms |
3708 KB |
Output is correct |
29 |
Correct |
13 ms |
5492 KB |
Output is correct |
30 |
Correct |
4 ms |
3440 KB |
Output is correct |
31 |
Correct |
10 ms |
4172 KB |
Output is correct |
32 |
Correct |
6 ms |
3568 KB |
Output is correct |
33 |
Correct |
32 ms |
8272 KB |
Output is correct |
34 |
Correct |
26 ms |
8308 KB |
Output is correct |
35 |
Correct |
34 ms |
7864 KB |
Output is correct |
36 |
Correct |
5 ms |
3560 KB |
Output is correct |
37 |
Correct |
53 ms |
10484 KB |
Output is correct |
38 |
Correct |
41 ms |
10220 KB |
Output is correct |
39 |
Correct |
46 ms |
10164 KB |
Output is correct |
40 |
Correct |
41 ms |
10264 KB |
Output is correct |
41 |
Correct |
46 ms |
10396 KB |
Output is correct |
42 |
Correct |
951 ms |
3700 KB |
Output is correct |
43 |
Correct |
906 ms |
3696 KB |
Output is correct |
44 |
Correct |
756 ms |
3380 KB |
Output is correct |
45 |
Correct |
235 ms |
22640 KB |
Output is correct |
46 |
Correct |
210 ms |
22640 KB |
Output is correct |
47 |
Correct |
403 ms |
35904 KB |
Output is correct |
48 |
Correct |
56 ms |
13628 KB |
Output is correct |
49 |
Correct |
35 ms |
10692 KB |
Output is correct |
50 |
Correct |
31 ms |
10744 KB |
Output is correct |
51 |
Correct |
104 ms |
17708 KB |
Output is correct |
52 |
Correct |
129 ms |
18884 KB |
Output is correct |
53 |
Correct |
27 ms |
9804 KB |
Output is correct |
54 |
Correct |
7 ms |
6900 KB |
Output is correct |
55 |
Correct |
10 ms |
7792 KB |
Output is correct |
56 |
Execution timed out |
1083 ms |
7412 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |