#include "bits/stdc++.h"
using namespace std;
typedef pair<int,int> pii;
#define f first
#define s second
#define nl '\n'
#define pb push_back
#define sz(x) (int) x.size()
const int N=30001;
const int B=175;
const int MX=12000000;
int n,m;
vector<int> v[N];
int P[N], x[N];
int dist[N][B];
bool done[N][B];
vector<pii> todo[MX+5];
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for(int i=0; i<m; i++){
cin >> x[i] >> P[i];
v[x[i]].push_back(i);
}
for(int i=0; i<n; i++){
for(int j=0; j<B; j++) dist[i][j]=1e9;
}
dist[x[0]][0]=0;
set<pair<int, pii>> s;
auto go=[&](int i, int d, int b){
for(int c:{i+b, i-b}){
if(c<0 || c>=n) continue;
if(d+1<dist[c][b]){
dist[c][b]=d+1;
todo[d+1].pb({c, b});
}
if(d+1<dist[c][0]){
dist[c][0]=d+1;
todo[d+1].pb({c, 0});
}
}
};
todo[0].pb({x[0], 0});
int d=0;
while(true){
pii p;
while(true){
while(d<MX && !sz(todo[d])) d++;
if(d==MX) break;
p=todo[d].back();
todo[d].pop_back();
if(!done[p.f][p.s]) break;
}
if(d==MX) break;
int a=p.f, b=p.s;
done[a][b]=1;
// cout << a << " " << b << nl;
if(a==x[1]){
cout << d; return 0;
}
if(!b){
for(int i:v[a]){
if(P[i]>=B){
for(int j=a%P[i]; j<n; j+=P[i]){
if(d+abs(j-a)/P[i]<dist[j][0]){
dist[j][0]=d+abs(j-a)/P[i];
todo[dist[j][0]].pb({j, 0});
}
}
}
else{
go(a, d, P[i]);
}
}
}
else{
go(a, d, b);
}
}
cout << -1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
102 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
98 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
105 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
104 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
111 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |