#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[2*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()
{
int cnt=0;
while(lc!=rc)
{
//assert(cnt<=mxM/3);
int now=lst[lc];
lc++;
cnt++;
Chk[now]=true;
if(now==Y) return;
if(now<=N)
{
for(int nxt : out[now])
{
if(Chk[nxt] && dis[nxt]<=dis[now]) continue;
dis[nxt]=dis[now];
lst[--lc]=nxt;
Chk[nxt]=true;
if(nxt==Y) return;
}
}
else
{
if(l[now]!=-1)
{
if(!Chk[l[now]] && 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(!Chk[r[now]] && dis[r[now]]>dis[now]+1)
{
dis[r[now]]=dis[now]+1;
lst[rc++]=r[now];
//lst.push_back(r[now]);
}
}
if(!Chk[in[now]] && dis[in[now]]>dis[now])
{
dis[in[now]]=dis[now];
lst[--lc]=in[now];
Chk[in[now]]=true;
//lst.push_front(in[now]);
if(in[now]==Y) return;
}
}
}
}
int main()
{
gibon
cin >> N >> M;
idx=N+1;
BP.resize(M);
for(int i=0;i<M;i++)
{
cin >> BP[i].fir >> BP[i].sec;
BP[i].fir++;
if(i==0) X=BP[i].fir;
if(i==1) Y=BP[i].fir;
}
sort(BP.begin(), BP.end(), cmp1);
int cur=0;
while(cur<M)
{
int ncur=cur;
bool jog=false;
for(int j=(BP[ncur].fir-1)%BP[ncur].sec+1;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++;
}
}
for(int i=0;i<idx;i++)
{
dis[i]=mxM;
}
dis[X]=0;
lst[mxM]=X;
lc=mxM, rc=mxM+1;
//assert(idx<=2*mxM/3);
zero_one_bfs();
if(dis[Y]==mxM) cout << -1;
else cout << dis[Y];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
972 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
972 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
2 ms |
1356 KB |
Output is correct |
18 |
Correct |
1 ms |
1228 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
2 ms |
1100 KB |
Output is correct |
21 |
Correct |
1 ms |
1100 KB |
Output is correct |
22 |
Correct |
1 ms |
1100 KB |
Output is correct |
23 |
Correct |
2 ms |
1228 KB |
Output is correct |
24 |
Correct |
2 ms |
1356 KB |
Output is correct |
25 |
Correct |
2 ms |
1228 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1484 KB |
Output is correct |
29 |
Correct |
3 ms |
2252 KB |
Output is correct |
30 |
Correct |
1 ms |
1356 KB |
Output is correct |
31 |
Correct |
2 ms |
1740 KB |
Output is correct |
32 |
Correct |
2 ms |
1484 KB |
Output is correct |
33 |
Correct |
5 ms |
3532 KB |
Output is correct |
34 |
Correct |
4 ms |
3276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
2 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
972 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
2 ms |
1356 KB |
Output is correct |
18 |
Correct |
1 ms |
1228 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
2 ms |
1100 KB |
Output is correct |
21 |
Correct |
2 ms |
1052 KB |
Output is correct |
22 |
Correct |
2 ms |
1100 KB |
Output is correct |
23 |
Correct |
1 ms |
1228 KB |
Output is correct |
24 |
Correct |
3 ms |
1356 KB |
Output is correct |
25 |
Correct |
2 ms |
1228 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1484 KB |
Output is correct |
29 |
Correct |
3 ms |
2252 KB |
Output is correct |
30 |
Correct |
1 ms |
1356 KB |
Output is correct |
31 |
Correct |
2 ms |
1740 KB |
Output is correct |
32 |
Correct |
1 ms |
1484 KB |
Output is correct |
33 |
Correct |
5 ms |
3532 KB |
Output is correct |
34 |
Correct |
4 ms |
3276 KB |
Output is correct |
35 |
Correct |
13 ms |
3216 KB |
Output is correct |
36 |
Correct |
3 ms |
1356 KB |
Output is correct |
37 |
Correct |
12 ms |
4300 KB |
Output is correct |
38 |
Correct |
17 ms |
4172 KB |
Output is correct |
39 |
Correct |
14 ms |
4172 KB |
Output is correct |
40 |
Correct |
15 ms |
4204 KB |
Output is correct |
41 |
Correct |
16 ms |
4224 KB |
Output is correct |
42 |
Correct |
11 ms |
1412 KB |
Output is correct |
43 |
Correct |
12 ms |
1412 KB |
Output is correct |
44 |
Correct |
12 ms |
1356 KB |
Output is correct |
45 |
Correct |
33 ms |
11204 KB |
Output is correct |
46 |
Correct |
27 ms |
10352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
972 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
2 ms |
1356 KB |
Output is correct |
18 |
Correct |
1 ms |
1228 KB |
Output is correct |
19 |
Correct |
2 ms |
1100 KB |
Output is correct |
20 |
Correct |
2 ms |
1100 KB |
Output is correct |
21 |
Correct |
1 ms |
1100 KB |
Output is correct |
22 |
Correct |
1 ms |
1100 KB |
Output is correct |
23 |
Correct |
1 ms |
1228 KB |
Output is correct |
24 |
Correct |
2 ms |
1356 KB |
Output is correct |
25 |
Correct |
2 ms |
1228 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1484 KB |
Output is correct |
29 |
Correct |
3 ms |
2252 KB |
Output is correct |
30 |
Correct |
2 ms |
1356 KB |
Output is correct |
31 |
Correct |
2 ms |
1740 KB |
Output is correct |
32 |
Correct |
2 ms |
1484 KB |
Output is correct |
33 |
Correct |
6 ms |
3532 KB |
Output is correct |
34 |
Correct |
5 ms |
3276 KB |
Output is correct |
35 |
Correct |
13 ms |
3148 KB |
Output is correct |
36 |
Correct |
3 ms |
1356 KB |
Output is correct |
37 |
Correct |
11 ms |
4300 KB |
Output is correct |
38 |
Correct |
17 ms |
4224 KB |
Output is correct |
39 |
Correct |
15 ms |
4100 KB |
Output is correct |
40 |
Correct |
15 ms |
4232 KB |
Output is correct |
41 |
Correct |
18 ms |
4300 KB |
Output is correct |
42 |
Correct |
12 ms |
1356 KB |
Output is correct |
43 |
Correct |
12 ms |
1408 KB |
Output is correct |
44 |
Correct |
12 ms |
1416 KB |
Output is correct |
45 |
Correct |
41 ms |
11132 KB |
Output is correct |
46 |
Correct |
29 ms |
10304 KB |
Output is correct |
47 |
Correct |
22 ms |
13960 KB |
Output is correct |
48 |
Correct |
13 ms |
5488 KB |
Output is correct |
49 |
Correct |
11 ms |
3952 KB |
Output is correct |
50 |
Correct |
9 ms |
3916 KB |
Output is correct |
51 |
Correct |
20 ms |
7308 KB |
Output is correct |
52 |
Correct |
21 ms |
7876 KB |
Output is correct |
53 |
Correct |
15 ms |
3460 KB |
Output is correct |
54 |
Correct |
1 ms |
1740 KB |
Output is correct |
55 |
Correct |
2 ms |
2124 KB |
Output is correct |
56 |
Correct |
19 ms |
2944 KB |
Output is correct |
57 |
Correct |
7 ms |
8104 KB |
Output is correct |
58 |
Correct |
12 ms |
2764 KB |
Output is correct |
59 |
Correct |
14 ms |
3208 KB |
Output is correct |
60 |
Correct |
14 ms |
3584 KB |
Output is correct |
61 |
Correct |
14 ms |
3456 KB |
Output is correct |
62 |
Correct |
26 ms |
12036 KB |
Output is correct |
63 |
Correct |
175 ms |
48324 KB |
Output is correct |
64 |
Correct |
201 ms |
58928 KB |
Output is correct |
65 |
Correct |
457 ms |
84288 KB |
Output is correct |
66 |
Correct |
777 ms |
149600 KB |
Output is correct |
67 |
Correct |
515 ms |
135700 KB |
Output is correct |