#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], jumps[N], h[N];
int get_pos(int x, int p) {
int k = lower_bound(h[x].begin(), h[x].end(), p) - h[x].begin();
return k;
}
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) {
if(h[x].empty() || h[x].back() != p) {
h[x].push_back(p);
jumps[x].push_back(-1);
}
}
P[b].push_back(p);
}
while(!P[x0].empty()) {
int k = P[x0].back();
P[x0].pop_back();
int i = get_pos(x0, k);
if(jumps[x0][i] == -1) {
jumps[x0][i] = 0;
q.emplace(x0, k);
}
}
while(!q.empty()) {
auto [x, p] = q.front();
q.pop();
int j = jumps[x][get_pos(x, p)];
cnt[x] = min(cnt[x], j);
if(x == x1) break;
if(x >= p) {
int k = get_pos(x - p, p);
if(jumps[x - p][k] == -1) {
jumps[x - p][k] = 1 + j;
q.emplace(x - p, p);
}
}
if(x + p < n) {
int k = get_pos(x + p, p);
if(jumps[x + p][k] == -1) {
jumps[x + p][k] = 1 + j;
q.emplace(x + p, p);
}
}
while(!P[x].empty()) {
int k = P[x].back();
P[x].pop_back();
if(x >= k) {
int i = get_pos(x - k, k);
if(jumps[x - k][i] == -1) {
jumps[x - k][i] = 1 + j;
q.emplace(x - k, k);
}
}
if(x + k < n) {
int i = get_pos(x + k, k);
if(jumps[x + k][i] == -1) {
jumps[x + k][i] = 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;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
2432 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 |
2 ms |
2508 KB |
Output is correct |
12 |
Correct |
2 ms |
2380 KB |
Output is correct |
13 |
Correct |
1 ms |
2380 KB |
Output is correct |
14 |
Correct |
3 ms |
2508 KB |
Output is correct |
15 |
Correct |
2 ms |
2508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2380 KB |
Output is correct |
2 |
Correct |
2 ms |
2508 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 |
1 ms |
2380 KB |
Output is correct |
10 |
Correct |
1 ms |
2380 KB |
Output is correct |
11 |
Correct |
2 ms |
2508 KB |
Output is correct |
12 |
Correct |
2 ms |
2380 KB |
Output is correct |
13 |
Correct |
1 ms |
2380 KB |
Output is correct |
14 |
Correct |
2 ms |
2508 KB |
Output is correct |
15 |
Correct |
2 ms |
2508 KB |
Output is correct |
16 |
Correct |
2 ms |
2508 KB |
Output is correct |
17 |
Correct |
3 ms |
2636 KB |
Output is correct |
18 |
Correct |
2 ms |
2508 KB |
Output is correct |
19 |
Correct |
2 ms |
2508 KB |
Output is correct |
20 |
Correct |
6 ms |
2616 KB |
Output is correct |
21 |
Correct |
2 ms |
2380 KB |
Output is correct |
22 |
Correct |
2 ms |
2508 KB |
Output is correct |
23 |
Correct |
2 ms |
2508 KB |
Output is correct |
24 |
Correct |
3 ms |
2636 KB |
Output is correct |
25 |
Correct |
2 ms |
2636 KB |
Output is correct |
26 |
Correct |
2 ms |
2508 KB |
Output is correct |
27 |
Correct |
2 ms |
2508 KB |
Output is correct |
28 |
Correct |
4 ms |
2764 KB |
Output is correct |
29 |
Correct |
7 ms |
3020 KB |
Output is correct |
30 |
Correct |
3 ms |
2764 KB |
Output is correct |
31 |
Correct |
5 ms |
3020 KB |
Output is correct |
32 |
Correct |
3 ms |
2764 KB |
Output is correct |
33 |
Correct |
16 ms |
3652 KB |
Output is correct |
34 |
Correct |
11 ms |
3532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 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 |
1 ms |
2380 KB |
Output is correct |
11 |
Correct |
2 ms |
2508 KB |
Output is correct |
12 |
Correct |
2 ms |
2380 KB |
Output is correct |
13 |
Correct |
2 ms |
2380 KB |
Output is correct |
14 |
Correct |
2 ms |
2508 KB |
Output is correct |
15 |
Correct |
2 ms |
2508 KB |
Output is correct |
16 |
Correct |
1 ms |
2508 KB |
Output is correct |
17 |
Correct |
3 ms |
2636 KB |
Output is correct |
18 |
Correct |
2 ms |
2508 KB |
Output is correct |
19 |
Correct |
2 ms |
2508 KB |
Output is correct |
20 |
Correct |
6 ms |
2636 KB |
Output is correct |
21 |
Correct |
1 ms |
2380 KB |
Output is correct |
22 |
Correct |
2 ms |
2508 KB |
Output is correct |
23 |
Correct |
2 ms |
2508 KB |
Output is correct |
24 |
Correct |
3 ms |
2636 KB |
Output is correct |
25 |
Correct |
2 ms |
2636 KB |
Output is correct |
26 |
Correct |
2 ms |
2636 KB |
Output is correct |
27 |
Correct |
2 ms |
2508 KB |
Output is correct |
28 |
Correct |
4 ms |
2764 KB |
Output is correct |
29 |
Correct |
8 ms |
2984 KB |
Output is correct |
30 |
Correct |
3 ms |
2764 KB |
Output is correct |
31 |
Correct |
5 ms |
3020 KB |
Output is correct |
32 |
Correct |
4 ms |
2764 KB |
Output is correct |
33 |
Correct |
23 ms |
3652 KB |
Output is correct |
34 |
Correct |
11 ms |
3648 KB |
Output is correct |
35 |
Correct |
18 ms |
4412 KB |
Output is correct |
36 |
Correct |
3 ms |
2792 KB |
Output is correct |
37 |
Correct |
13 ms |
5108 KB |
Output is correct |
38 |
Correct |
15 ms |
5196 KB |
Output is correct |
39 |
Correct |
14 ms |
5100 KB |
Output is correct |
40 |
Correct |
13 ms |
5084 KB |
Output is correct |
41 |
Correct |
18 ms |
5244 KB |
Output is correct |
42 |
Correct |
5 ms |
2852 KB |
Output is correct |
43 |
Correct |
5 ms |
2764 KB |
Output is correct |
44 |
Correct |
10 ms |
2816 KB |
Output is correct |
45 |
Correct |
86 ms |
7416 KB |
Output is correct |
46 |
Correct |
45 ms |
7548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
2380 KB |
Output is correct |
11 |
Correct |
2 ms |
2508 KB |
Output is correct |
12 |
Correct |
2 ms |
2380 KB |
Output is correct |
13 |
Correct |
1 ms |
2380 KB |
Output is correct |
14 |
Correct |
2 ms |
2508 KB |
Output is correct |
15 |
Correct |
2 ms |
2508 KB |
Output is correct |
16 |
Correct |
1 ms |
2508 KB |
Output is correct |
17 |
Correct |
3 ms |
2636 KB |
Output is correct |
18 |
Correct |
2 ms |
2508 KB |
Output is correct |
19 |
Correct |
2 ms |
2508 KB |
Output is correct |
20 |
Correct |
6 ms |
2596 KB |
Output is correct |
21 |
Correct |
2 ms |
2480 KB |
Output is correct |
22 |
Correct |
2 ms |
2508 KB |
Output is correct |
23 |
Correct |
2 ms |
2508 KB |
Output is correct |
24 |
Correct |
3 ms |
2636 KB |
Output is correct |
25 |
Correct |
2 ms |
2636 KB |
Output is correct |
26 |
Correct |
2 ms |
2636 KB |
Output is correct |
27 |
Correct |
2 ms |
2508 KB |
Output is correct |
28 |
Correct |
4 ms |
2868 KB |
Output is correct |
29 |
Correct |
7 ms |
3020 KB |
Output is correct |
30 |
Correct |
3 ms |
2764 KB |
Output is correct |
31 |
Correct |
4 ms |
3044 KB |
Output is correct |
32 |
Correct |
3 ms |
2764 KB |
Output is correct |
33 |
Correct |
16 ms |
3532 KB |
Output is correct |
34 |
Correct |
10 ms |
3532 KB |
Output is correct |
35 |
Correct |
15 ms |
4420 KB |
Output is correct |
36 |
Correct |
3 ms |
2764 KB |
Output is correct |
37 |
Correct |
13 ms |
5068 KB |
Output is correct |
38 |
Correct |
16 ms |
5116 KB |
Output is correct |
39 |
Correct |
14 ms |
5068 KB |
Output is correct |
40 |
Correct |
13 ms |
5068 KB |
Output is correct |
41 |
Correct |
16 ms |
5196 KB |
Output is correct |
42 |
Correct |
6 ms |
2816 KB |
Output is correct |
43 |
Correct |
6 ms |
2764 KB |
Output is correct |
44 |
Correct |
9 ms |
2764 KB |
Output is correct |
45 |
Correct |
74 ms |
7488 KB |
Output is correct |
46 |
Correct |
53 ms |
7504 KB |
Output is correct |
47 |
Correct |
44 ms |
12100 KB |
Output is correct |
48 |
Correct |
24 ms |
6664 KB |
Output is correct |
49 |
Correct |
19 ms |
5640 KB |
Output is correct |
50 |
Correct |
18 ms |
5676 KB |
Output is correct |
51 |
Correct |
44 ms |
8136 KB |
Output is correct |
52 |
Correct |
43 ms |
8352 KB |
Output is correct |
53 |
Correct |
17 ms |
5304 KB |
Output is correct |
54 |
Correct |
4 ms |
4300 KB |
Output is correct |
55 |
Correct |
6 ms |
4428 KB |
Output is correct |
56 |
Execution timed out |
1090 ms |
5460 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |