#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=10000000;
int n,m;
vector<int> v[N];
int P[N], x[N];
int dist[N][B];
bool done[N][B];
vector<pii> 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
235808 KB |
Output is correct |
2 |
Correct |
108 ms |
235868 KB |
Output is correct |
3 |
Correct |
110 ms |
235904 KB |
Output is correct |
4 |
Correct |
123 ms |
235804 KB |
Output is correct |
5 |
Correct |
106 ms |
235792 KB |
Output is correct |
6 |
Correct |
105 ms |
235804 KB |
Output is correct |
7 |
Correct |
109 ms |
235852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
235788 KB |
Output is correct |
2 |
Correct |
106 ms |
235820 KB |
Output is correct |
3 |
Correct |
108 ms |
235852 KB |
Output is correct |
4 |
Correct |
125 ms |
235872 KB |
Output is correct |
5 |
Correct |
107 ms |
235944 KB |
Output is correct |
6 |
Correct |
107 ms |
235880 KB |
Output is correct |
7 |
Correct |
111 ms |
235804 KB |
Output is correct |
8 |
Correct |
129 ms |
235868 KB |
Output is correct |
9 |
Correct |
108 ms |
235852 KB |
Output is correct |
10 |
Correct |
109 ms |
235976 KB |
Output is correct |
11 |
Correct |
108 ms |
235920 KB |
Output is correct |
12 |
Correct |
116 ms |
235980 KB |
Output is correct |
13 |
Correct |
106 ms |
235928 KB |
Output is correct |
14 |
Correct |
108 ms |
235976 KB |
Output is correct |
15 |
Correct |
112 ms |
236108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
235852 KB |
Output is correct |
2 |
Correct |
108 ms |
235868 KB |
Output is correct |
3 |
Correct |
113 ms |
235852 KB |
Output is correct |
4 |
Correct |
122 ms |
235872 KB |
Output is correct |
5 |
Correct |
107 ms |
235820 KB |
Output is correct |
6 |
Correct |
105 ms |
235880 KB |
Output is correct |
7 |
Correct |
108 ms |
235792 KB |
Output is correct |
8 |
Correct |
118 ms |
235860 KB |
Output is correct |
9 |
Correct |
114 ms |
235928 KB |
Output is correct |
10 |
Correct |
110 ms |
235912 KB |
Output is correct |
11 |
Correct |
107 ms |
235980 KB |
Output is correct |
12 |
Correct |
111 ms |
235988 KB |
Output is correct |
13 |
Correct |
110 ms |
235940 KB |
Output is correct |
14 |
Correct |
108 ms |
235956 KB |
Output is correct |
15 |
Correct |
110 ms |
236068 KB |
Output is correct |
16 |
Correct |
122 ms |
236020 KB |
Output is correct |
17 |
Correct |
109 ms |
236588 KB |
Output is correct |
18 |
Correct |
125 ms |
236992 KB |
Output is correct |
19 |
Correct |
121 ms |
237300 KB |
Output is correct |
20 |
Correct |
107 ms |
237716 KB |
Output is correct |
21 |
Correct |
122 ms |
236108 KB |
Output is correct |
22 |
Correct |
125 ms |
237004 KB |
Output is correct |
23 |
Correct |
108 ms |
237264 KB |
Output is correct |
24 |
Correct |
108 ms |
237596 KB |
Output is correct |
25 |
Correct |
109 ms |
237576 KB |
Output is correct |
26 |
Correct |
107 ms |
237680 KB |
Output is correct |
27 |
Correct |
110 ms |
237660 KB |
Output is correct |
28 |
Correct |
110 ms |
238152 KB |
Output is correct |
29 |
Correct |
110 ms |
238292 KB |
Output is correct |
30 |
Correct |
110 ms |
237800 KB |
Output is correct |
31 |
Correct |
115 ms |
238148 KB |
Output is correct |
32 |
Correct |
110 ms |
237820 KB |
Output is correct |
33 |
Correct |
114 ms |
238796 KB |
Output is correct |
34 |
Correct |
109 ms |
238240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
235752 KB |
Output is correct |
2 |
Correct |
107 ms |
235852 KB |
Output is correct |
3 |
Correct |
110 ms |
235756 KB |
Output is correct |
4 |
Correct |
127 ms |
235872 KB |
Output is correct |
5 |
Correct |
106 ms |
235852 KB |
Output is correct |
6 |
Correct |
110 ms |
235872 KB |
Output is correct |
7 |
Correct |
106 ms |
235756 KB |
Output is correct |
8 |
Correct |
107 ms |
235792 KB |
Output is correct |
9 |
Correct |
122 ms |
235832 KB |
Output is correct |
10 |
Correct |
120 ms |
235848 KB |
Output is correct |
11 |
Correct |
112 ms |
235936 KB |
Output is correct |
12 |
Correct |
108 ms |
235904 KB |
Output is correct |
13 |
Correct |
115 ms |
235984 KB |
Output is correct |
14 |
Correct |
108 ms |
236012 KB |
Output is correct |
15 |
Correct |
107 ms |
235980 KB |
Output is correct |
16 |
Correct |
126 ms |
236072 KB |
Output is correct |
17 |
Correct |
108 ms |
236504 KB |
Output is correct |
18 |
Correct |
124 ms |
237104 KB |
Output is correct |
19 |
Correct |
124 ms |
237284 KB |
Output is correct |
20 |
Correct |
112 ms |
237784 KB |
Output is correct |
21 |
Correct |
125 ms |
236196 KB |
Output is correct |
22 |
Correct |
125 ms |
237056 KB |
Output is correct |
23 |
Correct |
113 ms |
237200 KB |
Output is correct |
24 |
Correct |
109 ms |
237568 KB |
Output is correct |
25 |
Correct |
109 ms |
237584 KB |
Output is correct |
26 |
Correct |
108 ms |
237644 KB |
Output is correct |
27 |
Correct |
110 ms |
237700 KB |
Output is correct |
28 |
Correct |
110 ms |
238156 KB |
Output is correct |
29 |
Correct |
110 ms |
238284 KB |
Output is correct |
30 |
Correct |
109 ms |
237704 KB |
Output is correct |
31 |
Correct |
110 ms |
238132 KB |
Output is correct |
32 |
Correct |
110 ms |
237900 KB |
Output is correct |
33 |
Correct |
111 ms |
238872 KB |
Output is correct |
34 |
Correct |
109 ms |
238284 KB |
Output is correct |
35 |
Correct |
111 ms |
237628 KB |
Output is correct |
36 |
Correct |
110 ms |
236804 KB |
Output is correct |
37 |
Correct |
110 ms |
237872 KB |
Output is correct |
38 |
Correct |
115 ms |
238064 KB |
Output is correct |
39 |
Correct |
113 ms |
237732 KB |
Output is correct |
40 |
Correct |
115 ms |
237880 KB |
Output is correct |
41 |
Correct |
117 ms |
238144 KB |
Output is correct |
42 |
Correct |
112 ms |
237964 KB |
Output is correct |
43 |
Correct |
113 ms |
237968 KB |
Output is correct |
44 |
Correct |
114 ms |
237972 KB |
Output is correct |
45 |
Correct |
121 ms |
240884 KB |
Output is correct |
46 |
Correct |
116 ms |
239312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
235868 KB |
Output is correct |
2 |
Correct |
108 ms |
235852 KB |
Output is correct |
3 |
Correct |
121 ms |
235820 KB |
Output is correct |
4 |
Correct |
123 ms |
235856 KB |
Output is correct |
5 |
Correct |
110 ms |
235836 KB |
Output is correct |
6 |
Correct |
111 ms |
235812 KB |
Output is correct |
7 |
Correct |
109 ms |
235852 KB |
Output is correct |
8 |
Correct |
110 ms |
235868 KB |
Output is correct |
9 |
Correct |
108 ms |
235864 KB |
Output is correct |
10 |
Correct |
112 ms |
235908 KB |
Output is correct |
11 |
Correct |
110 ms |
235876 KB |
Output is correct |
12 |
Correct |
110 ms |
235984 KB |
Output is correct |
13 |
Correct |
111 ms |
235964 KB |
Output is correct |
14 |
Correct |
108 ms |
236060 KB |
Output is correct |
15 |
Correct |
108 ms |
236020 KB |
Output is correct |
16 |
Correct |
129 ms |
235980 KB |
Output is correct |
17 |
Correct |
109 ms |
236592 KB |
Output is correct |
18 |
Correct |
129 ms |
237072 KB |
Output is correct |
19 |
Correct |
122 ms |
237416 KB |
Output is correct |
20 |
Correct |
114 ms |
237644 KB |
Output is correct |
21 |
Correct |
122 ms |
236168 KB |
Output is correct |
22 |
Correct |
125 ms |
237100 KB |
Output is correct |
23 |
Correct |
108 ms |
237212 KB |
Output is correct |
24 |
Correct |
109 ms |
237588 KB |
Output is correct |
25 |
Correct |
112 ms |
237564 KB |
Output is correct |
26 |
Correct |
110 ms |
237784 KB |
Output is correct |
27 |
Correct |
113 ms |
237600 KB |
Output is correct |
28 |
Correct |
121 ms |
238320 KB |
Output is correct |
29 |
Correct |
112 ms |
238296 KB |
Output is correct |
30 |
Correct |
111 ms |
237744 KB |
Output is correct |
31 |
Correct |
109 ms |
238028 KB |
Output is correct |
32 |
Correct |
111 ms |
237848 KB |
Output is correct |
33 |
Correct |
112 ms |
238840 KB |
Output is correct |
34 |
Correct |
128 ms |
238256 KB |
Output is correct |
35 |
Correct |
113 ms |
237624 KB |
Output is correct |
36 |
Correct |
115 ms |
236788 KB |
Output is correct |
37 |
Correct |
111 ms |
237908 KB |
Output is correct |
38 |
Correct |
113 ms |
238028 KB |
Output is correct |
39 |
Correct |
114 ms |
237660 KB |
Output is correct |
40 |
Correct |
113 ms |
237760 KB |
Output is correct |
41 |
Correct |
117 ms |
238156 KB |
Output is correct |
42 |
Correct |
111 ms |
238028 KB |
Output is correct |
43 |
Correct |
118 ms |
238060 KB |
Output is correct |
44 |
Correct |
130 ms |
238048 KB |
Output is correct |
45 |
Correct |
131 ms |
241024 KB |
Output is correct |
46 |
Correct |
123 ms |
239340 KB |
Output is correct |
47 |
Correct |
152 ms |
247240 KB |
Output is correct |
48 |
Correct |
137 ms |
252364 KB |
Output is correct |
49 |
Correct |
133 ms |
253820 KB |
Output is correct |
50 |
Correct |
143 ms |
255420 KB |
Output is correct |
51 |
Runtime error |
113 ms |
262144 KB |
Execution killed with signal 9 |
52 |
Halted |
0 ms |
0 KB |
- |