#include <bits/stdc++.h>
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define bp __builtin_popcount
#define fir first
#define sec second
#define pii pair<int, int>
#define pll pair<ll, ll>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
int dx[4]={0, 1, 0, -1}, dy[4]={1, 0, -1 , 0};
const int mxN=30010;
const int mxM=7300000;
const int mxK=100001;
const int MOD=1000000007;
const ll INF=9223372036854775807;
int N, M;
vector <pii> BP;
int l[mxM], r[mxM], in[mxM];
vector <int> out[mxN];
int idx;
int X, Y;
int lst[4*mxM], lc, rc;
int dis[mxM];
bool Chk[mxM];
bool cmp1(pii a, pii b)
{
if(a.sec!=b.sec) return a.sec<b.sec;
if(a.fir%a.sec!=b.fir%b.sec) return a.fir%a.sec<b.fir%b.sec;
return a.fir<b.fir;
}
void zero_one_bfs()
{
while(lc!=rc)
{
int now=lst[lc];
lc++;
if(Chk[now]) continue;
Chk[now]=true;
if(now<N)
{
for(int nxt : out[now])
{
if(dis[nxt]>dis[now])
{
dis[nxt]=dis[now];
lst[--lc]=nxt;
}
}
}
else
{
if(l[now]!=-1)
{
if(dis[l[now]]>dis[now]+1)
{
dis[l[now]]=dis[now]+1;
lst[rc++]=l[now];
//lst.push_back(l[now]);
}
}
if(r[now]!=-1)
{
if(dis[r[now]]>dis[now]+1)
{
dis[r[now]]=dis[now]+1;
lst[rc++]=r[now];
//lst.push_back(r[now]);
}
}
if(dis[in[now]]>dis[now])
{
dis[in[now]]=dis[now];
lst[--lc]=in[now];
//lst.push_front(in[now]);
}
}
}
}
int main()
{
gibon
cin >> N >> M;
idx=N;
BP.resize(M);
for(int i=0;i<M;i++)
{
cin >> BP[i].fir >> BP[i].sec;
if(i==0) X=BP[i].fir;
if(i==1) Y=BP[i].fir;
}
sort(BP.begin(), BP.end(), cmp1);
for(int i=0;i<mxM;i++) l[i]=r[i]=in[i]=-1;
int cur=0;
while(cur<M)
{
int ncur=cur;
bool jog=false;
for(int j=BP[ncur].fir%BP[ncur].sec;j<N;j+=BP[ncur].sec)
{
if(cur<M && BP[cur].fir==j && BP[cur].sec==BP[ncur].sec)
{
out[j].push_back(idx);
while(cur<M && BP[cur].fir==j && BP[cur].sec==BP[ncur].sec) cur++;
}
in[idx]=j;
if(!jog) jog=true;
else
{
l[idx]=idx-1;
r[idx-1]=idx;
}
idx++;
if(idx>=mxM) return 0;
}
}
for(int i=0;i<idx;i++)
{
dis[i]=MOD;
}
dis[X]=0;
lst[2*mxM]=X;
lc=2*mxM, rc=2*mxM+1;
zero_one_bfs();
if(dis[Y]==MOD) cout << -1;
else cout << dis[Y];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
86724 KB |
Output is correct |
2 |
Correct |
37 ms |
86712 KB |
Output is correct |
3 |
Correct |
38 ms |
86664 KB |
Output is correct |
4 |
Correct |
38 ms |
86664 KB |
Output is correct |
5 |
Correct |
37 ms |
86636 KB |
Output is correct |
6 |
Correct |
40 ms |
86704 KB |
Output is correct |
7 |
Correct |
36 ms |
86724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
86652 KB |
Output is correct |
2 |
Correct |
40 ms |
86708 KB |
Output is correct |
3 |
Correct |
38 ms |
86676 KB |
Output is correct |
4 |
Correct |
37 ms |
86680 KB |
Output is correct |
5 |
Correct |
40 ms |
86640 KB |
Output is correct |
6 |
Correct |
37 ms |
86616 KB |
Output is correct |
7 |
Correct |
37 ms |
86720 KB |
Output is correct |
8 |
Correct |
38 ms |
86660 KB |
Output is correct |
9 |
Correct |
37 ms |
86660 KB |
Output is correct |
10 |
Correct |
37 ms |
86732 KB |
Output is correct |
11 |
Correct |
39 ms |
86740 KB |
Output is correct |
12 |
Correct |
39 ms |
86808 KB |
Output is correct |
13 |
Correct |
37 ms |
86660 KB |
Output is correct |
14 |
Correct |
38 ms |
86760 KB |
Output is correct |
15 |
Correct |
38 ms |
86816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
86724 KB |
Output is correct |
2 |
Correct |
37 ms |
86732 KB |
Output is correct |
3 |
Correct |
38 ms |
86700 KB |
Output is correct |
4 |
Correct |
37 ms |
86740 KB |
Output is correct |
5 |
Correct |
38 ms |
86752 KB |
Output is correct |
6 |
Correct |
38 ms |
86724 KB |
Output is correct |
7 |
Correct |
37 ms |
86708 KB |
Output is correct |
8 |
Correct |
37 ms |
86740 KB |
Output is correct |
9 |
Correct |
37 ms |
86720 KB |
Output is correct |
10 |
Correct |
40 ms |
86732 KB |
Output is correct |
11 |
Correct |
38 ms |
86724 KB |
Output is correct |
12 |
Correct |
37 ms |
86668 KB |
Output is correct |
13 |
Correct |
37 ms |
86708 KB |
Output is correct |
14 |
Correct |
39 ms |
86764 KB |
Output is correct |
15 |
Correct |
38 ms |
86724 KB |
Output is correct |
16 |
Correct |
37 ms |
86692 KB |
Output is correct |
17 |
Correct |
39 ms |
86812 KB |
Output is correct |
18 |
Correct |
38 ms |
86732 KB |
Output is correct |
19 |
Correct |
38 ms |
86696 KB |
Output is correct |
20 |
Correct |
38 ms |
86760 KB |
Output is correct |
21 |
Correct |
37 ms |
86728 KB |
Output is correct |
22 |
Correct |
39 ms |
86676 KB |
Output is correct |
23 |
Correct |
38 ms |
86736 KB |
Output is correct |
24 |
Correct |
39 ms |
86840 KB |
Output is correct |
25 |
Correct |
38 ms |
86836 KB |
Output is correct |
26 |
Correct |
37 ms |
86724 KB |
Output is correct |
27 |
Correct |
39 ms |
86720 KB |
Output is correct |
28 |
Correct |
38 ms |
86988 KB |
Output is correct |
29 |
Correct |
40 ms |
87284 KB |
Output is correct |
30 |
Correct |
38 ms |
86792 KB |
Output is correct |
31 |
Correct |
40 ms |
86980 KB |
Output is correct |
32 |
Correct |
37 ms |
86964 KB |
Output is correct |
33 |
Correct |
41 ms |
87760 KB |
Output is correct |
34 |
Correct |
43 ms |
87712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
86732 KB |
Output is correct |
2 |
Correct |
37 ms |
86724 KB |
Output is correct |
3 |
Correct |
38 ms |
86704 KB |
Output is correct |
4 |
Correct |
38 ms |
86684 KB |
Output is correct |
5 |
Correct |
39 ms |
86672 KB |
Output is correct |
6 |
Correct |
38 ms |
86724 KB |
Output is correct |
7 |
Correct |
41 ms |
86636 KB |
Output is correct |
8 |
Correct |
38 ms |
86668 KB |
Output is correct |
9 |
Correct |
38 ms |
86664 KB |
Output is correct |
10 |
Correct |
38 ms |
86760 KB |
Output is correct |
11 |
Correct |
38 ms |
86732 KB |
Output is correct |
12 |
Correct |
37 ms |
86752 KB |
Output is correct |
13 |
Correct |
39 ms |
86664 KB |
Output is correct |
14 |
Correct |
39 ms |
86724 KB |
Output is correct |
15 |
Correct |
40 ms |
86736 KB |
Output is correct |
16 |
Correct |
38 ms |
86656 KB |
Output is correct |
17 |
Correct |
39 ms |
86860 KB |
Output is correct |
18 |
Correct |
38 ms |
86732 KB |
Output is correct |
19 |
Correct |
38 ms |
86700 KB |
Output is correct |
20 |
Correct |
40 ms |
86784 KB |
Output is correct |
21 |
Correct |
38 ms |
86696 KB |
Output is correct |
22 |
Correct |
40 ms |
86764 KB |
Output is correct |
23 |
Correct |
38 ms |
86788 KB |
Output is correct |
24 |
Correct |
38 ms |
86980 KB |
Output is correct |
25 |
Correct |
39 ms |
86988 KB |
Output is correct |
26 |
Correct |
38 ms |
86716 KB |
Output is correct |
27 |
Correct |
39 ms |
86796 KB |
Output is correct |
28 |
Correct |
40 ms |
86944 KB |
Output is correct |
29 |
Correct |
39 ms |
87244 KB |
Output is correct |
30 |
Correct |
38 ms |
86852 KB |
Output is correct |
31 |
Correct |
39 ms |
86956 KB |
Output is correct |
32 |
Correct |
41 ms |
86988 KB |
Output is correct |
33 |
Correct |
50 ms |
87776 KB |
Output is correct |
34 |
Correct |
42 ms |
87756 KB |
Output is correct |
35 |
Correct |
49 ms |
87932 KB |
Output is correct |
36 |
Correct |
39 ms |
86932 KB |
Output is correct |
37 |
Correct |
50 ms |
88456 KB |
Output is correct |
38 |
Correct |
54 ms |
88412 KB |
Output is correct |
39 |
Correct |
54 ms |
88512 KB |
Output is correct |
40 |
Correct |
54 ms |
88572 KB |
Output is correct |
41 |
Correct |
56 ms |
88528 KB |
Output is correct |
42 |
Correct |
48 ms |
86980 KB |
Output is correct |
43 |
Correct |
48 ms |
86980 KB |
Output is correct |
44 |
Correct |
47 ms |
86972 KB |
Output is correct |
45 |
Correct |
73 ms |
91224 KB |
Output is correct |
46 |
Correct |
74 ms |
91204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
86632 KB |
Output is correct |
2 |
Correct |
36 ms |
86724 KB |
Output is correct |
3 |
Correct |
40 ms |
86668 KB |
Output is correct |
4 |
Correct |
38 ms |
86668 KB |
Output is correct |
5 |
Correct |
37 ms |
86768 KB |
Output is correct |
6 |
Correct |
37 ms |
86724 KB |
Output is correct |
7 |
Correct |
38 ms |
86660 KB |
Output is correct |
8 |
Correct |
37 ms |
86668 KB |
Output is correct |
9 |
Correct |
37 ms |
86708 KB |
Output is correct |
10 |
Correct |
37 ms |
86744 KB |
Output is correct |
11 |
Correct |
40 ms |
86752 KB |
Output is correct |
12 |
Correct |
38 ms |
86644 KB |
Output is correct |
13 |
Correct |
37 ms |
86676 KB |
Output is correct |
14 |
Correct |
39 ms |
86768 KB |
Output is correct |
15 |
Correct |
38 ms |
86732 KB |
Output is correct |
16 |
Correct |
38 ms |
86668 KB |
Output is correct |
17 |
Correct |
37 ms |
86860 KB |
Output is correct |
18 |
Correct |
37 ms |
86736 KB |
Output is correct |
19 |
Correct |
37 ms |
86820 KB |
Output is correct |
20 |
Correct |
41 ms |
86860 KB |
Output is correct |
21 |
Correct |
38 ms |
86724 KB |
Output is correct |
22 |
Correct |
37 ms |
86744 KB |
Output is correct |
23 |
Correct |
39 ms |
86816 KB |
Output is correct |
24 |
Correct |
40 ms |
86864 KB |
Output is correct |
25 |
Correct |
38 ms |
86768 KB |
Output is correct |
26 |
Correct |
38 ms |
86772 KB |
Output is correct |
27 |
Correct |
37 ms |
86724 KB |
Output is correct |
28 |
Correct |
39 ms |
86916 KB |
Output is correct |
29 |
Correct |
39 ms |
87256 KB |
Output is correct |
30 |
Correct |
38 ms |
86856 KB |
Output is correct |
31 |
Correct |
38 ms |
86976 KB |
Output is correct |
32 |
Correct |
38 ms |
86852 KB |
Output is correct |
33 |
Correct |
40 ms |
87768 KB |
Output is correct |
34 |
Correct |
41 ms |
87768 KB |
Output is correct |
35 |
Correct |
50 ms |
87980 KB |
Output is correct |
36 |
Correct |
41 ms |
86984 KB |
Output is correct |
37 |
Correct |
51 ms |
88512 KB |
Output is correct |
38 |
Correct |
54 ms |
88564 KB |
Output is correct |
39 |
Correct |
54 ms |
88444 KB |
Output is correct |
40 |
Correct |
55 ms |
88436 KB |
Output is correct |
41 |
Correct |
54 ms |
88408 KB |
Output is correct |
42 |
Correct |
47 ms |
87036 KB |
Output is correct |
43 |
Correct |
48 ms |
87040 KB |
Output is correct |
44 |
Correct |
47 ms |
87036 KB |
Output is correct |
45 |
Correct |
77 ms |
91204 KB |
Output is correct |
46 |
Correct |
67 ms |
91252 KB |
Output is correct |
47 |
Correct |
83 ms |
93688 KB |
Output is correct |
48 |
Correct |
47 ms |
88384 KB |
Output is correct |
49 |
Correct |
48 ms |
88032 KB |
Output is correct |
50 |
Correct |
44 ms |
87872 KB |
Output is correct |
51 |
Correct |
61 ms |
90308 KB |
Output is correct |
52 |
Correct |
63 ms |
90536 KB |
Output is correct |
53 |
Correct |
56 ms |
88388 KB |
Output is correct |
54 |
Correct |
37 ms |
87012 KB |
Output is correct |
55 |
Correct |
43 ms |
87108 KB |
Output is correct |
56 |
Correct |
58 ms |
88308 KB |
Output is correct |
57 |
Correct |
53 ms |
90772 KB |
Output is correct |
58 |
Correct |
47 ms |
87668 KB |
Output is correct |
59 |
Correct |
51 ms |
87884 KB |
Output is correct |
60 |
Correct |
49 ms |
88184 KB |
Output is correct |
61 |
Correct |
50 ms |
88084 KB |
Output is correct |
62 |
Correct |
61 ms |
92228 KB |
Output is correct |
63 |
Correct |
230 ms |
107332 KB |
Output is correct |
64 |
Correct |
226 ms |
111812 KB |
Output is correct |
65 |
Correct |
481 ms |
122616 KB |
Output is correct |
66 |
Correct |
826 ms |
150960 KB |
Output is correct |
67 |
Correct |
819 ms |
150828 KB |
Output is correct |