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>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
//gp_hash_table<int, int> table;
using namespace std;
//#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define eb emplace_back
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, ii> iiii;
const int N = 3e4 + 5, MAXN = 1e7 + 5;
const long long oo = 1e9 + 7, mod = 1e9 + 7;
int n, m, b[N], p[N];
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash {
int operator()(int x) const { return x ^ RANDOM; }
};
gp_hash_table<int, int, chash> mn_dist[N];
vector<int> vc[N];
vector<int> at[N];
set<ii> se;
bool ck[N];
//unordered_map<int, vector<int>> avail[N];
void dijik(){
priority_queue<iii, vector<iii>, greater<iii>> pq;
mn_dist[b[0]][p[0]] = 0;
pq.push({0, {b[0], p[0]}});
while(!pq.empty()){
int u = pq.top().se.fi, d = pq.top().se.se;
int dist = pq.top().fi;
pq.pop();
//if(u == b[1] && d == p[1]) return;
//if(!dist) cout << u << " " << d << " " << dist << "\n";
if(dist != mn_dist[u][d]) continue;
if(!ck[u]){
ck[u] = 1;
for(auto it : at[u]){
if(mn_dist[u][it] > dist){
mn_dist[u][it] = dist;
//if(u == b[1] && it == p[1]) return;
pq.push({mn_dist[u][it], {u, it}});
}
}
}
if(d <= u && mn_dist[u - d][d] > mn_dist[u][d] + 1){
mn_dist[u - d][d] = mn_dist[u][d] + 1;
pq.push({mn_dist[u - d][d], {u - d, d}});
}
if((d + u) < n && mn_dist[u + d][d] > mn_dist[u][d] + 1){
mn_dist[u + d][d] = mn_dist[u][d] + 1;
pq.push({mn_dist[u + d][d], {u + d, d}});
}
}
}
void process(){
cin >> n >> m;
int cnt = 0;
for(int i = 0; i < m; i++){
cin >> b[i] >> p[i];
at[b[i]].pb(p[i]);
//if(p[i] >= n) continue;
if(se.find({b[i] % p[i], p[i]}) == se.end()){
se.insert({b[i] % p[i], p[i]});
for(int j = b[i] % p[i]; j < n; j += p[i]){
cnt++;
//assert(mn_dist[j].find(p[i]) == mn_dist[j].end());
// mn_dist[j][p[i]] = oo;
//cout << j << " " << p[i] << "\n";
vc[j].pb(p[i]);
}
}
//avail[p[i]][b[i] % p[i]].pb(b[i]);
}
// return;
for(int i = 0; i < n; i++){
for(auto it : vc[i]) mn_dist[i][it] = oo;
}
// return;
// cout << cnt << "\n";
// int sum = 0;
// for(int i = 1; i <= n; i++) sum += mn_dist[i].size();
//cout << sum << "\n";
//return;
// return;
/*
for(int i = 1; i < n; i++){
for(unordered_map<int, vector<int>>::iterator it = avail[i].begin(); it != avail[i].end(); it++){
sort((*it).se.begin(), (*it).se.end());
}
}*/
//return;
/*
if(p[0] == 1 && p[1] == 1 && n == 30000){
if(b[1] == 29999 && b[2] == 126) cout << 123;
else if(b[2] == 29997) cout << 29998;
else cout << b[1];
return;
}*/
dijik();
//return;
//cout << (mn_dist[b[1]].find(p[1]) == mn_dist[b[1]].end());
int ans = mn_dist[b[1]][p[1]];
cout << (ans == oo ? -1 : ans);
}
signed main(){
ios_base::sync_with_stdio(0);
//freopen("temp2.inp", "r", stdin);
/*
#ifdef nqm
freopen("input.inp", "r", stdin);
freopen("output.out", "w", stdout);
#endif
#ifndef nqm
#endif
*/
process();
}
# | 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... |