#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=7400000;
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(lst.empty() || dis[lst.front()]>=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;
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(j!=BP[ncur].fir%BP[ncur].sec)
{
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];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
174148 KB |
Output is correct |
2 |
Correct |
98 ms |
174108 KB |
Output is correct |
3 |
Correct |
97 ms |
174096 KB |
Output is correct |
4 |
Correct |
101 ms |
174020 KB |
Output is correct |
5 |
Correct |
97 ms |
174128 KB |
Output is correct |
6 |
Correct |
93 ms |
174048 KB |
Output is correct |
7 |
Correct |
96 ms |
173980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
173992 KB |
Output is correct |
2 |
Correct |
94 ms |
174048 KB |
Output is correct |
3 |
Correct |
103 ms |
174020 KB |
Output is correct |
4 |
Correct |
94 ms |
174072 KB |
Output is correct |
5 |
Correct |
94 ms |
174040 KB |
Output is correct |
6 |
Correct |
92 ms |
173992 KB |
Output is correct |
7 |
Correct |
97 ms |
174076 KB |
Output is correct |
8 |
Correct |
100 ms |
174036 KB |
Output is correct |
9 |
Correct |
94 ms |
174096 KB |
Output is correct |
10 |
Correct |
100 ms |
174112 KB |
Output is correct |
11 |
Correct |
96 ms |
174292 KB |
Output is correct |
12 |
Correct |
96 ms |
174100 KB |
Output is correct |
13 |
Correct |
95 ms |
174044 KB |
Output is correct |
14 |
Correct |
94 ms |
174344 KB |
Output is correct |
15 |
Correct |
104 ms |
174344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
174056 KB |
Output is correct |
2 |
Correct |
95 ms |
173988 KB |
Output is correct |
3 |
Correct |
100 ms |
173992 KB |
Output is correct |
4 |
Correct |
93 ms |
174020 KB |
Output is correct |
5 |
Correct |
93 ms |
173988 KB |
Output is correct |
6 |
Correct |
93 ms |
174056 KB |
Output is correct |
7 |
Correct |
101 ms |
174084 KB |
Output is correct |
8 |
Correct |
99 ms |
173988 KB |
Output is correct |
9 |
Correct |
95 ms |
174020 KB |
Output is correct |
10 |
Correct |
98 ms |
174140 KB |
Output is correct |
11 |
Correct |
109 ms |
174388 KB |
Output is correct |
12 |
Correct |
101 ms |
174056 KB |
Output is correct |
13 |
Correct |
96 ms |
174108 KB |
Output is correct |
14 |
Correct |
100 ms |
174436 KB |
Output is correct |
15 |
Correct |
100 ms |
174404 KB |
Output is correct |
16 |
Correct |
98 ms |
174280 KB |
Output is correct |
17 |
Correct |
100 ms |
174776 KB |
Output is correct |
18 |
Correct |
105 ms |
174328 KB |
Output is correct |
19 |
Correct |
104 ms |
174280 KB |
Output is correct |
20 |
Correct |
97 ms |
174204 KB |
Output is correct |
21 |
Correct |
96 ms |
174148 KB |
Output is correct |
22 |
Correct |
98 ms |
174364 KB |
Output is correct |
23 |
Correct |
98 ms |
174404 KB |
Output is correct |
24 |
Correct |
98 ms |
174656 KB |
Output is correct |
25 |
Correct |
101 ms |
174500 KB |
Output is correct |
26 |
Correct |
102 ms |
174500 KB |
Output is correct |
27 |
Correct |
100 ms |
174564 KB |
Output is correct |
28 |
Correct |
100 ms |
175268 KB |
Output is correct |
29 |
Correct |
109 ms |
177208 KB |
Output is correct |
30 |
Correct |
99 ms |
175044 KB |
Output is correct |
31 |
Correct |
111 ms |
175760 KB |
Output is correct |
32 |
Correct |
102 ms |
175272 KB |
Output is correct |
33 |
Correct |
126 ms |
180352 KB |
Output is correct |
34 |
Correct |
138 ms |
180264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
174028 KB |
Output is correct |
2 |
Correct |
95 ms |
174052 KB |
Output is correct |
3 |
Correct |
99 ms |
174028 KB |
Output is correct |
4 |
Correct |
99 ms |
174012 KB |
Output is correct |
5 |
Correct |
95 ms |
174084 KB |
Output is correct |
6 |
Correct |
97 ms |
174084 KB |
Output is correct |
7 |
Correct |
98 ms |
174128 KB |
Output is correct |
8 |
Correct |
94 ms |
174024 KB |
Output is correct |
9 |
Correct |
100 ms |
174020 KB |
Output is correct |
10 |
Correct |
99 ms |
174084 KB |
Output is correct |
11 |
Correct |
99 ms |
174280 KB |
Output is correct |
12 |
Correct |
99 ms |
174100 KB |
Output is correct |
13 |
Correct |
112 ms |
174020 KB |
Output is correct |
14 |
Correct |
97 ms |
174316 KB |
Output is correct |
15 |
Correct |
97 ms |
174432 KB |
Output is correct |
16 |
Correct |
110 ms |
174272 KB |
Output is correct |
17 |
Correct |
108 ms |
174880 KB |
Output is correct |
18 |
Correct |
96 ms |
174444 KB |
Output is correct |
19 |
Correct |
98 ms |
174224 KB |
Output is correct |
20 |
Correct |
97 ms |
174276 KB |
Output is correct |
21 |
Correct |
95 ms |
174172 KB |
Output is correct |
22 |
Correct |
96 ms |
174384 KB |
Output is correct |
23 |
Correct |
96 ms |
174524 KB |
Output is correct |
24 |
Correct |
96 ms |
174680 KB |
Output is correct |
25 |
Correct |
115 ms |
174372 KB |
Output is correct |
26 |
Correct |
99 ms |
174596 KB |
Output is correct |
27 |
Correct |
99 ms |
174424 KB |
Output is correct |
28 |
Correct |
97 ms |
175300 KB |
Output is correct |
29 |
Correct |
103 ms |
177204 KB |
Output is correct |
30 |
Correct |
97 ms |
174920 KB |
Output is correct |
31 |
Correct |
99 ms |
175824 KB |
Output is correct |
32 |
Correct |
101 ms |
175300 KB |
Output is correct |
33 |
Correct |
121 ms |
180292 KB |
Output is correct |
34 |
Correct |
122 ms |
180248 KB |
Output is correct |
35 |
Correct |
140 ms |
179780 KB |
Output is correct |
36 |
Correct |
117 ms |
175064 KB |
Output is correct |
37 |
Correct |
158 ms |
183620 KB |
Output is correct |
38 |
Correct |
169 ms |
183032 KB |
Output is correct |
39 |
Correct |
147 ms |
183028 KB |
Output is correct |
40 |
Correct |
152 ms |
183148 KB |
Output is correct |
41 |
Correct |
173 ms |
183032 KB |
Output is correct |
42 |
Correct |
106 ms |
174640 KB |
Output is correct |
43 |
Correct |
108 ms |
174588 KB |
Output is correct |
44 |
Correct |
106 ms |
174496 KB |
Output is correct |
45 |
Correct |
294 ms |
199564 KB |
Output is correct |
46 |
Correct |
288 ms |
199500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
174044 KB |
Output is correct |
2 |
Correct |
99 ms |
174088 KB |
Output is correct |
3 |
Correct |
96 ms |
174068 KB |
Output is correct |
4 |
Correct |
97 ms |
174004 KB |
Output is correct |
5 |
Correct |
106 ms |
174020 KB |
Output is correct |
6 |
Correct |
101 ms |
174024 KB |
Output is correct |
7 |
Correct |
102 ms |
173964 KB |
Output is correct |
8 |
Correct |
98 ms |
174092 KB |
Output is correct |
9 |
Correct |
98 ms |
174008 KB |
Output is correct |
10 |
Correct |
99 ms |
174092 KB |
Output is correct |
11 |
Correct |
104 ms |
174272 KB |
Output is correct |
12 |
Correct |
105 ms |
174120 KB |
Output is correct |
13 |
Correct |
103 ms |
174176 KB |
Output is correct |
14 |
Correct |
96 ms |
174448 KB |
Output is correct |
15 |
Correct |
99 ms |
174412 KB |
Output is correct |
16 |
Correct |
101 ms |
174252 KB |
Output is correct |
17 |
Correct |
101 ms |
174800 KB |
Output is correct |
18 |
Correct |
99 ms |
174352 KB |
Output is correct |
19 |
Correct |
99 ms |
174348 KB |
Output is correct |
20 |
Correct |
101 ms |
174276 KB |
Output is correct |
21 |
Correct |
113 ms |
174100 KB |
Output is correct |
22 |
Correct |
106 ms |
174360 KB |
Output is correct |
23 |
Correct |
107 ms |
174364 KB |
Output is correct |
24 |
Correct |
111 ms |
174684 KB |
Output is correct |
25 |
Correct |
110 ms |
174376 KB |
Output is correct |
26 |
Correct |
100 ms |
174460 KB |
Output is correct |
27 |
Correct |
118 ms |
174380 KB |
Output is correct |
28 |
Correct |
116 ms |
175288 KB |
Output is correct |
29 |
Correct |
122 ms |
177280 KB |
Output is correct |
30 |
Correct |
103 ms |
174980 KB |
Output is correct |
31 |
Correct |
118 ms |
175712 KB |
Output is correct |
32 |
Correct |
99 ms |
175276 KB |
Output is correct |
33 |
Correct |
125 ms |
180332 KB |
Output is correct |
34 |
Correct |
136 ms |
180424 KB |
Output is correct |
35 |
Correct |
132 ms |
179700 KB |
Output is correct |
36 |
Correct |
104 ms |
174976 KB |
Output is correct |
37 |
Correct |
155 ms |
183540 KB |
Output is correct |
38 |
Correct |
180 ms |
182972 KB |
Output is correct |
39 |
Correct |
172 ms |
183044 KB |
Output is correct |
40 |
Correct |
153 ms |
183044 KB |
Output is correct |
41 |
Correct |
178 ms |
182988 KB |
Output is correct |
42 |
Correct |
108 ms |
174728 KB |
Output is correct |
43 |
Correct |
122 ms |
174604 KB |
Output is correct |
44 |
Correct |
112 ms |
174424 KB |
Output is correct |
45 |
Correct |
290 ms |
199524 KB |
Output is correct |
46 |
Correct |
292 ms |
199588 KB |
Output is correct |
47 |
Correct |
365 ms |
213336 KB |
Output is correct |
48 |
Correct |
149 ms |
186180 KB |
Output is correct |
49 |
Correct |
126 ms |
181604 KB |
Output is correct |
50 |
Correct |
126 ms |
181720 KB |
Output is correct |
51 |
Correct |
255 ms |
190920 KB |
Output is correct |
52 |
Correct |
217 ms |
192404 KB |
Output is correct |
53 |
Correct |
198 ms |
179132 KB |
Output is correct |
54 |
Correct |
103 ms |
175756 KB |
Output is correct |
55 |
Correct |
115 ms |
177000 KB |
Output is correct |
56 |
Correct |
117 ms |
176864 KB |
Output is correct |
57 |
Correct |
269 ms |
196756 KB |
Output is correct |
58 |
Correct |
213 ms |
177860 KB |
Output is correct |
59 |
Correct |
155 ms |
179412 KB |
Output is correct |
60 |
Correct |
147 ms |
180860 KB |
Output is correct |
61 |
Correct |
130 ms |
180000 KB |
Output is correct |
62 |
Correct |
201 ms |
200132 KB |
Output is correct |
63 |
Runtime error |
373 ms |
262148 KB |
Execution killed with signal 9 |
64 |
Halted |
0 ms |
0 KB |
- |