#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e6 +12, MOD = (int)1e9+7;
const int M = 3e4 + 21;
int n,m,b[M],ft[M];
vector<int> g[N];
vector<pair<int,int>> x[M];
void add(int v,int u) {
g[v].push_back(u);
g[u].push_back(v);
}
bool sgn(int x) {
return (x < n);
}
int d[N];
void test() {
cin >> n >> m;
for(int i = 0;i < m;i++) {
int p;
cin >> b[i] >> p;
int d = b[i] % p;
x[p].emplace_back(d,i);
}
int it = n - 1;
int num = 0;
for(int i = 1;i < n;i++){
sort(x[i].begin(),x[i].end());
for(int j = 0;j < (int)x[i].size();j++){
if(!j || x[i][j].first != x[i][j - 1].first){
num += (n - x[i][j].first) / i;
}
}
}
if(num > (int)3e4 * (int)180){
cout << -2;
return;
}
for(int i = 1;i < n;i++){
for(int j = 0;j < (int)x[i].size();j++){
if(!j || x[i][j].first != x[i][j - 1].first){
// cout << i << ' ';
for(int k = x[i][j].first;k < n;k += i){
ft[k] = ++it;
if(k - i >= 0){
add(ft[k - i],ft[k]);
}
g[ft[k]].push_back(k);
}
}
int v = b[x[i][j].second];
g[v].push_back(ft[v]);
}
// cout << '\n';
}
set<pair<int,int>> st;
for(int i = 0;i <= it;i++){
d[i] = 1e9;
}
st.insert({0,b[0]});
d[b[0]] = 0;
while(!st.empty()){
auto [c,v] = (*st.begin());
st.erase(st.begin());
for(int to:g[v]){
int w = (sgn(to) == sgn(v));
if(d[to] > d[v] + w){
st.erase({d[to],to});
d[to] = d[v] + w;
st.insert({d[to],to});
}
}
}
if(d[b[1]] == 1e9){
d[b[1]] = -1;
}
cout << d[b[1]];
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
47964 KB |
Output is correct |
2 |
Correct |
21 ms |
47960 KB |
Output is correct |
3 |
Correct |
20 ms |
47960 KB |
Output is correct |
4 |
Correct |
19 ms |
47964 KB |
Output is correct |
5 |
Correct |
19 ms |
48056 KB |
Output is correct |
6 |
Correct |
26 ms |
47932 KB |
Output is correct |
7 |
Correct |
18 ms |
47964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
47964 KB |
Output is correct |
2 |
Correct |
18 ms |
47964 KB |
Output is correct |
3 |
Correct |
18 ms |
47964 KB |
Output is correct |
4 |
Correct |
20 ms |
47960 KB |
Output is correct |
5 |
Correct |
28 ms |
47964 KB |
Output is correct |
6 |
Correct |
19 ms |
47964 KB |
Output is correct |
7 |
Correct |
19 ms |
47964 KB |
Output is correct |
8 |
Correct |
21 ms |
48152 KB |
Output is correct |
9 |
Correct |
20 ms |
47964 KB |
Output is correct |
10 |
Correct |
30 ms |
47964 KB |
Output is correct |
11 |
Correct |
24 ms |
48184 KB |
Output is correct |
12 |
Correct |
20 ms |
47964 KB |
Output is correct |
13 |
Correct |
19 ms |
48152 KB |
Output is correct |
14 |
Correct |
21 ms |
48728 KB |
Output is correct |
15 |
Correct |
25 ms |
48492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47960 KB |
Output is correct |
2 |
Correct |
21 ms |
47964 KB |
Output is correct |
3 |
Correct |
19 ms |
47964 KB |
Output is correct |
4 |
Correct |
20 ms |
47960 KB |
Output is correct |
5 |
Correct |
20 ms |
47964 KB |
Output is correct |
6 |
Correct |
20 ms |
47964 KB |
Output is correct |
7 |
Correct |
20 ms |
47964 KB |
Output is correct |
8 |
Correct |
22 ms |
47964 KB |
Output is correct |
9 |
Correct |
21 ms |
47964 KB |
Output is correct |
10 |
Correct |
21 ms |
48200 KB |
Output is correct |
11 |
Correct |
24 ms |
48220 KB |
Output is correct |
12 |
Correct |
21 ms |
47960 KB |
Output is correct |
13 |
Correct |
23 ms |
47960 KB |
Output is correct |
14 |
Correct |
22 ms |
48476 KB |
Output is correct |
15 |
Correct |
22 ms |
48456 KB |
Output is correct |
16 |
Correct |
21 ms |
48216 KB |
Output is correct |
17 |
Correct |
24 ms |
48728 KB |
Output is correct |
18 |
Correct |
20 ms |
48216 KB |
Output is correct |
19 |
Correct |
24 ms |
48220 KB |
Output is correct |
20 |
Correct |
22 ms |
48220 KB |
Output is correct |
21 |
Correct |
21 ms |
48216 KB |
Output is correct |
22 |
Correct |
23 ms |
48220 KB |
Output is correct |
23 |
Correct |
23 ms |
48272 KB |
Output is correct |
24 |
Correct |
24 ms |
48732 KB |
Output is correct |
25 |
Correct |
24 ms |
48472 KB |
Output is correct |
26 |
Correct |
23 ms |
48476 KB |
Output is correct |
27 |
Correct |
23 ms |
48300 KB |
Output is correct |
28 |
Correct |
24 ms |
48984 KB |
Output is correct |
29 |
Correct |
32 ms |
50268 KB |
Output is correct |
30 |
Correct |
25 ms |
48692 KB |
Output is correct |
31 |
Correct |
26 ms |
49244 KB |
Output is correct |
32 |
Correct |
27 ms |
49016 KB |
Output is correct |
33 |
Correct |
44 ms |
52316 KB |
Output is correct |
34 |
Correct |
43 ms |
52304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47960 KB |
Output is correct |
2 |
Correct |
21 ms |
48144 KB |
Output is correct |
3 |
Correct |
22 ms |
47960 KB |
Output is correct |
4 |
Correct |
22 ms |
48052 KB |
Output is correct |
5 |
Correct |
21 ms |
47960 KB |
Output is correct |
6 |
Correct |
22 ms |
47964 KB |
Output is correct |
7 |
Correct |
21 ms |
47964 KB |
Output is correct |
8 |
Correct |
22 ms |
47964 KB |
Output is correct |
9 |
Correct |
23 ms |
47952 KB |
Output is correct |
10 |
Correct |
22 ms |
48220 KB |
Output is correct |
11 |
Correct |
22 ms |
48292 KB |
Output is correct |
12 |
Correct |
27 ms |
47964 KB |
Output is correct |
13 |
Correct |
22 ms |
47964 KB |
Output is correct |
14 |
Correct |
22 ms |
48236 KB |
Output is correct |
15 |
Correct |
21 ms |
48476 KB |
Output is correct |
16 |
Correct |
21 ms |
48152 KB |
Output is correct |
17 |
Correct |
25 ms |
48732 KB |
Output is correct |
18 |
Correct |
22 ms |
48348 KB |
Output is correct |
19 |
Correct |
21 ms |
48128 KB |
Output is correct |
20 |
Correct |
21 ms |
48216 KB |
Output is correct |
21 |
Correct |
22 ms |
48020 KB |
Output is correct |
22 |
Correct |
21 ms |
48404 KB |
Output is correct |
23 |
Correct |
21 ms |
48216 KB |
Output is correct |
24 |
Correct |
23 ms |
48536 KB |
Output is correct |
25 |
Correct |
22 ms |
48460 KB |
Output is correct |
26 |
Correct |
23 ms |
48476 KB |
Output is correct |
27 |
Correct |
22 ms |
48476 KB |
Output is correct |
28 |
Correct |
23 ms |
48988 KB |
Output is correct |
29 |
Correct |
31 ms |
50268 KB |
Output is correct |
30 |
Correct |
28 ms |
48728 KB |
Output is correct |
31 |
Correct |
27 ms |
49240 KB |
Output is correct |
32 |
Correct |
25 ms |
48988 KB |
Output is correct |
33 |
Correct |
42 ms |
52312 KB |
Output is correct |
34 |
Correct |
42 ms |
52312 KB |
Output is correct |
35 |
Correct |
45 ms |
53072 KB |
Output is correct |
36 |
Correct |
23 ms |
48728 KB |
Output is correct |
37 |
Correct |
61 ms |
55900 KB |
Output is correct |
38 |
Correct |
70 ms |
56148 KB |
Output is correct |
39 |
Correct |
62 ms |
55888 KB |
Output is correct |
40 |
Correct |
64 ms |
55896 KB |
Output is correct |
41 |
Correct |
68 ms |
56008 KB |
Output is correct |
42 |
Correct |
26 ms |
49116 KB |
Output is correct |
43 |
Correct |
24 ms |
48852 KB |
Output is correct |
44 |
Correct |
22 ms |
48860 KB |
Output is correct |
45 |
Correct |
113 ms |
67156 KB |
Output is correct |
46 |
Correct |
108 ms |
67156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47964 KB |
Output is correct |
2 |
Correct |
20 ms |
47960 KB |
Output is correct |
3 |
Correct |
19 ms |
47960 KB |
Output is correct |
4 |
Correct |
18 ms |
48152 KB |
Output is correct |
5 |
Correct |
19 ms |
48016 KB |
Output is correct |
6 |
Correct |
19 ms |
48148 KB |
Output is correct |
7 |
Correct |
19 ms |
47904 KB |
Output is correct |
8 |
Correct |
19 ms |
47964 KB |
Output is correct |
9 |
Correct |
19 ms |
47960 KB |
Output is correct |
10 |
Correct |
19 ms |
48220 KB |
Output is correct |
11 |
Correct |
21 ms |
48220 KB |
Output is correct |
12 |
Correct |
22 ms |
47960 KB |
Output is correct |
13 |
Correct |
22 ms |
48148 KB |
Output is correct |
14 |
Correct |
25 ms |
48404 KB |
Output is correct |
15 |
Correct |
22 ms |
48472 KB |
Output is correct |
16 |
Correct |
20 ms |
48220 KB |
Output is correct |
17 |
Correct |
23 ms |
48732 KB |
Output is correct |
18 |
Correct |
20 ms |
48408 KB |
Output is correct |
19 |
Correct |
20 ms |
48220 KB |
Output is correct |
20 |
Correct |
20 ms |
48220 KB |
Output is correct |
21 |
Correct |
20 ms |
48172 KB |
Output is correct |
22 |
Correct |
19 ms |
48220 KB |
Output is correct |
23 |
Correct |
20 ms |
48220 KB |
Output is correct |
24 |
Correct |
23 ms |
48616 KB |
Output is correct |
25 |
Correct |
22 ms |
48524 KB |
Output is correct |
26 |
Correct |
22 ms |
48476 KB |
Output is correct |
27 |
Correct |
21 ms |
48368 KB |
Output is correct |
28 |
Correct |
23 ms |
48964 KB |
Output is correct |
29 |
Correct |
30 ms |
50124 KB |
Output is correct |
30 |
Correct |
23 ms |
48732 KB |
Output is correct |
31 |
Correct |
26 ms |
49320 KB |
Output is correct |
32 |
Correct |
24 ms |
48808 KB |
Output is correct |
33 |
Correct |
46 ms |
52420 KB |
Output is correct |
34 |
Correct |
38 ms |
52304 KB |
Output is correct |
35 |
Correct |
56 ms |
53076 KB |
Output is correct |
36 |
Correct |
25 ms |
48732 KB |
Output is correct |
37 |
Correct |
63 ms |
55888 KB |
Output is correct |
38 |
Correct |
63 ms |
56148 KB |
Output is correct |
39 |
Correct |
70 ms |
55888 KB |
Output is correct |
40 |
Correct |
67 ms |
55888 KB |
Output is correct |
41 |
Correct |
66 ms |
55892 KB |
Output is correct |
42 |
Correct |
25 ms |
49116 KB |
Output is correct |
43 |
Correct |
24 ms |
48856 KB |
Output is correct |
44 |
Correct |
24 ms |
48860 KB |
Output is correct |
45 |
Correct |
119 ms |
67304 KB |
Output is correct |
46 |
Correct |
118 ms |
67300 KB |
Output is correct |
47 |
Correct |
215 ms |
76880 KB |
Output is correct |
48 |
Correct |
40 ms |
57684 KB |
Output is correct |
49 |
Correct |
36 ms |
54260 KB |
Output is correct |
50 |
Correct |
33 ms |
54100 KB |
Output is correct |
51 |
Correct |
95 ms |
61520 KB |
Output is correct |
52 |
Correct |
120 ms |
62380 KB |
Output is correct |
53 |
Correct |
40 ms |
52816 KB |
Output is correct |
54 |
Correct |
24 ms |
49244 KB |
Output is correct |
55 |
Correct |
27 ms |
50268 KB |
Output is correct |
56 |
Correct |
31 ms |
51156 KB |
Output is correct |
57 |
Correct |
129 ms |
63544 KB |
Output is correct |
58 |
Correct |
35 ms |
51168 KB |
Output is correct |
59 |
Correct |
36 ms |
52440 KB |
Output is correct |
60 |
Correct |
42 ms |
53212 KB |
Output is correct |
61 |
Correct |
39 ms |
52940 KB |
Output is correct |
62 |
Correct |
82 ms |
67156 KB |
Output is correct |
63 |
Runtime error |
226 ms |
223828 KB |
Execution killed with signal 11 |
64 |
Halted |
0 ms |
0 KB |
- |