#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=3000000;
const int mxK=100001;
const int MOD=1000000007;
const ll INF=9223372036854775807;
int N, M;
vector <pii> BP;
vector <pair<int, bool>> adj[mxM];
int idx;
int X, Y;
list <int> lst;
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(!lst.empty())
{
int now=lst.front();
lst.pop_front();
if(Chk[now]) continue;
Chk[now]=true;
for(auto nxt : adj[now])
{
if(dis[nxt.fir]>dis[now]+nxt.sec)
{
dis[nxt.fir]=dis[now]+nxt.sec;
if(dis[now]==dis[nxt.fir]) lst.push_front(nxt.fir);
else lst.push_back(nxt.fir);
}
}
}
}
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);
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)
{
adj[j].push_back({idx, 0});
while(cur<M && BP[cur].fir==j && BP[cur].sec==BP[ncur].sec) cur++;
}
adj[idx].push_back({j, 0});
if(!jog) jog=true;
else
{
adj[idx].push_back({idx-1, 1});
adj[idx-1].push_back({idx, 1});
}
idx++;
if(idx>=mxM) return 0;
}
}
for(int i=0;i<idx;i++)
{
dis[i]=MOD;
}
dis[X]=0;
lst.push_front(X);
zero_one_bfs();
if(dis[Y]==MOD) cout << -1;
else cout << dis[Y];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
70752 KB |
Output is correct |
2 |
Correct |
38 ms |
70748 KB |
Output is correct |
3 |
Correct |
39 ms |
70756 KB |
Output is correct |
4 |
Correct |
39 ms |
70700 KB |
Output is correct |
5 |
Correct |
54 ms |
70700 KB |
Output is correct |
6 |
Correct |
45 ms |
70664 KB |
Output is correct |
7 |
Correct |
44 ms |
70732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
70756 KB |
Output is correct |
2 |
Correct |
40 ms |
70724 KB |
Output is correct |
3 |
Correct |
41 ms |
70720 KB |
Output is correct |
4 |
Correct |
45 ms |
70656 KB |
Output is correct |
5 |
Correct |
39 ms |
70764 KB |
Output is correct |
6 |
Correct |
39 ms |
70720 KB |
Output is correct |
7 |
Correct |
38 ms |
70712 KB |
Output is correct |
8 |
Correct |
39 ms |
70688 KB |
Output is correct |
9 |
Correct |
39 ms |
70792 KB |
Output is correct |
10 |
Correct |
42 ms |
70796 KB |
Output is correct |
11 |
Correct |
43 ms |
71020 KB |
Output is correct |
12 |
Correct |
45 ms |
70760 KB |
Output is correct |
13 |
Correct |
57 ms |
70740 KB |
Output is correct |
14 |
Correct |
42 ms |
71096 KB |
Output is correct |
15 |
Correct |
45 ms |
71088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
70636 KB |
Output is correct |
2 |
Correct |
39 ms |
70732 KB |
Output is correct |
3 |
Correct |
42 ms |
70708 KB |
Output is correct |
4 |
Correct |
44 ms |
70744 KB |
Output is correct |
5 |
Correct |
45 ms |
70752 KB |
Output is correct |
6 |
Correct |
49 ms |
70688 KB |
Output is correct |
7 |
Correct |
47 ms |
70700 KB |
Output is correct |
8 |
Correct |
50 ms |
70724 KB |
Output is correct |
9 |
Correct |
49 ms |
70732 KB |
Output is correct |
10 |
Correct |
70 ms |
70804 KB |
Output is correct |
11 |
Correct |
43 ms |
70912 KB |
Output is correct |
12 |
Correct |
42 ms |
70764 KB |
Output is correct |
13 |
Correct |
47 ms |
70728 KB |
Output is correct |
14 |
Correct |
44 ms |
71004 KB |
Output is correct |
15 |
Correct |
45 ms |
71028 KB |
Output is correct |
16 |
Correct |
38 ms |
70860 KB |
Output is correct |
17 |
Correct |
43 ms |
71500 KB |
Output is correct |
18 |
Correct |
48 ms |
71104 KB |
Output is correct |
19 |
Correct |
46 ms |
71040 KB |
Output is correct |
20 |
Correct |
41 ms |
70860 KB |
Output is correct |
21 |
Correct |
40 ms |
70836 KB |
Output is correct |
22 |
Correct |
40 ms |
71056 KB |
Output is correct |
23 |
Correct |
42 ms |
71012 KB |
Output is correct |
24 |
Correct |
44 ms |
71316 KB |
Output is correct |
25 |
Correct |
41 ms |
71108 KB |
Output is correct |
26 |
Correct |
42 ms |
71172 KB |
Output is correct |
27 |
Correct |
41 ms |
71036 KB |
Output is correct |
28 |
Correct |
46 ms |
71884 KB |
Output is correct |
29 |
Correct |
60 ms |
73900 KB |
Output is correct |
30 |
Correct |
48 ms |
71556 KB |
Output is correct |
31 |
Correct |
48 ms |
72500 KB |
Output is correct |
32 |
Correct |
47 ms |
71948 KB |
Output is correct |
33 |
Correct |
65 ms |
76996 KB |
Output is correct |
34 |
Correct |
62 ms |
76916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
70732 KB |
Output is correct |
2 |
Correct |
39 ms |
70656 KB |
Output is correct |
3 |
Correct |
40 ms |
70772 KB |
Output is correct |
4 |
Correct |
86 ms |
70752 KB |
Output is correct |
5 |
Correct |
47 ms |
70756 KB |
Output is correct |
6 |
Correct |
42 ms |
70704 KB |
Output is correct |
7 |
Correct |
44 ms |
70752 KB |
Output is correct |
8 |
Correct |
40 ms |
70636 KB |
Output is correct |
9 |
Correct |
38 ms |
70784 KB |
Output is correct |
10 |
Correct |
39 ms |
70744 KB |
Output is correct |
11 |
Correct |
51 ms |
70968 KB |
Output is correct |
12 |
Correct |
40 ms |
70732 KB |
Output is correct |
13 |
Correct |
39 ms |
70708 KB |
Output is correct |
14 |
Correct |
52 ms |
71100 KB |
Output is correct |
15 |
Correct |
40 ms |
70980 KB |
Output is correct |
16 |
Correct |
43 ms |
70944 KB |
Output is correct |
17 |
Correct |
51 ms |
71476 KB |
Output is correct |
18 |
Correct |
42 ms |
71080 KB |
Output is correct |
19 |
Correct |
43 ms |
70960 KB |
Output is correct |
20 |
Correct |
43 ms |
70852 KB |
Output is correct |
21 |
Correct |
40 ms |
70780 KB |
Output is correct |
22 |
Correct |
39 ms |
71060 KB |
Output is correct |
23 |
Correct |
40 ms |
70992 KB |
Output is correct |
24 |
Correct |
42 ms |
71364 KB |
Output is correct |
25 |
Correct |
42 ms |
71160 KB |
Output is correct |
26 |
Correct |
42 ms |
71136 KB |
Output is correct |
27 |
Correct |
59 ms |
71076 KB |
Output is correct |
28 |
Correct |
43 ms |
71876 KB |
Output is correct |
29 |
Correct |
51 ms |
73868 KB |
Output is correct |
30 |
Correct |
42 ms |
71620 KB |
Output is correct |
31 |
Correct |
49 ms |
72472 KB |
Output is correct |
32 |
Correct |
51 ms |
71912 KB |
Output is correct |
33 |
Correct |
65 ms |
76908 KB |
Output is correct |
34 |
Correct |
69 ms |
76916 KB |
Output is correct |
35 |
Correct |
88 ms |
76492 KB |
Output is correct |
36 |
Correct |
56 ms |
71616 KB |
Output is correct |
37 |
Correct |
123 ms |
80248 KB |
Output is correct |
38 |
Correct |
97 ms |
79684 KB |
Output is correct |
39 |
Correct |
96 ms |
79696 KB |
Output is correct |
40 |
Correct |
92 ms |
79708 KB |
Output is correct |
41 |
Correct |
94 ms |
79684 KB |
Output is correct |
42 |
Correct |
52 ms |
71400 KB |
Output is correct |
43 |
Correct |
51 ms |
71264 KB |
Output is correct |
44 |
Correct |
54 ms |
71112 KB |
Output is correct |
45 |
Correct |
225 ms |
96208 KB |
Output is correct |
46 |
Correct |
224 ms |
96232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
70660 KB |
Output is correct |
2 |
Correct |
38 ms |
70680 KB |
Output is correct |
3 |
Correct |
61 ms |
70756 KB |
Output is correct |
4 |
Correct |
40 ms |
70752 KB |
Output is correct |
5 |
Correct |
39 ms |
70672 KB |
Output is correct |
6 |
Correct |
39 ms |
70752 KB |
Output is correct |
7 |
Correct |
38 ms |
70772 KB |
Output is correct |
8 |
Correct |
51 ms |
70728 KB |
Output is correct |
9 |
Correct |
50 ms |
70676 KB |
Output is correct |
10 |
Correct |
39 ms |
70732 KB |
Output is correct |
11 |
Correct |
42 ms |
70972 KB |
Output is correct |
12 |
Correct |
44 ms |
70716 KB |
Output is correct |
13 |
Correct |
40 ms |
70728 KB |
Output is correct |
14 |
Correct |
48 ms |
71068 KB |
Output is correct |
15 |
Correct |
48 ms |
71100 KB |
Output is correct |
16 |
Correct |
46 ms |
70948 KB |
Output is correct |
17 |
Correct |
54 ms |
71496 KB |
Output is correct |
18 |
Correct |
40 ms |
71040 KB |
Output is correct |
19 |
Correct |
49 ms |
70928 KB |
Output is correct |
20 |
Correct |
45 ms |
70932 KB |
Output is correct |
21 |
Correct |
46 ms |
70820 KB |
Output is correct |
22 |
Correct |
43 ms |
71056 KB |
Output is correct |
23 |
Correct |
42 ms |
71072 KB |
Output is correct |
24 |
Correct |
42 ms |
71428 KB |
Output is correct |
25 |
Correct |
40 ms |
71104 KB |
Output is correct |
26 |
Correct |
87 ms |
71164 KB |
Output is correct |
27 |
Correct |
40 ms |
71108 KB |
Output is correct |
28 |
Correct |
45 ms |
71996 KB |
Output is correct |
29 |
Correct |
56 ms |
73924 KB |
Output is correct |
30 |
Correct |
42 ms |
71596 KB |
Output is correct |
31 |
Correct |
65 ms |
72456 KB |
Output is correct |
32 |
Correct |
43 ms |
71904 KB |
Output is correct |
33 |
Correct |
65 ms |
77044 KB |
Output is correct |
34 |
Correct |
99 ms |
76952 KB |
Output is correct |
35 |
Correct |
71 ms |
76484 KB |
Output is correct |
36 |
Correct |
46 ms |
71620 KB |
Output is correct |
37 |
Correct |
92 ms |
80276 KB |
Output is correct |
38 |
Correct |
93 ms |
79688 KB |
Output is correct |
39 |
Correct |
104 ms |
79772 KB |
Output is correct |
40 |
Correct |
113 ms |
79700 KB |
Output is correct |
41 |
Correct |
101 ms |
79628 KB |
Output is correct |
42 |
Correct |
53 ms |
71396 KB |
Output is correct |
43 |
Correct |
75 ms |
71336 KB |
Output is correct |
44 |
Correct |
48 ms |
71076 KB |
Output is correct |
45 |
Correct |
220 ms |
96212 KB |
Output is correct |
46 |
Correct |
210 ms |
96196 KB |
Output is correct |
47 |
Correct |
304 ms |
110012 KB |
Output is correct |
48 |
Correct |
77 ms |
82728 KB |
Output is correct |
49 |
Correct |
66 ms |
78252 KB |
Output is correct |
50 |
Correct |
70 ms |
78200 KB |
Output is correct |
51 |
Correct |
140 ms |
87584 KB |
Output is correct |
52 |
Correct |
157 ms |
89192 KB |
Output is correct |
53 |
Correct |
73 ms |
75872 KB |
Output is correct |
54 |
Correct |
44 ms |
72400 KB |
Output is correct |
55 |
Correct |
49 ms |
73800 KB |
Output is correct |
56 |
Correct |
64 ms |
73572 KB |
Output is correct |
57 |
Correct |
215 ms |
93528 KB |
Output is correct |
58 |
Correct |
70 ms |
74564 KB |
Output is correct |
59 |
Correct |
83 ms |
76124 KB |
Output is correct |
60 |
Correct |
81 ms |
77524 KB |
Output is correct |
61 |
Correct |
72 ms |
76696 KB |
Output is correct |
62 |
Correct |
141 ms |
96876 KB |
Output is correct |
63 |
Correct |
906 ms |
189400 KB |
Output is correct |
64 |
Execution timed out |
1096 ms |
216252 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |