#include "bits/stdc++.h"
using namespace std;
#define double long double
const int MAXN = 10400000;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
vector<int>adj[MAXN];
void solve(int tc) {
int n, m;
cin >> n >> m;
int s = sqrt(n);
int b[m], p[m];
for(int i=0; i<m; i++) cin >> b[i] >> p[i];
int nxt = (s+1) * n;
for(int i=1; i<=s; i++) {
for(int j=i*n; j<(i+1)*n; j++) {
if(j+i < (i+1)*n) adj[j].push_back((j+i) * 2 + 1);
if(j-i >= i*n) adj[j].push_back((j-i) * 2 + 1);
adj[j].push_back((j%n) * 2 + 0);
}
}
for(int i=0; i<m; i++) {
if(p[i] <= s) adj[b[i]].push_back((b[i]+p[i]*n) * 2 + 0);
else {
for(int j=b[i]%p[i]; j<n; j+=p[i]) {
if(j+p[i] < n) adj[nxt + j/p[i]].push_back((nxt + j/p[i] + 1) * 2 + 1);
if(j-p[i] >= 0) adj[nxt + j/p[i]].push_back((nxt + j/p[i] - 1) * 2 + 1);
adj[nxt + j/p[i]].push_back(j * 2 + 0);
}
adj[b[i]].push_back((nxt + b[i]/p[i]) * 2 + 0);
for(int j=b[i]%p[i]; j<n; j+=p[i]) {
nxt++;
}
}
}
int k = nxt;
int dist[k];
bool vis[k];
for(int i=0; i<k; i++) dist[i] = 1e9, vis[i] = 0;
dist[b[0]] = 0;
int dq[k];
int head = 0, tail = 1;
dq[0] = b[0];
while(head != tail) {
int f = dq[head++];
if(!vis[f]) {
vis[f] = 1;
for(int x: adj[f]) {
if(!vis[x / 2] && dist[x / 2] > dist[f] + (x % 2)) {
dist[x / 2] = dist[f] + (x % 2);
if(x % 2 == 0) {
head--;
if(head<0)head+=k;
dq[head] = x/2;
}
else dq[tail++] = x/2;
}
}
}
}
cout<<(dist[b[1]]==1e9?-1:dist[b[1]])<<"\n";
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
// 勿忘初衷
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
244532 KB |
Output is correct |
2 |
Correct |
108 ms |
244412 KB |
Output is correct |
3 |
Runtime error |
327 ms |
262144 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
244444 KB |
Output is correct |
2 |
Correct |
115 ms |
244432 KB |
Output is correct |
3 |
Runtime error |
323 ms |
262144 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
244472 KB |
Output is correct |
2 |
Correct |
110 ms |
244520 KB |
Output is correct |
3 |
Runtime error |
324 ms |
262144 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
244464 KB |
Output is correct |
2 |
Correct |
109 ms |
244400 KB |
Output is correct |
3 |
Runtime error |
334 ms |
262144 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
244508 KB |
Output is correct |
2 |
Correct |
111 ms |
244524 KB |
Output is correct |
3 |
Runtime error |
333 ms |
262144 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |