#include "bits/stdc++.h"
using namespace std;
typedef pair<int,int> pii;
#define f first
#define s second
#define nl '\n'
const int N=30001;
const int B=175;
int n,m;
vector<int> v[N];
int P[N], x[N];
int dist[N][B];
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[0][0]=0;
set<pair<int, pii>> s;
s.insert({0, {x[0], 0}});
auto go=[&](int i, int d, int b){
// cout << "go " << i << " " << d << " " << b << nl;
for(int c:{i+b, i-b}){
if(c<0 || c>=n) continue;
// cout << "-> " << c << " " << b << nl;
if(d+1<dist[c][b]){
if(dist[c][b]!=1e9) s.erase({dist[c][b], {c, b}});
dist[c][b]=d+1;
s.insert({d+1, {c, b}});
}
if(d+1<dist[c][0]){
if(dist[c][0]!=1e9) s.erase({dist[c][0], {c, 0}});
dist[c][0]=d+1;
s.insert({d+1, {c, 0}});
}
}
};
while(!s.empty()){
auto p=*s.begin(); s.erase(s.begin());
int d=p.f, a=p.s.f, b=p.s.s;
// 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]){
if(dist[j][0]!=1e9) s.erase({dist[j][0], {j, 0}});
dist[j][0]=d+abs(j-a)/P[i];
s.insert({dist[j][0], {j, 0}});
}
}
}
else{
go(a, d, P[i]);
}
}
}
else{
go(a, d, b);
}
}
cout << -1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1036 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1168 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
2 ms |
1152 KB |
Output is correct |
14 |
Correct |
3 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1036 KB |
Output is correct |
14 |
Correct |
4 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1620 KB |
Output is correct |
18 |
Correct |
2 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
2388 KB |
Output is correct |
20 |
Correct |
2 ms |
2516 KB |
Output is correct |
21 |
Correct |
2 ms |
1292 KB |
Output is correct |
22 |
Correct |
2 ms |
2260 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
3 ms |
2448 KB |
Output is correct |
25 |
Correct |
2 ms |
2388 KB |
Output is correct |
26 |
Correct |
2 ms |
2388 KB |
Output is correct |
27 |
Correct |
2 ms |
2388 KB |
Output is correct |
28 |
Correct |
4 ms |
2516 KB |
Output is correct |
29 |
Correct |
10 ms |
2444 KB |
Output is correct |
30 |
Correct |
4 ms |
2308 KB |
Output is correct |
31 |
Correct |
6 ms |
2388 KB |
Output is correct |
32 |
Correct |
4 ms |
2440 KB |
Output is correct |
33 |
Correct |
18 ms |
2704 KB |
Output is correct |
34 |
Correct |
14 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
2 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
1028 KB |
Output is correct |
9 |
Correct |
2 ms |
1032 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1300 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1684 KB |
Output is correct |
18 |
Correct |
2 ms |
2260 KB |
Output is correct |
19 |
Correct |
3 ms |
2516 KB |
Output is correct |
20 |
Correct |
3 ms |
2448 KB |
Output is correct |
21 |
Correct |
2 ms |
1236 KB |
Output is correct |
22 |
Correct |
2 ms |
2260 KB |
Output is correct |
23 |
Correct |
3 ms |
2132 KB |
Output is correct |
24 |
Correct |
3 ms |
2452 KB |
Output is correct |
25 |
Correct |
3 ms |
2388 KB |
Output is correct |
26 |
Correct |
2 ms |
2388 KB |
Output is correct |
27 |
Correct |
2 ms |
2388 KB |
Output is correct |
28 |
Correct |
3 ms |
2516 KB |
Output is correct |
29 |
Correct |
9 ms |
2496 KB |
Output is correct |
30 |
Correct |
4 ms |
2388 KB |
Output is correct |
31 |
Correct |
6 ms |
2476 KB |
Output is correct |
32 |
Correct |
5 ms |
2388 KB |
Output is correct |
33 |
Correct |
18 ms |
2644 KB |
Output is correct |
34 |
Correct |
15 ms |
2700 KB |
Output is correct |
35 |
Correct |
12 ms |
2896 KB |
Output is correct |
36 |
Correct |
3 ms |
1876 KB |
Output is correct |
37 |
Correct |
9 ms |
3092 KB |
Output is correct |
38 |
Correct |
9 ms |
3224 KB |
Output is correct |
39 |
Correct |
7 ms |
3052 KB |
Output is correct |
40 |
Correct |
7 ms |
3028 KB |
Output is correct |
41 |
Correct |
10 ms |
3156 KB |
Output is correct |
42 |
Correct |
6 ms |
3032 KB |
Output is correct |
43 |
Correct |
5 ms |
3028 KB |
Output is correct |
44 |
Correct |
6 ms |
3028 KB |
Output is correct |
45 |
Correct |
69 ms |
4864 KB |
Output is correct |
46 |
Correct |
53 ms |
4940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
1028 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
1032 KB |
Output is correct |
10 |
Correct |
1 ms |
1036 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1040 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1620 KB |
Output is correct |
18 |
Correct |
2 ms |
2192 KB |
Output is correct |
19 |
Correct |
2 ms |
2440 KB |
Output is correct |
20 |
Correct |
3 ms |
2388 KB |
Output is correct |
21 |
Correct |
2 ms |
1236 KB |
Output is correct |
22 |
Correct |
2 ms |
2188 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
3 ms |
2388 KB |
Output is correct |
25 |
Correct |
3 ms |
2388 KB |
Output is correct |
26 |
Correct |
2 ms |
2448 KB |
Output is correct |
27 |
Correct |
2 ms |
2388 KB |
Output is correct |
28 |
Correct |
3 ms |
2388 KB |
Output is correct |
29 |
Correct |
10 ms |
2508 KB |
Output is correct |
30 |
Correct |
3 ms |
2324 KB |
Output is correct |
31 |
Correct |
6 ms |
2388 KB |
Output is correct |
32 |
Correct |
4 ms |
2440 KB |
Output is correct |
33 |
Correct |
24 ms |
2572 KB |
Output is correct |
34 |
Correct |
12 ms |
2644 KB |
Output is correct |
35 |
Correct |
10 ms |
2908 KB |
Output is correct |
36 |
Correct |
4 ms |
1876 KB |
Output is correct |
37 |
Correct |
8 ms |
3108 KB |
Output is correct |
38 |
Correct |
9 ms |
3220 KB |
Output is correct |
39 |
Correct |
7 ms |
3096 KB |
Output is correct |
40 |
Correct |
7 ms |
3108 KB |
Output is correct |
41 |
Correct |
8 ms |
3156 KB |
Output is correct |
42 |
Correct |
6 ms |
3040 KB |
Output is correct |
43 |
Correct |
5 ms |
3092 KB |
Output is correct |
44 |
Correct |
6 ms |
3028 KB |
Output is correct |
45 |
Correct |
65 ms |
4868 KB |
Output is correct |
46 |
Correct |
38 ms |
4892 KB |
Output is correct |
47 |
Correct |
23 ms |
10904 KB |
Output is correct |
48 |
Correct |
15 ms |
17800 KB |
Output is correct |
49 |
Correct |
20 ms |
19212 KB |
Output is correct |
50 |
Correct |
15 ms |
20692 KB |
Output is correct |
51 |
Correct |
36 ms |
23416 KB |
Output is correct |
52 |
Correct |
38 ms |
23440 KB |
Output is correct |
53 |
Correct |
19 ms |
22660 KB |
Output is correct |
54 |
Correct |
14 ms |
21560 KB |
Output is correct |
55 |
Correct |
13 ms |
21584 KB |
Output is correct |
56 |
Correct |
24 ms |
22868 KB |
Output is correct |
57 |
Correct |
13 ms |
21604 KB |
Output is correct |
58 |
Correct |
31 ms |
22092 KB |
Output is correct |
59 |
Correct |
23 ms |
22288 KB |
Output is correct |
60 |
Correct |
32 ms |
22252 KB |
Output is correct |
61 |
Correct |
29 ms |
22284 KB |
Output is correct |
62 |
Correct |
48 ms |
22944 KB |
Output is correct |
63 |
Correct |
82 ms |
24076 KB |
Output is correct |
64 |
Correct |
93 ms |
24068 KB |
Output is correct |
65 |
Correct |
403 ms |
24204 KB |
Output is correct |
66 |
Execution timed out |
1000 ms |
25904 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |