This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#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;
deque<ii> pq;
mn_dist[b[0]][p[0]] = 0;
pq.push_front({b[0], p[0]});
while(!pq.empty()){
int u = pq.front().fi, d = pq.front().se;
int dist = mn_dist[u][d];
pq.pop_front();
//cout << u << " " << d << " " << mn_dist[u][d] << "\n";
if(!ck[u]){
ck[u] = 1;
for(auto it : at[u]){
if(mn_dist[u][it] > dist){
mn_dist[u][it] = dist;
pq.push_front({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_back({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_back({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(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++;
vc[j].pb(p[i]);
}
}
}
for(int i = 0; i < n; i++){
for(auto it : vc[i]) mn_dist[i][it] = oo;
}
dijik();
int ans = mn_dist[b[1]][p[1]];
cout << (ans == oo ? -1 : ans);
}
signed main(){
ios_base::sync_with_stdio(0);
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... |