#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
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];
gp_hash_table<int, int> jumps[N];
void solve() {
int n, m;
cin >> n >> m;
vector<int> cnt(n, INF);
queue<ii> q;
vector<ii> a(m);
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;
a[i] = {p, b};
}
sort(a.begin(), a.end());
for(auto [p, b] : a) {
if(!P[b].empty() && P[b].back() == p) continue;
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 |
6 ms |
7372 KB |
Output is correct |
2 |
Correct |
5 ms |
7372 KB |
Output is correct |
3 |
Correct |
6 ms |
7372 KB |
Output is correct |
4 |
Correct |
9 ms |
7372 KB |
Output is correct |
5 |
Correct |
9 ms |
7268 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
9 ms |
7348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7372 KB |
Output is correct |
2 |
Correct |
6 ms |
7372 KB |
Output is correct |
3 |
Correct |
6 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7372 KB |
Output is correct |
5 |
Correct |
7 ms |
7372 KB |
Output is correct |
6 |
Correct |
6 ms |
7272 KB |
Output is correct |
7 |
Correct |
6 ms |
7372 KB |
Output is correct |
8 |
Correct |
5 ms |
7372 KB |
Output is correct |
9 |
Correct |
7 ms |
7388 KB |
Output is correct |
10 |
Correct |
5 ms |
7372 KB |
Output is correct |
11 |
Correct |
6 ms |
7584 KB |
Output is correct |
12 |
Correct |
10 ms |
7372 KB |
Output is correct |
13 |
Correct |
6 ms |
7372 KB |
Output is correct |
14 |
Correct |
7 ms |
7500 KB |
Output is correct |
15 |
Correct |
6 ms |
7500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7372 KB |
Output is correct |
2 |
Correct |
6 ms |
7372 KB |
Output is correct |
3 |
Correct |
6 ms |
7372 KB |
Output is correct |
4 |
Correct |
6 ms |
7372 KB |
Output is correct |
5 |
Correct |
6 ms |
7372 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
6 ms |
7372 KB |
Output is correct |
8 |
Correct |
6 ms |
7372 KB |
Output is correct |
9 |
Correct |
8 ms |
7372 KB |
Output is correct |
10 |
Correct |
6 ms |
7372 KB |
Output is correct |
11 |
Correct |
7 ms |
7500 KB |
Output is correct |
12 |
Correct |
7 ms |
7372 KB |
Output is correct |
13 |
Correct |
10 ms |
7336 KB |
Output is correct |
14 |
Correct |
7 ms |
7500 KB |
Output is correct |
15 |
Correct |
7 ms |
7500 KB |
Output is correct |
16 |
Correct |
7 ms |
7500 KB |
Output is correct |
17 |
Correct |
9 ms |
7884 KB |
Output is correct |
18 |
Correct |
7 ms |
7500 KB |
Output is correct |
19 |
Correct |
7 ms |
7500 KB |
Output is correct |
20 |
Correct |
41 ms |
7412 KB |
Output is correct |
21 |
Correct |
7 ms |
7500 KB |
Output is correct |
22 |
Correct |
7 ms |
7512 KB |
Output is correct |
23 |
Correct |
9 ms |
7564 KB |
Output is correct |
24 |
Correct |
9 ms |
7776 KB |
Output is correct |
25 |
Correct |
7 ms |
7632 KB |
Output is correct |
26 |
Correct |
7 ms |
7500 KB |
Output is correct |
27 |
Correct |
7 ms |
7500 KB |
Output is correct |
28 |
Correct |
11 ms |
8012 KB |
Output is correct |
29 |
Correct |
18 ms |
9616 KB |
Output is correct |
30 |
Correct |
9 ms |
7944 KB |
Output is correct |
31 |
Correct |
13 ms |
8832 KB |
Output is correct |
32 |
Correct |
9 ms |
8044 KB |
Output is correct |
33 |
Correct |
19 ms |
10924 KB |
Output is correct |
34 |
Correct |
17 ms |
10856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7372 KB |
Output is correct |
2 |
Correct |
6 ms |
7372 KB |
Output is correct |
3 |
Correct |
5 ms |
7372 KB |
Output is correct |
4 |
Correct |
6 ms |
7256 KB |
Output is correct |
5 |
Correct |
6 ms |
7244 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
10 ms |
7372 KB |
Output is correct |
8 |
Correct |
6 ms |
7260 KB |
Output is correct |
9 |
Correct |
7 ms |
7316 KB |
Output is correct |
10 |
Correct |
10 ms |
7464 KB |
Output is correct |
11 |
Correct |
8 ms |
7500 KB |
Output is correct |
12 |
Correct |
9 ms |
7372 KB |
Output is correct |
13 |
Correct |
6 ms |
7372 KB |
Output is correct |
14 |
Correct |
7 ms |
7600 KB |
Output is correct |
15 |
Correct |
7 ms |
7592 KB |
Output is correct |
16 |
Correct |
7 ms |
7500 KB |
Output is correct |
17 |
Correct |
8 ms |
7956 KB |
Output is correct |
18 |
Correct |
10 ms |
7500 KB |
Output is correct |
19 |
Correct |
10 ms |
7500 KB |
Output is correct |
20 |
Correct |
47 ms |
7428 KB |
Output is correct |
21 |
Correct |
9 ms |
7504 KB |
Output is correct |
22 |
Correct |
6 ms |
7500 KB |
Output is correct |
23 |
Correct |
7 ms |
7512 KB |
Output is correct |
24 |
Correct |
8 ms |
7800 KB |
Output is correct |
25 |
Correct |
7 ms |
7500 KB |
Output is correct |
26 |
Correct |
9 ms |
7500 KB |
Output is correct |
27 |
Correct |
9 ms |
7500 KB |
Output is correct |
28 |
Correct |
10 ms |
8044 KB |
Output is correct |
29 |
Correct |
12 ms |
9676 KB |
Output is correct |
30 |
Correct |
8 ms |
8012 KB |
Output is correct |
31 |
Correct |
10 ms |
8748 KB |
Output is correct |
32 |
Correct |
8 ms |
8012 KB |
Output is correct |
33 |
Correct |
17 ms |
10828 KB |
Output is correct |
34 |
Correct |
15 ms |
10828 KB |
Output is correct |
35 |
Correct |
23 ms |
12084 KB |
Output is correct |
36 |
Correct |
9 ms |
8020 KB |
Output is correct |
37 |
Correct |
26 ms |
13980 KB |
Output is correct |
38 |
Correct |
29 ms |
14120 KB |
Output is correct |
39 |
Correct |
31 ms |
14156 KB |
Output is correct |
40 |
Correct |
28 ms |
14168 KB |
Output is correct |
41 |
Correct |
30 ms |
14220 KB |
Output is correct |
42 |
Correct |
10 ms |
7804 KB |
Output is correct |
43 |
Correct |
10 ms |
7704 KB |
Output is correct |
44 |
Correct |
44 ms |
7772 KB |
Output is correct |
45 |
Correct |
56 ms |
20796 KB |
Output is correct |
46 |
Correct |
44 ms |
20736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7372 KB |
Output is correct |
2 |
Correct |
5 ms |
7372 KB |
Output is correct |
3 |
Correct |
6 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7372 KB |
Output is correct |
5 |
Correct |
5 ms |
7372 KB |
Output is correct |
6 |
Correct |
6 ms |
7372 KB |
Output is correct |
7 |
Correct |
6 ms |
7372 KB |
Output is correct |
8 |
Correct |
5 ms |
7372 KB |
Output is correct |
9 |
Correct |
6 ms |
7372 KB |
Output is correct |
10 |
Correct |
6 ms |
7408 KB |
Output is correct |
11 |
Correct |
11 ms |
7500 KB |
Output is correct |
12 |
Correct |
6 ms |
7372 KB |
Output is correct |
13 |
Correct |
6 ms |
7372 KB |
Output is correct |
14 |
Correct |
7 ms |
7500 KB |
Output is correct |
15 |
Correct |
7 ms |
7596 KB |
Output is correct |
16 |
Correct |
6 ms |
7500 KB |
Output is correct |
17 |
Correct |
8 ms |
7872 KB |
Output is correct |
18 |
Correct |
7 ms |
7452 KB |
Output is correct |
19 |
Correct |
7 ms |
7408 KB |
Output is correct |
20 |
Correct |
39 ms |
7372 KB |
Output is correct |
21 |
Correct |
6 ms |
7500 KB |
Output is correct |
22 |
Correct |
6 ms |
7500 KB |
Output is correct |
23 |
Correct |
7 ms |
7500 KB |
Output is correct |
24 |
Correct |
8 ms |
7796 KB |
Output is correct |
25 |
Correct |
7 ms |
7628 KB |
Output is correct |
26 |
Correct |
7 ms |
7500 KB |
Output is correct |
27 |
Correct |
7 ms |
7548 KB |
Output is correct |
28 |
Correct |
10 ms |
8108 KB |
Output is correct |
29 |
Correct |
12 ms |
9676 KB |
Output is correct |
30 |
Correct |
8 ms |
7964 KB |
Output is correct |
31 |
Correct |
11 ms |
8780 KB |
Output is correct |
32 |
Correct |
8 ms |
7920 KB |
Output is correct |
33 |
Correct |
17 ms |
10828 KB |
Output is correct |
34 |
Correct |
14 ms |
10904 KB |
Output is correct |
35 |
Correct |
23 ms |
12056 KB |
Output is correct |
36 |
Correct |
9 ms |
8072 KB |
Output is correct |
37 |
Correct |
26 ms |
13980 KB |
Output is correct |
38 |
Correct |
29 ms |
14156 KB |
Output is correct |
39 |
Correct |
28 ms |
14156 KB |
Output is correct |
40 |
Correct |
29 ms |
14092 KB |
Output is correct |
41 |
Correct |
30 ms |
14164 KB |
Output is correct |
42 |
Correct |
10 ms |
7800 KB |
Output is correct |
43 |
Correct |
10 ms |
7744 KB |
Output is correct |
44 |
Correct |
43 ms |
7648 KB |
Output is correct |
45 |
Correct |
56 ms |
20688 KB |
Output is correct |
46 |
Correct |
45 ms |
20704 KB |
Output is correct |
47 |
Correct |
100 ms |
34456 KB |
Output is correct |
48 |
Correct |
43 ms |
14792 KB |
Output is correct |
49 |
Correct |
32 ms |
11540 KB |
Output is correct |
50 |
Correct |
29 ms |
11264 KB |
Output is correct |
51 |
Correct |
72 ms |
17464 KB |
Output is correct |
52 |
Correct |
76 ms |
18364 KB |
Output is correct |
53 |
Correct |
24 ms |
9532 KB |
Output is correct |
54 |
Correct |
7 ms |
7500 KB |
Output is correct |
55 |
Correct |
12 ms |
7500 KB |
Output is correct |
56 |
Execution timed out |
1087 ms |
8028 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |