#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=2000000;
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 |
26 ms |
47268 KB |
Output is correct |
2 |
Correct |
27 ms |
47240 KB |
Output is correct |
3 |
Correct |
27 ms |
47300 KB |
Output is correct |
4 |
Correct |
27 ms |
47172 KB |
Output is correct |
5 |
Correct |
26 ms |
47180 KB |
Output is correct |
6 |
Correct |
29 ms |
47180 KB |
Output is correct |
7 |
Correct |
27 ms |
47180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
47224 KB |
Output is correct |
2 |
Correct |
30 ms |
47280 KB |
Output is correct |
3 |
Correct |
27 ms |
47208 KB |
Output is correct |
4 |
Correct |
27 ms |
47180 KB |
Output is correct |
5 |
Correct |
29 ms |
47184 KB |
Output is correct |
6 |
Correct |
28 ms |
47280 KB |
Output is correct |
7 |
Correct |
28 ms |
47244 KB |
Output is correct |
8 |
Correct |
30 ms |
47180 KB |
Output is correct |
9 |
Correct |
27 ms |
47212 KB |
Output is correct |
10 |
Correct |
27 ms |
47308 KB |
Output is correct |
11 |
Correct |
27 ms |
47480 KB |
Output is correct |
12 |
Correct |
27 ms |
47300 KB |
Output is correct |
13 |
Correct |
26 ms |
47204 KB |
Output is correct |
14 |
Correct |
32 ms |
47680 KB |
Output is correct |
15 |
Correct |
30 ms |
47568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
47268 KB |
Output is correct |
2 |
Correct |
27 ms |
47188 KB |
Output is correct |
3 |
Correct |
26 ms |
47280 KB |
Output is correct |
4 |
Correct |
27 ms |
47208 KB |
Output is correct |
5 |
Correct |
28 ms |
47248 KB |
Output is correct |
6 |
Correct |
27 ms |
47288 KB |
Output is correct |
7 |
Correct |
27 ms |
47220 KB |
Output is correct |
8 |
Correct |
27 ms |
47220 KB |
Output is correct |
9 |
Correct |
28 ms |
47188 KB |
Output is correct |
10 |
Correct |
27 ms |
47376 KB |
Output is correct |
11 |
Correct |
30 ms |
47476 KB |
Output is correct |
12 |
Correct |
27 ms |
47264 KB |
Output is correct |
13 |
Correct |
28 ms |
47300 KB |
Output is correct |
14 |
Correct |
29 ms |
47640 KB |
Output is correct |
15 |
Correct |
32 ms |
47644 KB |
Output is correct |
16 |
Correct |
28 ms |
47472 KB |
Output is correct |
17 |
Correct |
30 ms |
48012 KB |
Output is correct |
18 |
Correct |
29 ms |
47624 KB |
Output is correct |
19 |
Correct |
28 ms |
47540 KB |
Output is correct |
20 |
Correct |
28 ms |
47488 KB |
Output is correct |
21 |
Correct |
29 ms |
47344 KB |
Output is correct |
22 |
Correct |
28 ms |
47564 KB |
Output is correct |
23 |
Correct |
28 ms |
47564 KB |
Output is correct |
24 |
Correct |
31 ms |
47844 KB |
Output is correct |
25 |
Correct |
30 ms |
47700 KB |
Output is correct |
26 |
Correct |
28 ms |
47684 KB |
Output is correct |
27 |
Correct |
31 ms |
47668 KB |
Output is correct |
28 |
Correct |
32 ms |
48468 KB |
Output is correct |
29 |
Correct |
38 ms |
50444 KB |
Output is correct |
30 |
Correct |
29 ms |
48204 KB |
Output is correct |
31 |
Correct |
36 ms |
49028 KB |
Output is correct |
32 |
Correct |
31 ms |
48460 KB |
Output is correct |
33 |
Correct |
53 ms |
53436 KB |
Output is correct |
34 |
Correct |
53 ms |
53548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
47172 KB |
Output is correct |
2 |
Correct |
25 ms |
47180 KB |
Output is correct |
3 |
Correct |
28 ms |
47188 KB |
Output is correct |
4 |
Correct |
28 ms |
47288 KB |
Output is correct |
5 |
Correct |
27 ms |
47180 KB |
Output is correct |
6 |
Correct |
26 ms |
47180 KB |
Output is correct |
7 |
Correct |
27 ms |
47160 KB |
Output is correct |
8 |
Correct |
27 ms |
47156 KB |
Output is correct |
9 |
Correct |
27 ms |
47180 KB |
Output is correct |
10 |
Correct |
27 ms |
47308 KB |
Output is correct |
11 |
Correct |
28 ms |
47504 KB |
Output is correct |
12 |
Correct |
28 ms |
47308 KB |
Output is correct |
13 |
Correct |
27 ms |
47320 KB |
Output is correct |
14 |
Correct |
28 ms |
47564 KB |
Output is correct |
15 |
Correct |
29 ms |
47528 KB |
Output is correct |
16 |
Correct |
33 ms |
47368 KB |
Output is correct |
17 |
Correct |
30 ms |
48028 KB |
Output is correct |
18 |
Correct |
32 ms |
47556 KB |
Output is correct |
19 |
Correct |
32 ms |
47544 KB |
Output is correct |
20 |
Correct |
28 ms |
47420 KB |
Output is correct |
21 |
Correct |
28 ms |
47368 KB |
Output is correct |
22 |
Correct |
28 ms |
47580 KB |
Output is correct |
23 |
Correct |
28 ms |
47564 KB |
Output is correct |
24 |
Correct |
29 ms |
47892 KB |
Output is correct |
25 |
Correct |
28 ms |
47596 KB |
Output is correct |
26 |
Correct |
31 ms |
47628 KB |
Output is correct |
27 |
Correct |
29 ms |
47652 KB |
Output is correct |
28 |
Correct |
31 ms |
48484 KB |
Output is correct |
29 |
Correct |
39 ms |
50380 KB |
Output is correct |
30 |
Correct |
29 ms |
48092 KB |
Output is correct |
31 |
Correct |
33 ms |
49016 KB |
Output is correct |
32 |
Correct |
30 ms |
48460 KB |
Output is correct |
33 |
Correct |
53 ms |
53524 KB |
Output is correct |
34 |
Correct |
52 ms |
53540 KB |
Output is correct |
35 |
Correct |
70 ms |
52952 KB |
Output is correct |
36 |
Correct |
31 ms |
48152 KB |
Output is correct |
37 |
Correct |
83 ms |
56772 KB |
Output is correct |
38 |
Correct |
81 ms |
56144 KB |
Output is correct |
39 |
Correct |
87 ms |
56216 KB |
Output is correct |
40 |
Correct |
82 ms |
56144 KB |
Output is correct |
41 |
Correct |
85 ms |
56220 KB |
Output is correct |
42 |
Correct |
38 ms |
47812 KB |
Output is correct |
43 |
Correct |
42 ms |
47784 KB |
Output is correct |
44 |
Correct |
38 ms |
47628 KB |
Output is correct |
45 |
Correct |
196 ms |
72792 KB |
Output is correct |
46 |
Correct |
203 ms |
72692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
47268 KB |
Output is correct |
2 |
Correct |
27 ms |
47208 KB |
Output is correct |
3 |
Correct |
27 ms |
47180 KB |
Output is correct |
4 |
Correct |
27 ms |
47296 KB |
Output is correct |
5 |
Correct |
27 ms |
47288 KB |
Output is correct |
6 |
Correct |
27 ms |
47152 KB |
Output is correct |
7 |
Correct |
30 ms |
47288 KB |
Output is correct |
8 |
Correct |
27 ms |
47240 KB |
Output is correct |
9 |
Correct |
27 ms |
47224 KB |
Output is correct |
10 |
Correct |
27 ms |
47332 KB |
Output is correct |
11 |
Correct |
29 ms |
47520 KB |
Output is correct |
12 |
Correct |
28 ms |
47212 KB |
Output is correct |
13 |
Correct |
27 ms |
47276 KB |
Output is correct |
14 |
Correct |
28 ms |
47556 KB |
Output is correct |
15 |
Correct |
28 ms |
47536 KB |
Output is correct |
16 |
Correct |
27 ms |
47384 KB |
Output is correct |
17 |
Correct |
30 ms |
47992 KB |
Output is correct |
18 |
Correct |
29 ms |
47564 KB |
Output is correct |
19 |
Correct |
30 ms |
47504 KB |
Output is correct |
20 |
Correct |
28 ms |
47356 KB |
Output is correct |
21 |
Correct |
27 ms |
47300 KB |
Output is correct |
22 |
Correct |
27 ms |
47584 KB |
Output is correct |
23 |
Correct |
28 ms |
47540 KB |
Output is correct |
24 |
Correct |
30 ms |
47884 KB |
Output is correct |
25 |
Correct |
31 ms |
47584 KB |
Output is correct |
26 |
Correct |
29 ms |
47692 KB |
Output is correct |
27 |
Correct |
34 ms |
47564 KB |
Output is correct |
28 |
Correct |
35 ms |
48508 KB |
Output is correct |
29 |
Correct |
37 ms |
50480 KB |
Output is correct |
30 |
Correct |
30 ms |
48204 KB |
Output is correct |
31 |
Correct |
33 ms |
49028 KB |
Output is correct |
32 |
Correct |
31 ms |
48512 KB |
Output is correct |
33 |
Correct |
53 ms |
53544 KB |
Output is correct |
34 |
Correct |
52 ms |
53548 KB |
Output is correct |
35 |
Correct |
60 ms |
52984 KB |
Output is correct |
36 |
Correct |
32 ms |
48252 KB |
Output is correct |
37 |
Correct |
98 ms |
56796 KB |
Output is correct |
38 |
Correct |
79 ms |
56184 KB |
Output is correct |
39 |
Correct |
79 ms |
56236 KB |
Output is correct |
40 |
Correct |
85 ms |
56132 KB |
Output is correct |
41 |
Correct |
83 ms |
56320 KB |
Output is correct |
42 |
Correct |
40 ms |
47896 KB |
Output is correct |
43 |
Correct |
39 ms |
47812 KB |
Output is correct |
44 |
Correct |
39 ms |
47656 KB |
Output is correct |
45 |
Correct |
201 ms |
72736 KB |
Output is correct |
46 |
Correct |
199 ms |
72764 KB |
Output is correct |
47 |
Correct |
289 ms |
86520 KB |
Output is correct |
48 |
Correct |
66 ms |
59376 KB |
Output is correct |
49 |
Correct |
53 ms |
54724 KB |
Output is correct |
50 |
Correct |
54 ms |
54716 KB |
Output is correct |
51 |
Correct |
137 ms |
64180 KB |
Output is correct |
52 |
Correct |
134 ms |
65564 KB |
Output is correct |
53 |
Correct |
69 ms |
52408 KB |
Output is correct |
54 |
Correct |
32 ms |
48972 KB |
Output is correct |
55 |
Correct |
36 ms |
50152 KB |
Output is correct |
56 |
Correct |
51 ms |
50124 KB |
Output is correct |
57 |
Correct |
154 ms |
69924 KB |
Output is correct |
58 |
Correct |
46 ms |
51028 KB |
Output is correct |
59 |
Correct |
54 ms |
52572 KB |
Output is correct |
60 |
Correct |
62 ms |
54040 KB |
Output is correct |
61 |
Correct |
67 ms |
53276 KB |
Output is correct |
62 |
Correct |
151 ms |
73412 KB |
Output is correct |
63 |
Incorrect |
323 ms |
139480 KB |
Output isn't correct |
64 |
Halted |
0 ms |
0 KB |
- |