This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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=10000000;
int n,m;
vector<int> v[N];
int P[N], x[N];
int dist[N][B];
bool done[N][B];
vector<pii> todo[MX];
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |