#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 |
1 |
Correct |
6 ms |
8012 KB |
Output is correct |
2 |
Correct |
6 ms |
8012 KB |
Output is correct |
3 |
Correct |
6 ms |
7992 KB |
Output is correct |
4 |
Correct |
6 ms |
8012 KB |
Output is correct |
5 |
Correct |
6 ms |
8012 KB |
Output is correct |
6 |
Correct |
7 ms |
8012 KB |
Output is correct |
7 |
Correct |
6 ms |
8032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
8080 KB |
Output is correct |
2 |
Correct |
6 ms |
8012 KB |
Output is correct |
3 |
Correct |
6 ms |
8012 KB |
Output is correct |
4 |
Correct |
7 ms |
8080 KB |
Output is correct |
5 |
Correct |
6 ms |
8068 KB |
Output is correct |
6 |
Correct |
6 ms |
8080 KB |
Output is correct |
7 |
Correct |
6 ms |
8012 KB |
Output is correct |
8 |
Correct |
6 ms |
7984 KB |
Output is correct |
9 |
Correct |
6 ms |
8084 KB |
Output is correct |
10 |
Correct |
6 ms |
8140 KB |
Output is correct |
11 |
Correct |
9 ms |
8344 KB |
Output is correct |
12 |
Correct |
6 ms |
8088 KB |
Output is correct |
13 |
Correct |
7 ms |
8012 KB |
Output is correct |
14 |
Correct |
9 ms |
8396 KB |
Output is correct |
15 |
Correct |
8 ms |
8396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8140 KB |
Output is correct |
2 |
Correct |
6 ms |
8080 KB |
Output is correct |
3 |
Correct |
6 ms |
8012 KB |
Output is correct |
4 |
Correct |
7 ms |
8012 KB |
Output is correct |
5 |
Correct |
8 ms |
7984 KB |
Output is correct |
6 |
Correct |
6 ms |
8012 KB |
Output is correct |
7 |
Correct |
6 ms |
8012 KB |
Output is correct |
8 |
Correct |
7 ms |
8012 KB |
Output is correct |
9 |
Correct |
6 ms |
8048 KB |
Output is correct |
10 |
Correct |
7 ms |
8080 KB |
Output is correct |
11 |
Correct |
8 ms |
8396 KB |
Output is correct |
12 |
Correct |
6 ms |
8012 KB |
Output is correct |
13 |
Correct |
7 ms |
8052 KB |
Output is correct |
14 |
Correct |
9 ms |
8396 KB |
Output is correct |
15 |
Correct |
10 ms |
8396 KB |
Output is correct |
16 |
Correct |
7 ms |
8268 KB |
Output is correct |
17 |
Correct |
11 ms |
8780 KB |
Output is correct |
18 |
Correct |
7 ms |
8268 KB |
Output is correct |
19 |
Correct |
7 ms |
8200 KB |
Output is correct |
20 |
Correct |
6 ms |
8140 KB |
Output is correct |
21 |
Correct |
7 ms |
8212 KB |
Output is correct |
22 |
Correct |
7 ms |
8268 KB |
Output is correct |
23 |
Correct |
9 ms |
8352 KB |
Output is correct |
24 |
Correct |
11 ms |
8604 KB |
Output is correct |
25 |
Correct |
12 ms |
8524 KB |
Output is correct |
26 |
Correct |
8 ms |
8268 KB |
Output is correct |
27 |
Correct |
8 ms |
8268 KB |
Output is correct |
28 |
Correct |
11 ms |
8964 KB |
Output is correct |
29 |
Correct |
19 ms |
10492 KB |
Output is correct |
30 |
Correct |
10 ms |
8780 KB |
Output is correct |
31 |
Correct |
15 ms |
9720 KB |
Output is correct |
32 |
Correct |
11 ms |
8804 KB |
Output is correct |
33 |
Correct |
33 ms |
11972 KB |
Output is correct |
34 |
Correct |
32 ms |
11980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8012 KB |
Output is correct |
2 |
Correct |
6 ms |
8012 KB |
Output is correct |
3 |
Correct |
6 ms |
8012 KB |
Output is correct |
4 |
Correct |
6 ms |
8012 KB |
Output is correct |
5 |
Correct |
6 ms |
8012 KB |
Output is correct |
6 |
Correct |
6 ms |
8080 KB |
Output is correct |
7 |
Correct |
6 ms |
8012 KB |
Output is correct |
8 |
Correct |
7 ms |
8012 KB |
Output is correct |
9 |
Correct |
6 ms |
8012 KB |
Output is correct |
10 |
Correct |
7 ms |
8080 KB |
Output is correct |
11 |
Correct |
8 ms |
8368 KB |
Output is correct |
12 |
Correct |
7 ms |
8012 KB |
Output is correct |
13 |
Correct |
6 ms |
8096 KB |
Output is correct |
14 |
Correct |
9 ms |
8400 KB |
Output is correct |
15 |
Correct |
9 ms |
8396 KB |
Output is correct |
16 |
Correct |
7 ms |
8296 KB |
Output is correct |
17 |
Correct |
11 ms |
8732 KB |
Output is correct |
18 |
Correct |
8 ms |
8268 KB |
Output is correct |
19 |
Correct |
7 ms |
8232 KB |
Output is correct |
20 |
Correct |
7 ms |
8140 KB |
Output is correct |
21 |
Correct |
7 ms |
8140 KB |
Output is correct |
22 |
Correct |
7 ms |
8316 KB |
Output is correct |
23 |
Correct |
8 ms |
8268 KB |
Output is correct |
24 |
Correct |
11 ms |
8604 KB |
Output is correct |
25 |
Correct |
9 ms |
8476 KB |
Output is correct |
26 |
Correct |
8 ms |
8268 KB |
Output is correct |
27 |
Correct |
9 ms |
8268 KB |
Output is correct |
28 |
Correct |
11 ms |
8992 KB |
Output is correct |
29 |
Correct |
18 ms |
10496 KB |
Output is correct |
30 |
Correct |
10 ms |
8780 KB |
Output is correct |
31 |
Correct |
14 ms |
9676 KB |
Output is correct |
32 |
Correct |
11 ms |
8924 KB |
Output is correct |
33 |
Correct |
31 ms |
11976 KB |
Output is correct |
34 |
Correct |
31 ms |
11980 KB |
Output is correct |
35 |
Correct |
52 ms |
14664 KB |
Output is correct |
36 |
Correct |
13 ms |
9164 KB |
Output is correct |
37 |
Correct |
71 ms |
16560 KB |
Output is correct |
38 |
Correct |
76 ms |
17476 KB |
Output is correct |
39 |
Correct |
85 ms |
17524 KB |
Output is correct |
40 |
Correct |
78 ms |
17552 KB |
Output is correct |
41 |
Correct |
76 ms |
17488 KB |
Output is correct |
42 |
Correct |
11 ms |
8864 KB |
Output is correct |
43 |
Correct |
11 ms |
8780 KB |
Output is correct |
44 |
Correct |
10 ms |
8800 KB |
Output is correct |
45 |
Correct |
132 ms |
24472 KB |
Output is correct |
46 |
Correct |
134 ms |
24456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8012 KB |
Output is correct |
2 |
Correct |
6 ms |
8012 KB |
Output is correct |
3 |
Correct |
6 ms |
8084 KB |
Output is correct |
4 |
Correct |
6 ms |
8012 KB |
Output is correct |
5 |
Correct |
6 ms |
8012 KB |
Output is correct |
6 |
Correct |
7 ms |
8068 KB |
Output is correct |
7 |
Correct |
9 ms |
8052 KB |
Output is correct |
8 |
Correct |
6 ms |
8084 KB |
Output is correct |
9 |
Correct |
6 ms |
8012 KB |
Output is correct |
10 |
Correct |
6 ms |
8140 KB |
Output is correct |
11 |
Correct |
9 ms |
8300 KB |
Output is correct |
12 |
Correct |
6 ms |
8012 KB |
Output is correct |
13 |
Correct |
6 ms |
8084 KB |
Output is correct |
14 |
Correct |
9 ms |
8396 KB |
Output is correct |
15 |
Correct |
9 ms |
8440 KB |
Output is correct |
16 |
Correct |
7 ms |
8268 KB |
Output is correct |
17 |
Correct |
11 ms |
8732 KB |
Output is correct |
18 |
Correct |
8 ms |
8268 KB |
Output is correct |
19 |
Correct |
7 ms |
8268 KB |
Output is correct |
20 |
Correct |
6 ms |
8220 KB |
Output is correct |
21 |
Correct |
7 ms |
8140 KB |
Output is correct |
22 |
Correct |
7 ms |
8340 KB |
Output is correct |
23 |
Correct |
9 ms |
8268 KB |
Output is correct |
24 |
Correct |
11 ms |
8652 KB |
Output is correct |
25 |
Correct |
9 ms |
8524 KB |
Output is correct |
26 |
Correct |
8 ms |
8356 KB |
Output is correct |
27 |
Correct |
8 ms |
8268 KB |
Output is correct |
28 |
Correct |
11 ms |
8996 KB |
Output is correct |
29 |
Correct |
19 ms |
10388 KB |
Output is correct |
30 |
Correct |
10 ms |
8768 KB |
Output is correct |
31 |
Correct |
14 ms |
9624 KB |
Output is correct |
32 |
Correct |
11 ms |
8912 KB |
Output is correct |
33 |
Correct |
32 ms |
11904 KB |
Output is correct |
34 |
Correct |
32 ms |
11988 KB |
Output is correct |
35 |
Correct |
52 ms |
14716 KB |
Output is correct |
36 |
Correct |
16 ms |
9168 KB |
Output is correct |
37 |
Correct |
78 ms |
16548 KB |
Output is correct |
38 |
Correct |
76 ms |
17476 KB |
Output is correct |
39 |
Correct |
78 ms |
17480 KB |
Output is correct |
40 |
Correct |
76 ms |
17592 KB |
Output is correct |
41 |
Correct |
80 ms |
17440 KB |
Output is correct |
42 |
Correct |
12 ms |
8912 KB |
Output is correct |
43 |
Correct |
12 ms |
8780 KB |
Output is correct |
44 |
Correct |
10 ms |
8780 KB |
Output is correct |
45 |
Correct |
144 ms |
24536 KB |
Output is correct |
46 |
Correct |
135 ms |
24456 KB |
Output is correct |
47 |
Correct |
308 ms |
37824 KB |
Output is correct |
48 |
Correct |
53 ms |
17860 KB |
Output is correct |
49 |
Correct |
47 ms |
14584 KB |
Output is correct |
50 |
Correct |
47 ms |
13996 KB |
Output is correct |
51 |
Correct |
150 ms |
21448 KB |
Output is correct |
52 |
Correct |
164 ms |
22244 KB |
Output is correct |
53 |
Correct |
46 ms |
13248 KB |
Output is correct |
54 |
Correct |
9 ms |
9036 KB |
Output is correct |
55 |
Correct |
11 ms |
9036 KB |
Output is correct |
56 |
Correct |
16 ms |
10316 KB |
Output is correct |
57 |
Correct |
129 ms |
19268 KB |
Output is correct |
58 |
Correct |
22 ms |
10628 KB |
Output is correct |
59 |
Correct |
31 ms |
11580 KB |
Output is correct |
60 |
Correct |
43 ms |
12628 KB |
Output is correct |
61 |
Correct |
38 ms |
12356 KB |
Output is correct |
62 |
Correct |
155 ms |
29064 KB |
Output is correct |
63 |
Correct |
929 ms |
113032 KB |
Output is correct |
64 |
Execution timed out |
1041 ms |
115536 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |