#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=5000000;
short n,m;
vector<short> v[N];
short P[N], x[N];
int dist[N][B];
bool done[N][B];
vector<pair<short, short>> 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 |
Correct |
54 ms |
118348 KB |
Output is correct |
2 |
Correct |
54 ms |
118400 KB |
Output is correct |
3 |
Correct |
57 ms |
118348 KB |
Output is correct |
4 |
Correct |
67 ms |
118448 KB |
Output is correct |
5 |
Correct |
56 ms |
118340 KB |
Output is correct |
6 |
Correct |
60 ms |
118560 KB |
Output is correct |
7 |
Correct |
56 ms |
118400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
118444 KB |
Output is correct |
2 |
Correct |
68 ms |
118436 KB |
Output is correct |
3 |
Correct |
69 ms |
118456 KB |
Output is correct |
4 |
Correct |
63 ms |
118348 KB |
Output is correct |
5 |
Correct |
60 ms |
118388 KB |
Output is correct |
6 |
Correct |
57 ms |
118452 KB |
Output is correct |
7 |
Correct |
59 ms |
118396 KB |
Output is correct |
8 |
Correct |
56 ms |
118472 KB |
Output is correct |
9 |
Correct |
55 ms |
118488 KB |
Output is correct |
10 |
Correct |
56 ms |
118420 KB |
Output is correct |
11 |
Correct |
58 ms |
118464 KB |
Output is correct |
12 |
Correct |
60 ms |
118496 KB |
Output is correct |
13 |
Correct |
62 ms |
118452 KB |
Output is correct |
14 |
Correct |
57 ms |
118540 KB |
Output is correct |
15 |
Correct |
59 ms |
118568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
118428 KB |
Output is correct |
2 |
Correct |
60 ms |
118440 KB |
Output is correct |
3 |
Correct |
59 ms |
118364 KB |
Output is correct |
4 |
Correct |
68 ms |
118396 KB |
Output is correct |
5 |
Correct |
56 ms |
118456 KB |
Output is correct |
6 |
Correct |
56 ms |
118448 KB |
Output is correct |
7 |
Correct |
58 ms |
118380 KB |
Output is correct |
8 |
Correct |
56 ms |
118476 KB |
Output is correct |
9 |
Correct |
61 ms |
118476 KB |
Output is correct |
10 |
Correct |
62 ms |
118452 KB |
Output is correct |
11 |
Correct |
59 ms |
118556 KB |
Output is correct |
12 |
Correct |
54 ms |
118532 KB |
Output is correct |
13 |
Correct |
66 ms |
118500 KB |
Output is correct |
14 |
Correct |
59 ms |
118484 KB |
Output is correct |
15 |
Correct |
58 ms |
118548 KB |
Output is correct |
16 |
Correct |
62 ms |
118488 KB |
Output is correct |
17 |
Correct |
57 ms |
119124 KB |
Output is correct |
18 |
Correct |
66 ms |
119624 KB |
Output is correct |
19 |
Correct |
67 ms |
119796 KB |
Output is correct |
20 |
Correct |
58 ms |
120252 KB |
Output is correct |
21 |
Correct |
63 ms |
118696 KB |
Output is correct |
22 |
Correct |
65 ms |
119704 KB |
Output is correct |
23 |
Correct |
60 ms |
119756 KB |
Output is correct |
24 |
Correct |
59 ms |
120152 KB |
Output is correct |
25 |
Correct |
64 ms |
120096 KB |
Output is correct |
26 |
Correct |
57 ms |
120248 KB |
Output is correct |
27 |
Correct |
57 ms |
120272 KB |
Output is correct |
28 |
Correct |
63 ms |
120652 KB |
Output is correct |
29 |
Correct |
59 ms |
120552 KB |
Output is correct |
30 |
Correct |
65 ms |
120220 KB |
Output is correct |
31 |
Correct |
59 ms |
120328 KB |
Output is correct |
32 |
Correct |
58 ms |
120284 KB |
Output is correct |
33 |
Correct |
59 ms |
120780 KB |
Output is correct |
34 |
Correct |
57 ms |
120468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
118448 KB |
Output is correct |
2 |
Correct |
70 ms |
118400 KB |
Output is correct |
3 |
Correct |
58 ms |
118356 KB |
Output is correct |
4 |
Correct |
65 ms |
118440 KB |
Output is correct |
5 |
Correct |
57 ms |
118340 KB |
Output is correct |
6 |
Correct |
65 ms |
118460 KB |
Output is correct |
7 |
Correct |
68 ms |
118464 KB |
Output is correct |
8 |
Correct |
58 ms |
118476 KB |
Output is correct |
9 |
Correct |
55 ms |
118412 KB |
Output is correct |
10 |
Correct |
55 ms |
118544 KB |
Output is correct |
11 |
Correct |
58 ms |
118552 KB |
Output is correct |
12 |
Correct |
58 ms |
118548 KB |
Output is correct |
13 |
Correct |
70 ms |
118476 KB |
Output is correct |
14 |
Correct |
57 ms |
118484 KB |
Output is correct |
15 |
Correct |
58 ms |
118444 KB |
Output is correct |
16 |
Correct |
64 ms |
118552 KB |
Output is correct |
17 |
Correct |
57 ms |
119120 KB |
Output is correct |
18 |
Correct |
76 ms |
119564 KB |
Output is correct |
19 |
Correct |
73 ms |
119788 KB |
Output is correct |
20 |
Correct |
70 ms |
120268 KB |
Output is correct |
21 |
Correct |
65 ms |
118656 KB |
Output is correct |
22 |
Correct |
66 ms |
119676 KB |
Output is correct |
23 |
Correct |
57 ms |
119752 KB |
Output is correct |
24 |
Correct |
57 ms |
120080 KB |
Output is correct |
25 |
Correct |
59 ms |
120200 KB |
Output is correct |
26 |
Correct |
58 ms |
120336 KB |
Output is correct |
27 |
Correct |
66 ms |
120204 KB |
Output is correct |
28 |
Correct |
59 ms |
120652 KB |
Output is correct |
29 |
Correct |
69 ms |
120488 KB |
Output is correct |
30 |
Correct |
62 ms |
120228 KB |
Output is correct |
31 |
Correct |
59 ms |
120404 KB |
Output is correct |
32 |
Correct |
59 ms |
120300 KB |
Output is correct |
33 |
Correct |
67 ms |
120956 KB |
Output is correct |
34 |
Correct |
60 ms |
120548 KB |
Output is correct |
35 |
Correct |
63 ms |
119884 KB |
Output is correct |
36 |
Correct |
59 ms |
119372 KB |
Output is correct |
37 |
Correct |
61 ms |
120256 KB |
Output is correct |
38 |
Correct |
61 ms |
120452 KB |
Output is correct |
39 |
Correct |
62 ms |
120148 KB |
Output is correct |
40 |
Correct |
64 ms |
120252 KB |
Output is correct |
41 |
Correct |
66 ms |
120420 KB |
Output is correct |
42 |
Correct |
63 ms |
120416 KB |
Output is correct |
43 |
Correct |
62 ms |
120324 KB |
Output is correct |
44 |
Correct |
65 ms |
120512 KB |
Output is correct |
45 |
Correct |
75 ms |
121932 KB |
Output is correct |
46 |
Correct |
67 ms |
121064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
118324 KB |
Output is correct |
2 |
Correct |
62 ms |
118384 KB |
Output is correct |
3 |
Correct |
56 ms |
118456 KB |
Output is correct |
4 |
Correct |
69 ms |
118580 KB |
Output is correct |
5 |
Correct |
58 ms |
118424 KB |
Output is correct |
6 |
Correct |
55 ms |
118352 KB |
Output is correct |
7 |
Correct |
60 ms |
118452 KB |
Output is correct |
8 |
Correct |
59 ms |
118476 KB |
Output is correct |
9 |
Correct |
60 ms |
118580 KB |
Output is correct |
10 |
Correct |
58 ms |
118476 KB |
Output is correct |
11 |
Correct |
56 ms |
118536 KB |
Output is correct |
12 |
Correct |
56 ms |
118552 KB |
Output is correct |
13 |
Correct |
57 ms |
118532 KB |
Output is correct |
14 |
Correct |
59 ms |
118544 KB |
Output is correct |
15 |
Correct |
63 ms |
118568 KB |
Output is correct |
16 |
Correct |
69 ms |
118512 KB |
Output is correct |
17 |
Correct |
57 ms |
119064 KB |
Output is correct |
18 |
Correct |
67 ms |
119640 KB |
Output is correct |
19 |
Correct |
73 ms |
119892 KB |
Output is correct |
20 |
Correct |
66 ms |
120212 KB |
Output is correct |
21 |
Correct |
67 ms |
118732 KB |
Output is correct |
22 |
Correct |
66 ms |
119700 KB |
Output is correct |
23 |
Correct |
57 ms |
119840 KB |
Output is correct |
24 |
Correct |
67 ms |
120024 KB |
Output is correct |
25 |
Correct |
60 ms |
120088 KB |
Output is correct |
26 |
Correct |
59 ms |
120240 KB |
Output is correct |
27 |
Correct |
62 ms |
120280 KB |
Output is correct |
28 |
Correct |
60 ms |
120620 KB |
Output is correct |
29 |
Correct |
60 ms |
120548 KB |
Output is correct |
30 |
Correct |
60 ms |
120236 KB |
Output is correct |
31 |
Correct |
73 ms |
120340 KB |
Output is correct |
32 |
Correct |
62 ms |
120344 KB |
Output is correct |
33 |
Correct |
72 ms |
120892 KB |
Output is correct |
34 |
Correct |
63 ms |
120476 KB |
Output is correct |
35 |
Correct |
66 ms |
119936 KB |
Output is correct |
36 |
Correct |
61 ms |
119372 KB |
Output is correct |
37 |
Correct |
64 ms |
120336 KB |
Output is correct |
38 |
Correct |
65 ms |
120420 KB |
Output is correct |
39 |
Correct |
65 ms |
120084 KB |
Output is correct |
40 |
Correct |
65 ms |
120240 KB |
Output is correct |
41 |
Correct |
65 ms |
120500 KB |
Output is correct |
42 |
Correct |
62 ms |
120420 KB |
Output is correct |
43 |
Correct |
69 ms |
120436 KB |
Output is correct |
44 |
Correct |
69 ms |
120448 KB |
Output is correct |
45 |
Correct |
86 ms |
121872 KB |
Output is correct |
46 |
Correct |
69 ms |
121068 KB |
Output is correct |
47 |
Correct |
71 ms |
129560 KB |
Output is correct |
48 |
Correct |
79 ms |
134784 KB |
Output is correct |
49 |
Correct |
81 ms |
136272 KB |
Output is correct |
50 |
Correct |
78 ms |
137968 KB |
Output is correct |
51 |
Correct |
80 ms |
145024 KB |
Output is correct |
52 |
Correct |
79 ms |
145048 KB |
Output is correct |
53 |
Correct |
74 ms |
141260 KB |
Output is correct |
54 |
Correct |
98 ms |
144588 KB |
Output is correct |
55 |
Correct |
95 ms |
144904 KB |
Output is correct |
56 |
Correct |
87 ms |
146024 KB |
Output is correct |
57 |
Correct |
73 ms |
139248 KB |
Output is correct |
58 |
Correct |
88 ms |
146004 KB |
Output is correct |
59 |
Correct |
89 ms |
145968 KB |
Output is correct |
60 |
Correct |
87 ms |
145996 KB |
Output is correct |
61 |
Correct |
82 ms |
145744 KB |
Output is correct |
62 |
Correct |
130 ms |
153152 KB |
Output is correct |
63 |
Correct |
108 ms |
145704 KB |
Output is correct |
64 |
Correct |
111 ms |
145420 KB |
Output is correct |
65 |
Correct |
166 ms |
150984 KB |
Output is correct |
66 |
Correct |
434 ms |
168044 KB |
Output is correct |
67 |
Correct |
177 ms |
153024 KB |
Output is correct |