#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, K = 174, 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);
}
for(int p : P[x0]) {
if(jumps[x0][p] == -1) {
jumps[x0][p] = 0;
q.emplace(x0, p);
}
}
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(jumps[x][k] == -1) {
jumps[x][k] = j;
q.emplace(x, 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 |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
1 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2664 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 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 |
2668 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
3 ms |
2636 KB |
Output is correct |
11 |
Correct |
3 ms |
2808 KB |
Output is correct |
12 |
Correct |
5 ms |
2636 KB |
Output is correct |
13 |
Correct |
4 ms |
2672 KB |
Output is correct |
14 |
Correct |
3 ms |
2892 KB |
Output is correct |
15 |
Correct |
2 ms |
2892 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 |
1 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 |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2672 KB |
Output is correct |
8 |
Correct |
1 ms |
2636 KB |
Output is correct |
9 |
Correct |
3 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2676 KB |
Output is correct |
11 |
Correct |
3 ms |
2804 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 |
2932 KB |
Output is correct |
15 |
Correct |
2 ms |
2892 KB |
Output is correct |
16 |
Correct |
2 ms |
2764 KB |
Output is correct |
17 |
Incorrect |
4 ms |
3320 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
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 |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
1 ms |
2672 KB |
Output is correct |
8 |
Correct |
1 ms |
2636 KB |
Output is correct |
9 |
Correct |
1 ms |
2664 KB |
Output is correct |
10 |
Correct |
2 ms |
2676 KB |
Output is correct |
11 |
Correct |
2 ms |
2764 KB |
Output is correct |
12 |
Correct |
5 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 |
16 |
Correct |
2 ms |
2764 KB |
Output is correct |
17 |
Incorrect |
4 ms |
3324 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
1 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 |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2668 KB |
Output is correct |
8 |
Correct |
1 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2672 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 |
2720 KB |
Output is correct |
13 |
Correct |
4 ms |
2676 KB |
Output is correct |
14 |
Correct |
3 ms |
2928 KB |
Output is correct |
15 |
Correct |
3 ms |
2892 KB |
Output is correct |
16 |
Correct |
2 ms |
2764 KB |
Output is correct |
17 |
Incorrect |
4 ms |
3276 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |