#include <bits/stdc++.h>
using namespace std;
int deq[21000005], edge[21000005], nxt[21000005], lnk[10500005], dist[10500005], a[30005][175], n, m, s, t, sz, cnt, l=10500000, r=10500000;
void add(int u, int v, int w)
{
cnt++;
edge[cnt]=v*2+w;
nxt[cnt]=lnk[u];
lnk[u]=cnt;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
sz=n;
for (int i=0; i<m; i++)
{
int b, p;
cin >> b >> p;
if (i==0)
s=b;
else if (i==1)
t=b;
if (p*p<=m)
{
if (!a[b][p])
{
a[b%p][p]=sz;
sz++;
add(a[b%p][p], b%p, 0);
for (int j=b%p+p; j<n; j+=p)
{
a[j][p]=sz;
sz++;
add(a[j][p], j, 0);
add(a[j][p], a[j-p][p], 1);
add(a[j-p][p], a[j][p], 1);
}
}
add(b, a[b][p], 0);
}
else
{
int prev=b;
for (int j=b-p; j>=0; j-=p)
{
add(prev, sz, 1);
add(sz, j, 0);
prev=sz;
sz++;
}
prev=b;
for (int j=b+p; j<n; j+=p)
{
add(prev, sz, 1);
add(sz, j, 0);
prev=sz;
sz++;
}
}
}
for (int i=0; i<sz; i++)
dist[i]=1e9;
dist[s]=0;
deq[10500000]=s;
while (l<=r)
{
int u=deq[l], cur;
l++;
cur=lnk[u];
while (cur)
{
int v=edge[cur]>>1, w=edge[cur]&1;
if (dist[v]==1e9)
{
dist[v]=dist[u]+w;
if (w)
{
r++;
deq[r]=v;
}
else
{
l--;
deq[l]=v;
}
}
cur=nxt[cur];
}
}
if (dist[t]==1e9)
cout << -1;
else
cout << dist[t];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
456 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
572 KB |
Output is correct |
17 |
Correct |
2 ms |
1228 KB |
Output is correct |
18 |
Correct |
1 ms |
1740 KB |
Output is correct |
19 |
Correct |
1 ms |
1868 KB |
Output is correct |
20 |
Correct |
1 ms |
1740 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
1740 KB |
Output is correct |
23 |
Correct |
1 ms |
1604 KB |
Output is correct |
24 |
Correct |
2 ms |
1996 KB |
Output is correct |
25 |
Correct |
2 ms |
1868 KB |
Output is correct |
26 |
Correct |
1 ms |
1996 KB |
Output is correct |
27 |
Correct |
2 ms |
1868 KB |
Output is correct |
28 |
Correct |
2 ms |
2376 KB |
Output is correct |
29 |
Correct |
4 ms |
3660 KB |
Output is correct |
30 |
Correct |
2 ms |
2124 KB |
Output is correct |
31 |
Correct |
3 ms |
2636 KB |
Output is correct |
32 |
Correct |
3 ms |
2380 KB |
Output is correct |
33 |
Correct |
9 ms |
5620 KB |
Output is correct |
34 |
Correct |
8 ms |
5524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
2 ms |
1228 KB |
Output is correct |
18 |
Correct |
1 ms |
1612 KB |
Output is correct |
19 |
Correct |
1 ms |
1868 KB |
Output is correct |
20 |
Correct |
1 ms |
1736 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
1612 KB |
Output is correct |
23 |
Correct |
1 ms |
1612 KB |
Output is correct |
24 |
Correct |
2 ms |
1996 KB |
Output is correct |
25 |
Correct |
1 ms |
1868 KB |
Output is correct |
26 |
Correct |
1 ms |
1996 KB |
Output is correct |
27 |
Correct |
1 ms |
1868 KB |
Output is correct |
28 |
Correct |
2 ms |
2380 KB |
Output is correct |
29 |
Correct |
4 ms |
3660 KB |
Output is correct |
30 |
Correct |
2 ms |
2112 KB |
Output is correct |
31 |
Correct |
3 ms |
2636 KB |
Output is correct |
32 |
Correct |
2 ms |
2380 KB |
Output is correct |
33 |
Correct |
8 ms |
5592 KB |
Output is correct |
34 |
Correct |
8 ms |
5580 KB |
Output is correct |
35 |
Correct |
11 ms |
4036 KB |
Output is correct |
36 |
Correct |
2 ms |
1484 KB |
Output is correct |
37 |
Correct |
15 ms |
6800 KB |
Output is correct |
38 |
Correct |
14 ms |
6212 KB |
Output is correct |
39 |
Correct |
17 ms |
6348 KB |
Output is correct |
40 |
Correct |
15 ms |
6228 KB |
Output is correct |
41 |
Correct |
15 ms |
6212 KB |
Output is correct |
42 |
Correct |
6 ms |
2380 KB |
Output is correct |
43 |
Correct |
5 ms |
2356 KB |
Output is correct |
44 |
Correct |
5 ms |
2252 KB |
Output is correct |
45 |
Correct |
65 ms |
17132 KB |
Output is correct |
46 |
Correct |
72 ms |
17092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
452 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
580 KB |
Output is correct |
17 |
Correct |
1 ms |
1228 KB |
Output is correct |
18 |
Correct |
1 ms |
1736 KB |
Output is correct |
19 |
Correct |
2 ms |
1740 KB |
Output is correct |
20 |
Correct |
1 ms |
1740 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
1612 KB |
Output is correct |
23 |
Correct |
1 ms |
1668 KB |
Output is correct |
24 |
Correct |
2 ms |
1996 KB |
Output is correct |
25 |
Correct |
2 ms |
1868 KB |
Output is correct |
26 |
Correct |
2 ms |
1996 KB |
Output is correct |
27 |
Correct |
2 ms |
1976 KB |
Output is correct |
28 |
Correct |
2 ms |
2380 KB |
Output is correct |
29 |
Correct |
3 ms |
3660 KB |
Output is correct |
30 |
Correct |
1 ms |
2124 KB |
Output is correct |
31 |
Correct |
2 ms |
2636 KB |
Output is correct |
32 |
Correct |
2 ms |
2380 KB |
Output is correct |
33 |
Correct |
8 ms |
5580 KB |
Output is correct |
34 |
Correct |
9 ms |
5560 KB |
Output is correct |
35 |
Correct |
8 ms |
4044 KB |
Output is correct |
36 |
Correct |
2 ms |
1484 KB |
Output is correct |
37 |
Correct |
17 ms |
6840 KB |
Output is correct |
38 |
Correct |
15 ms |
6220 KB |
Output is correct |
39 |
Correct |
15 ms |
6340 KB |
Output is correct |
40 |
Correct |
15 ms |
6220 KB |
Output is correct |
41 |
Correct |
15 ms |
6220 KB |
Output is correct |
42 |
Correct |
5 ms |
2380 KB |
Output is correct |
43 |
Correct |
7 ms |
2380 KB |
Output is correct |
44 |
Correct |
4 ms |
2124 KB |
Output is correct |
45 |
Correct |
66 ms |
17144 KB |
Output is correct |
46 |
Correct |
66 ms |
17044 KB |
Output is correct |
47 |
Correct |
86 ms |
32000 KB |
Output is correct |
48 |
Correct |
17 ms |
22600 KB |
Output is correct |
49 |
Correct |
14 ms |
21332 KB |
Output is correct |
50 |
Correct |
17 ms |
23328 KB |
Output is correct |
51 |
Correct |
36 ms |
30532 KB |
Output is correct |
52 |
Correct |
36 ms |
31520 KB |
Output is correct |
53 |
Correct |
15 ms |
23372 KB |
Output is correct |
54 |
Correct |
1 ms |
1228 KB |
Output is correct |
55 |
Correct |
11 ms |
22716 KB |
Output is correct |
56 |
Correct |
18 ms |
22612 KB |
Output is correct |
57 |
Correct |
47 ms |
37900 KB |
Output is correct |
58 |
Correct |
15 ms |
23628 KB |
Output is correct |
59 |
Correct |
20 ms |
24908 KB |
Output is correct |
60 |
Correct |
23 ms |
26144 KB |
Output is correct |
61 |
Correct |
21 ms |
25060 KB |
Output is correct |
62 |
Correct |
34 ms |
36044 KB |
Output is correct |
63 |
Correct |
308 ms |
83004 KB |
Output is correct |
64 |
Correct |
337 ms |
97444 KB |
Output is correct |
65 |
Correct |
905 ms |
140148 KB |
Output is correct |
66 |
Execution timed out |
1094 ms |
242736 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |