#include<bits/stdc++.h>
#define ll int
#define pb push_back
#define mp make_pair
#define ld long double
#define F first
#define S second
#define pii pair<ll,ll>
using namespace :: std;
const ll maxn=3e4+10;
const ll mod=1e9+7;
const ll inf=1e9+500;
const ll rad=100;
const ll maxm=maxn*(rad)*3+50;
ll b[maxn];
ll p[maxn];
vector<ll> vec[maxn];
ll cn;
vector<pair<ll,bool> > ger[maxm];
bitset<maxm> h;
int main(){
ll n,m;
cin>>n>>m;
for(ll i=0;i<m;i++){
cin>>b[i]>>p[i];
vec[b[i]].pb(i);
}
for(ll i=0;i<n;i++){
for(auto v:vec[i]){
ger[i+m].pb(mp(v,0));
}
}
for(ll j=1;j<rad;j++){
for(ll i=0;i<n;i++){
ger[m+j*n+i].pb(mp(i+m,0));
if(i>=j){
ger[m+j*n+i].pb(mp(m+j*n+i-j,1));
}
}
}
for(ll j=1;j<rad;j++){
for(ll i=0;i<n;i++){
ll v=m+rad*n+(j-1)*n+i;
ger[v].pb(mp(i+m,0));
if(i+j<n){
ger[v].pb(mp(v+j,1));
}
}
}
for(ll i=0;i<m;i++){
if(p[i]<rad){
ll v=b[i];
ger[i].pb(mp(m+n*p[i]+v,0));
ger[i].pb(mp(m+rad*n+(p[i]-1)*n+v,0));
}
}
cn=m+n*2*rad+1;
for(ll i=0;i<m;i++){
if(p[i]>=rad){
ll mo=cn;
ger[i].pb(mp(cn,0));
ger[cn].pb(mp(b[i]+m,0));
cn++;
for(ll j=b[i]+p[i];j<n;j+=p[i]){
ger[cn].pb(mp(j+m,0));
ger[cn-1].pb(mp(cn,1));
cn++;
}
for(ll j=b[i]-p[i];j>=0;j-=p[i]){
ger[cn].pb(mp(j+m,0));
ger[mo].pb(mp(cn,1));
mo=cn;
cn++;
}
}
}
deque<pii>dk;
dk.pb(mp(0,1));
while(dk.size()){
ll v=dk.front().F;
ll w=dk.front().S;
dk.pop_front();
if(!h[v]){
if(v==1){
cout<<w-1;
return 0;
}
h[v]=1;
for(auto e:ger[v]){
if(e.S){
dk.push_back(mp(e.F,w+1));
}else{
dk.push_front(mp(e.F,w));
}
}
}
}
cout<<-1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
165 ms |
212600 KB |
Output is correct |
2 |
Correct |
164 ms |
212600 KB |
Output is correct |
3 |
Correct |
163 ms |
212660 KB |
Output is correct |
4 |
Correct |
168 ms |
212660 KB |
Output is correct |
5 |
Correct |
165 ms |
212660 KB |
Output is correct |
6 |
Correct |
167 ms |
212732 KB |
Output is correct |
7 |
Correct |
166 ms |
212732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
212732 KB |
Output is correct |
2 |
Correct |
165 ms |
212744 KB |
Output is correct |
3 |
Correct |
164 ms |
212744 KB |
Output is correct |
4 |
Correct |
162 ms |
212744 KB |
Output is correct |
5 |
Correct |
163 ms |
212744 KB |
Output is correct |
6 |
Correct |
163 ms |
212900 KB |
Output is correct |
7 |
Correct |
167 ms |
212900 KB |
Output is correct |
8 |
Correct |
166 ms |
213096 KB |
Output is correct |
9 |
Correct |
166 ms |
213160 KB |
Output is correct |
10 |
Correct |
169 ms |
213372 KB |
Output is correct |
11 |
Correct |
173 ms |
213500 KB |
Output is correct |
12 |
Correct |
167 ms |
213544 KB |
Output is correct |
13 |
Correct |
173 ms |
213544 KB |
Output is correct |
14 |
Correct |
206 ms |
213544 KB |
Output is correct |
15 |
Correct |
166 ms |
213544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
213544 KB |
Output is correct |
2 |
Correct |
164 ms |
213544 KB |
Output is correct |
3 |
Correct |
163 ms |
213544 KB |
Output is correct |
4 |
Correct |
167 ms |
213544 KB |
Output is correct |
5 |
Correct |
166 ms |
213544 KB |
Output is correct |
6 |
Correct |
165 ms |
213544 KB |
Output is correct |
7 |
Correct |
166 ms |
213544 KB |
Output is correct |
8 |
Correct |
162 ms |
213544 KB |
Output is correct |
9 |
Correct |
166 ms |
213544 KB |
Output is correct |
10 |
Correct |
176 ms |
213544 KB |
Output is correct |
11 |
Correct |
166 ms |
213544 KB |
Output is correct |
12 |
Correct |
169 ms |
213544 KB |
Output is correct |
13 |
Correct |
167 ms |
213544 KB |
Output is correct |
14 |
Correct |
168 ms |
213544 KB |
Output is correct |
15 |
Correct |
169 ms |
213544 KB |
Output is correct |
16 |
Correct |
170 ms |
214012 KB |
Output is correct |
17 |
Correct |
182 ms |
217724 KB |
Output is correct |
18 |
Correct |
204 ms |
223740 KB |
Output is correct |
19 |
Correct |
210 ms |
225200 KB |
Output is correct |
20 |
Correct |
209 ms |
225320 KB |
Output is correct |
21 |
Correct |
187 ms |
225320 KB |
Output is correct |
22 |
Correct |
206 ms |
225320 KB |
Output is correct |
23 |
Correct |
201 ms |
225320 KB |
Output is correct |
24 |
Correct |
221 ms |
225320 KB |
Output is correct |
25 |
Correct |
215 ms |
225620 KB |
Output is correct |
26 |
Correct |
220 ms |
225620 KB |
Output is correct |
27 |
Correct |
209 ms |
225620 KB |
Output is correct |
28 |
Correct |
228 ms |
225736 KB |
Output is correct |
29 |
Correct |
220 ms |
225736 KB |
Output is correct |
30 |
Correct |
210 ms |
225736 KB |
Output is correct |
31 |
Correct |
214 ms |
225736 KB |
Output is correct |
32 |
Correct |
212 ms |
225736 KB |
Output is correct |
33 |
Correct |
217 ms |
225736 KB |
Output is correct |
34 |
Correct |
216 ms |
225736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
225736 KB |
Output is correct |
2 |
Correct |
168 ms |
225736 KB |
Output is correct |
3 |
Correct |
163 ms |
225736 KB |
Output is correct |
4 |
Correct |
168 ms |
225736 KB |
Output is correct |
5 |
Correct |
162 ms |
225736 KB |
Output is correct |
6 |
Correct |
170 ms |
225736 KB |
Output is correct |
7 |
Correct |
163 ms |
225736 KB |
Output is correct |
8 |
Correct |
165 ms |
225736 KB |
Output is correct |
9 |
Correct |
165 ms |
225736 KB |
Output is correct |
10 |
Correct |
165 ms |
225736 KB |
Output is correct |
11 |
Correct |
169 ms |
225736 KB |
Output is correct |
12 |
Correct |
168 ms |
225736 KB |
Output is correct |
13 |
Correct |
175 ms |
225736 KB |
Output is correct |
14 |
Correct |
165 ms |
225736 KB |
Output is correct |
15 |
Correct |
168 ms |
225736 KB |
Output is correct |
16 |
Correct |
170 ms |
225736 KB |
Output is correct |
17 |
Correct |
183 ms |
225736 KB |
Output is correct |
18 |
Correct |
205 ms |
225736 KB |
Output is correct |
19 |
Correct |
231 ms |
225736 KB |
Output is correct |
20 |
Correct |
213 ms |
225736 KB |
Output is correct |
21 |
Correct |
172 ms |
225736 KB |
Output is correct |
22 |
Correct |
208 ms |
225736 KB |
Output is correct |
23 |
Correct |
205 ms |
225736 KB |
Output is correct |
24 |
Correct |
211 ms |
225736 KB |
Output is correct |
25 |
Correct |
209 ms |
225776 KB |
Output is correct |
26 |
Correct |
212 ms |
225776 KB |
Output is correct |
27 |
Correct |
215 ms |
225776 KB |
Output is correct |
28 |
Correct |
228 ms |
226160 KB |
Output is correct |
29 |
Correct |
214 ms |
226160 KB |
Output is correct |
30 |
Correct |
223 ms |
226160 KB |
Output is correct |
31 |
Correct |
210 ms |
226160 KB |
Output is correct |
32 |
Correct |
209 ms |
226160 KB |
Output is correct |
33 |
Correct |
220 ms |
226160 KB |
Output is correct |
34 |
Correct |
215 ms |
226160 KB |
Output is correct |
35 |
Correct |
232 ms |
226160 KB |
Output is correct |
36 |
Correct |
197 ms |
226160 KB |
Output is correct |
37 |
Correct |
267 ms |
229980 KB |
Output is correct |
38 |
Correct |
257 ms |
231212 KB |
Output is correct |
39 |
Correct |
250 ms |
231460 KB |
Output is correct |
40 |
Correct |
254 ms |
231736 KB |
Output is correct |
41 |
Correct |
250 ms |
232128 KB |
Output is correct |
42 |
Correct |
230 ms |
232128 KB |
Output is correct |
43 |
Correct |
231 ms |
232128 KB |
Output is correct |
44 |
Correct |
242 ms |
232128 KB |
Output is correct |
45 |
Correct |
342 ms |
239216 KB |
Output is correct |
46 |
Correct |
302 ms |
239372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
178 ms |
239372 KB |
Output is correct |
2 |
Correct |
169 ms |
239372 KB |
Output is correct |
3 |
Correct |
171 ms |
239372 KB |
Output is correct |
4 |
Correct |
164 ms |
239372 KB |
Output is correct |
5 |
Correct |
162 ms |
239372 KB |
Output is correct |
6 |
Correct |
165 ms |
239372 KB |
Output is correct |
7 |
Correct |
172 ms |
239372 KB |
Output is correct |
8 |
Correct |
163 ms |
239372 KB |
Output is correct |
9 |
Correct |
164 ms |
239372 KB |
Output is correct |
10 |
Correct |
176 ms |
239372 KB |
Output is correct |
11 |
Correct |
178 ms |
239372 KB |
Output is correct |
12 |
Correct |
190 ms |
239372 KB |
Output is correct |
13 |
Correct |
190 ms |
239372 KB |
Output is correct |
14 |
Correct |
167 ms |
239372 KB |
Output is correct |
15 |
Correct |
187 ms |
239372 KB |
Output is correct |
16 |
Correct |
169 ms |
239372 KB |
Output is correct |
17 |
Correct |
213 ms |
239372 KB |
Output is correct |
18 |
Correct |
206 ms |
239372 KB |
Output is correct |
19 |
Correct |
219 ms |
239372 KB |
Output is correct |
20 |
Correct |
220 ms |
239372 KB |
Output is correct |
21 |
Correct |
208 ms |
239372 KB |
Output is correct |
22 |
Correct |
224 ms |
239372 KB |
Output is correct |
23 |
Correct |
232 ms |
239372 KB |
Output is correct |
24 |
Correct |
243 ms |
239372 KB |
Output is correct |
25 |
Correct |
241 ms |
239372 KB |
Output is correct |
26 |
Correct |
237 ms |
239372 KB |
Output is correct |
27 |
Correct |
264 ms |
239372 KB |
Output is correct |
28 |
Correct |
245 ms |
239372 KB |
Output is correct |
29 |
Correct |
240 ms |
239372 KB |
Output is correct |
30 |
Correct |
235 ms |
239372 KB |
Output is correct |
31 |
Correct |
238 ms |
239372 KB |
Output is correct |
32 |
Correct |
245 ms |
239372 KB |
Output is correct |
33 |
Correct |
229 ms |
239372 KB |
Output is correct |
34 |
Correct |
244 ms |
239372 KB |
Output is correct |
35 |
Correct |
264 ms |
239372 KB |
Output is correct |
36 |
Correct |
219 ms |
239372 KB |
Output is correct |
37 |
Correct |
279 ms |
239372 KB |
Output is correct |
38 |
Correct |
293 ms |
239372 KB |
Output is correct |
39 |
Correct |
277 ms |
239372 KB |
Output is correct |
40 |
Correct |
299 ms |
239372 KB |
Output is correct |
41 |
Correct |
301 ms |
239372 KB |
Output is correct |
42 |
Correct |
277 ms |
239372 KB |
Output is correct |
43 |
Correct |
277 ms |
239372 KB |
Output is correct |
44 |
Correct |
248 ms |
239372 KB |
Output is correct |
45 |
Correct |
327 ms |
241696 KB |
Output is correct |
46 |
Correct |
344 ms |
241964 KB |
Output is correct |
47 |
Execution timed out |
1061 ms |
262144 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |