#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5226152+12, MOD = (int)1e9+7;
const int M = 3e4 + 21;
short n,m,b[M];
int ft[M];
vector<int> g[N];
vector<pair<short,short>> 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;
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]);
}
}
deque<int> q;
for(int i = 0;i <= it;i++){
d[i] = 1e9;
}
d[b[0]] = 0;
q.push_front(b[0]);
while(!q.empty()){
int v = q[0];
q.pop_front();
for(int to:g[v]){
int w = (sgn(v) == sgn(to));
if(d[to] > d[v] + w){
d[to] = d[v] + w;
if(w == 0){
q.push_front(to);
}else{
q.push_back(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();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
123732 KB |
Output is correct |
2 |
Correct |
58 ms |
124032 KB |
Output is correct |
3 |
Correct |
51 ms |
123724 KB |
Output is correct |
4 |
Correct |
54 ms |
123732 KB |
Output is correct |
5 |
Correct |
53 ms |
123732 KB |
Output is correct |
6 |
Correct |
54 ms |
123732 KB |
Output is correct |
7 |
Correct |
52 ms |
123732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
123736 KB |
Output is correct |
2 |
Correct |
54 ms |
123732 KB |
Output is correct |
3 |
Correct |
54 ms |
123728 KB |
Output is correct |
4 |
Correct |
55 ms |
123732 KB |
Output is correct |
5 |
Correct |
54 ms |
123736 KB |
Output is correct |
6 |
Correct |
61 ms |
123988 KB |
Output is correct |
7 |
Correct |
53 ms |
123736 KB |
Output is correct |
8 |
Correct |
52 ms |
123808 KB |
Output is correct |
9 |
Correct |
54 ms |
123732 KB |
Output is correct |
10 |
Correct |
51 ms |
123828 KB |
Output is correct |
11 |
Correct |
54 ms |
123988 KB |
Output is correct |
12 |
Correct |
53 ms |
123736 KB |
Output is correct |
13 |
Correct |
53 ms |
123892 KB |
Output is correct |
14 |
Correct |
53 ms |
123984 KB |
Output is correct |
15 |
Correct |
53 ms |
123984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
123696 KB |
Output is correct |
2 |
Correct |
52 ms |
123884 KB |
Output is correct |
3 |
Correct |
52 ms |
123732 KB |
Output is correct |
4 |
Correct |
52 ms |
123736 KB |
Output is correct |
5 |
Correct |
52 ms |
123728 KB |
Output is correct |
6 |
Correct |
54 ms |
123728 KB |
Output is correct |
7 |
Correct |
53 ms |
123732 KB |
Output is correct |
8 |
Correct |
64 ms |
123776 KB |
Output is correct |
9 |
Correct |
49 ms |
123740 KB |
Output is correct |
10 |
Correct |
57 ms |
123736 KB |
Output is correct |
11 |
Correct |
55 ms |
123984 KB |
Output is correct |
12 |
Correct |
53 ms |
123904 KB |
Output is correct |
13 |
Correct |
53 ms |
123812 KB |
Output is correct |
14 |
Correct |
54 ms |
123988 KB |
Output is correct |
15 |
Correct |
56 ms |
123988 KB |
Output is correct |
16 |
Correct |
56 ms |
123808 KB |
Output is correct |
17 |
Correct |
71 ms |
124320 KB |
Output is correct |
18 |
Correct |
52 ms |
123984 KB |
Output is correct |
19 |
Correct |
55 ms |
123988 KB |
Output is correct |
20 |
Correct |
56 ms |
123980 KB |
Output is correct |
21 |
Correct |
53 ms |
123984 KB |
Output is correct |
22 |
Correct |
55 ms |
123988 KB |
Output is correct |
23 |
Correct |
51 ms |
123988 KB |
Output is correct |
24 |
Correct |
56 ms |
124200 KB |
Output is correct |
25 |
Correct |
58 ms |
124248 KB |
Output is correct |
26 |
Correct |
55 ms |
123984 KB |
Output is correct |
27 |
Correct |
54 ms |
123984 KB |
Output is correct |
28 |
Correct |
54 ms |
124764 KB |
Output is correct |
29 |
Correct |
60 ms |
126036 KB |
Output is correct |
30 |
Correct |
59 ms |
124500 KB |
Output is correct |
31 |
Correct |
56 ms |
125012 KB |
Output is correct |
32 |
Correct |
54 ms |
124736 KB |
Output is correct |
33 |
Correct |
62 ms |
128080 KB |
Output is correct |
34 |
Correct |
67 ms |
127956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
123772 KB |
Output is correct |
2 |
Correct |
54 ms |
123700 KB |
Output is correct |
3 |
Correct |
54 ms |
123728 KB |
Output is correct |
4 |
Correct |
55 ms |
123640 KB |
Output is correct |
5 |
Correct |
54 ms |
123980 KB |
Output is correct |
6 |
Correct |
52 ms |
123728 KB |
Output is correct |
7 |
Correct |
59 ms |
123668 KB |
Output is correct |
8 |
Correct |
53 ms |
123708 KB |
Output is correct |
9 |
Correct |
54 ms |
123868 KB |
Output is correct |
10 |
Correct |
55 ms |
123728 KB |
Output is correct |
11 |
Correct |
54 ms |
123992 KB |
Output is correct |
12 |
Correct |
56 ms |
123984 KB |
Output is correct |
13 |
Correct |
54 ms |
123732 KB |
Output is correct |
14 |
Correct |
55 ms |
123984 KB |
Output is correct |
15 |
Correct |
52 ms |
123920 KB |
Output is correct |
16 |
Correct |
54 ms |
124008 KB |
Output is correct |
17 |
Correct |
55 ms |
124496 KB |
Output is correct |
18 |
Correct |
55 ms |
123984 KB |
Output is correct |
19 |
Correct |
57 ms |
123988 KB |
Output is correct |
20 |
Correct |
55 ms |
123884 KB |
Output is correct |
21 |
Correct |
53 ms |
123988 KB |
Output is correct |
22 |
Correct |
54 ms |
124116 KB |
Output is correct |
23 |
Correct |
54 ms |
123988 KB |
Output is correct |
24 |
Correct |
57 ms |
124240 KB |
Output is correct |
25 |
Correct |
52 ms |
124140 KB |
Output is correct |
26 |
Correct |
54 ms |
124148 KB |
Output is correct |
27 |
Correct |
54 ms |
124012 KB |
Output is correct |
28 |
Correct |
55 ms |
124752 KB |
Output is correct |
29 |
Correct |
57 ms |
126036 KB |
Output is correct |
30 |
Correct |
63 ms |
124376 KB |
Output is correct |
31 |
Correct |
55 ms |
124888 KB |
Output is correct |
32 |
Correct |
54 ms |
124500 KB |
Output is correct |
33 |
Correct |
64 ms |
127932 KB |
Output is correct |
34 |
Correct |
60 ms |
128084 KB |
Output is correct |
35 |
Correct |
69 ms |
128380 KB |
Output is correct |
36 |
Correct |
53 ms |
124356 KB |
Output is correct |
37 |
Correct |
81 ms |
132432 KB |
Output is correct |
38 |
Correct |
74 ms |
131480 KB |
Output is correct |
39 |
Correct |
73 ms |
131668 KB |
Output is correct |
40 |
Correct |
115 ms |
131408 KB |
Output is correct |
41 |
Correct |
88 ms |
131640 KB |
Output is correct |
42 |
Correct |
53 ms |
124240 KB |
Output is correct |
43 |
Correct |
60 ms |
124432 KB |
Output is correct |
44 |
Correct |
61 ms |
124244 KB |
Output is correct |
45 |
Correct |
116 ms |
141216 KB |
Output is correct |
46 |
Correct |
114 ms |
141172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
123884 KB |
Output is correct |
2 |
Correct |
52 ms |
123860 KB |
Output is correct |
3 |
Correct |
52 ms |
123660 KB |
Output is correct |
4 |
Correct |
51 ms |
123732 KB |
Output is correct |
5 |
Correct |
50 ms |
123740 KB |
Output is correct |
6 |
Correct |
50 ms |
123696 KB |
Output is correct |
7 |
Correct |
54 ms |
123648 KB |
Output is correct |
8 |
Correct |
49 ms |
123740 KB |
Output is correct |
9 |
Correct |
56 ms |
123876 KB |
Output is correct |
10 |
Correct |
58 ms |
123948 KB |
Output is correct |
11 |
Correct |
53 ms |
124116 KB |
Output is correct |
12 |
Correct |
57 ms |
123700 KB |
Output is correct |
13 |
Correct |
56 ms |
123760 KB |
Output is correct |
14 |
Correct |
51 ms |
123984 KB |
Output is correct |
15 |
Correct |
54 ms |
123984 KB |
Output is correct |
16 |
Correct |
49 ms |
124052 KB |
Output is correct |
17 |
Correct |
53 ms |
124376 KB |
Output is correct |
18 |
Correct |
53 ms |
123988 KB |
Output is correct |
19 |
Correct |
48 ms |
123992 KB |
Output is correct |
20 |
Correct |
49 ms |
123872 KB |
Output is correct |
21 |
Correct |
50 ms |
123728 KB |
Output is correct |
22 |
Correct |
54 ms |
123968 KB |
Output is correct |
23 |
Correct |
54 ms |
124104 KB |
Output is correct |
24 |
Correct |
62 ms |
124244 KB |
Output is correct |
25 |
Correct |
55 ms |
124240 KB |
Output is correct |
26 |
Correct |
60 ms |
123984 KB |
Output is correct |
27 |
Correct |
55 ms |
124012 KB |
Output is correct |
28 |
Correct |
53 ms |
124768 KB |
Output is correct |
29 |
Correct |
58 ms |
126288 KB |
Output is correct |
30 |
Correct |
56 ms |
124496 KB |
Output is correct |
31 |
Correct |
56 ms |
125012 KB |
Output is correct |
32 |
Correct |
56 ms |
124500 KB |
Output is correct |
33 |
Correct |
66 ms |
128084 KB |
Output is correct |
34 |
Correct |
72 ms |
128160 KB |
Output is correct |
35 |
Correct |
71 ms |
128340 KB |
Output is correct |
36 |
Correct |
57 ms |
124428 KB |
Output is correct |
37 |
Correct |
83 ms |
132596 KB |
Output is correct |
38 |
Correct |
76 ms |
131504 KB |
Output is correct |
39 |
Correct |
85 ms |
131728 KB |
Output is correct |
40 |
Correct |
78 ms |
131412 KB |
Output is correct |
41 |
Correct |
76 ms |
131668 KB |
Output is correct |
42 |
Correct |
51 ms |
124464 KB |
Output is correct |
43 |
Correct |
57 ms |
124256 KB |
Output is correct |
44 |
Correct |
63 ms |
124244 KB |
Output is correct |
45 |
Correct |
112 ms |
141140 KB |
Output is correct |
46 |
Correct |
114 ms |
141140 KB |
Output is correct |
47 |
Correct |
167 ms |
154332 KB |
Output is correct |
48 |
Correct |
77 ms |
133204 KB |
Output is correct |
49 |
Correct |
68 ms |
129724 KB |
Output is correct |
50 |
Correct |
67 ms |
129664 KB |
Output is correct |
51 |
Correct |
93 ms |
136528 KB |
Output is correct |
52 |
Correct |
113 ms |
137712 KB |
Output is correct |
53 |
Correct |
70 ms |
128080 KB |
Output is correct |
54 |
Correct |
82 ms |
125008 KB |
Output is correct |
55 |
Correct |
92 ms |
126036 KB |
Output is correct |
56 |
Correct |
59 ms |
126288 KB |
Output is correct |
57 |
Runtime error |
333 ms |
262144 KB |
Execution killed with signal 9 |
58 |
Halted |
0 ms |
0 KB |
- |