#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T& container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 3e4;
const int INF = 1e9 + 69;
int n, m;
vector<int> a[N];
bool activated[N];
vector<int> pos[N], di[N];
map<int, int> dis[N];
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
vector<pair<int, int>> dog(m);
for(int i = 0; i<m; ++i){
cin >> dog[i].first >> dog[i].second;
}
pair<int, int> dog0 = dog[0], dog1 = dog[1];
remove_dup(dog);
for(auto i: dog) a[i.first].push_back(i.second);
for(auto i: dog) pos[i.second].push_back(i.first % i.second);
for(int i = 1; i<=n; ++i){
vector<int> sigma = pos[i]; pos[i].clear();
remove_dup(sigma);
for(int j: sigma){
for(int k = j; k < n; k += i) pos[i].push_back(k);
}
sort(ALL(pos[i]));
di[i].resize(pos[i].size(), INF);
}
int o_cnt = 0;
int ans = INF;
deque<pair<int, int>> q;
q.push_back(dog0); dis[dog0.first][dog0.second] = 0;
while(q.size()){
pair<int, int> u = q.front(); q.pop_front();
int d = dis[u.first][u.second];
o_cnt++;
if (o_cnt > 6e6) exit(1);
if (u.first == dog1.first) {
minimize(ans, d);
break;
}
for(int i = -u.second; i <= u.second; i += u.second * 2) {
pair<int, int> v = {u.first + i, u.second};
if (v.first < 0 || v.first >= n) continue;
if (!dis[v.first].count(v.second)) {
dis[v.first][v.second] = d + 1;
q.push_back(v);
}
}
if (!activated[u.first]){
activated[u.first] = true;
for(int j: a[u.first]){
for(int i = -j; i <= j; i += j * 2) {
pair<int, int> v = {u.first + i, j};
if (v.first < 0 || v.first >= n) continue;
if (!dis[v.first].count(v.second)) {
dis[v.first][v.second] = d + 1;
q.push_back(v);
}
}
}
}
}
if (ans == INF) cout << -1 << "\n";
else cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
1 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
3932 KB |
Output is correct |
7 |
Correct |
1 ms |
3932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3928 KB |
Output is correct |
2 |
Correct |
1 ms |
3928 KB |
Output is correct |
3 |
Correct |
2 ms |
3932 KB |
Output is correct |
4 |
Correct |
1 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
3932 KB |
Output is correct |
7 |
Correct |
1 ms |
3932 KB |
Output is correct |
8 |
Correct |
1 ms |
3932 KB |
Output is correct |
9 |
Correct |
2 ms |
3932 KB |
Output is correct |
10 |
Correct |
1 ms |
3952 KB |
Output is correct |
11 |
Correct |
2 ms |
3932 KB |
Output is correct |
12 |
Correct |
1 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3932 KB |
Output is correct |
14 |
Correct |
3 ms |
4188 KB |
Output is correct |
15 |
Correct |
2 ms |
4188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3932 KB |
Output is correct |
2 |
Correct |
2 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
2 ms |
3928 KB |
Output is correct |
6 |
Correct |
1 ms |
3928 KB |
Output is correct |
7 |
Correct |
1 ms |
3932 KB |
Output is correct |
8 |
Correct |
1 ms |
3932 KB |
Output is correct |
9 |
Correct |
1 ms |
3760 KB |
Output is correct |
10 |
Correct |
1 ms |
3932 KB |
Output is correct |
11 |
Correct |
2 ms |
3932 KB |
Output is correct |
12 |
Correct |
1 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3932 KB |
Output is correct |
14 |
Correct |
3 ms |
4188 KB |
Output is correct |
15 |
Correct |
2 ms |
4188 KB |
Output is correct |
16 |
Correct |
1 ms |
3884 KB |
Output is correct |
17 |
Correct |
3 ms |
4184 KB |
Output is correct |
18 |
Correct |
2 ms |
3932 KB |
Output is correct |
19 |
Correct |
2 ms |
3932 KB |
Output is correct |
20 |
Correct |
2 ms |
3932 KB |
Output is correct |
21 |
Correct |
1 ms |
3932 KB |
Output is correct |
22 |
Correct |
2 ms |
3928 KB |
Output is correct |
23 |
Correct |
2 ms |
4188 KB |
Output is correct |
24 |
Correct |
3 ms |
4276 KB |
Output is correct |
25 |
Correct |
2 ms |
3932 KB |
Output is correct |
26 |
Correct |
2 ms |
4188 KB |
Output is correct |
27 |
Correct |
2 ms |
4188 KB |
Output is correct |
28 |
Correct |
5 ms |
4956 KB |
Output is correct |
29 |
Correct |
14 ms |
7260 KB |
Output is correct |
30 |
Correct |
4 ms |
4700 KB |
Output is correct |
31 |
Correct |
8 ms |
5724 KB |
Output is correct |
32 |
Correct |
8 ms |
5208 KB |
Output is correct |
33 |
Correct |
32 ms |
10452 KB |
Output is correct |
34 |
Correct |
18 ms |
8024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
1 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
3932 KB |
Output is correct |
7 |
Correct |
2 ms |
3932 KB |
Output is correct |
8 |
Correct |
1 ms |
3932 KB |
Output is correct |
9 |
Correct |
1 ms |
3932 KB |
Output is correct |
10 |
Correct |
2 ms |
3932 KB |
Output is correct |
11 |
Correct |
3 ms |
3932 KB |
Output is correct |
12 |
Correct |
2 ms |
3928 KB |
Output is correct |
13 |
Correct |
2 ms |
3932 KB |
Output is correct |
14 |
Correct |
3 ms |
4336 KB |
Output is correct |
15 |
Correct |
3 ms |
4224 KB |
Output is correct |
16 |
Correct |
2 ms |
3932 KB |
Output is correct |
17 |
Correct |
3 ms |
4188 KB |
Output is correct |
18 |
Correct |
2 ms |
3932 KB |
Output is correct |
19 |
Correct |
2 ms |
3932 KB |
Output is correct |
20 |
Correct |
2 ms |
4048 KB |
Output is correct |
21 |
Correct |
2 ms |
3932 KB |
Output is correct |
22 |
Correct |
2 ms |
3932 KB |
Output is correct |
23 |
Correct |
2 ms |
4188 KB |
Output is correct |
24 |
Correct |
4 ms |
4256 KB |
Output is correct |
25 |
Correct |
2 ms |
4188 KB |
Output is correct |
26 |
Correct |
2 ms |
4188 KB |
Output is correct |
27 |
Correct |
2 ms |
4188 KB |
Output is correct |
28 |
Correct |
5 ms |
4956 KB |
Output is correct |
29 |
Correct |
15 ms |
7260 KB |
Output is correct |
30 |
Correct |
4 ms |
4700 KB |
Output is correct |
31 |
Correct |
7 ms |
5768 KB |
Output is correct |
32 |
Correct |
6 ms |
5212 KB |
Output is correct |
33 |
Correct |
32 ms |
10572 KB |
Output is correct |
34 |
Correct |
18 ms |
8024 KB |
Output is correct |
35 |
Correct |
14 ms |
6492 KB |
Output is correct |
36 |
Correct |
3 ms |
4188 KB |
Output is correct |
37 |
Correct |
15 ms |
7020 KB |
Output is correct |
38 |
Correct |
17 ms |
6796 KB |
Output is correct |
39 |
Correct |
13 ms |
5976 KB |
Output is correct |
40 |
Correct |
13 ms |
5980 KB |
Output is correct |
41 |
Correct |
14 ms |
6236 KB |
Output is correct |
42 |
Correct |
5 ms |
4440 KB |
Output is correct |
43 |
Correct |
5 ms |
4444 KB |
Output is correct |
44 |
Correct |
5 ms |
4188 KB |
Output is correct |
45 |
Correct |
229 ms |
30768 KB |
Output is correct |
46 |
Correct |
118 ms |
21332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
1 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
2 ms |
3928 KB |
Output is correct |
7 |
Correct |
1 ms |
3932 KB |
Output is correct |
8 |
Correct |
1 ms |
3828 KB |
Output is correct |
9 |
Correct |
2 ms |
3928 KB |
Output is correct |
10 |
Correct |
2 ms |
3932 KB |
Output is correct |
11 |
Correct |
2 ms |
3932 KB |
Output is correct |
12 |
Correct |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
2 ms |
3928 KB |
Output is correct |
14 |
Correct |
3 ms |
4188 KB |
Output is correct |
15 |
Correct |
2 ms |
4232 KB |
Output is correct |
16 |
Correct |
1 ms |
3932 KB |
Output is correct |
17 |
Correct |
3 ms |
4188 KB |
Output is correct |
18 |
Correct |
2 ms |
3932 KB |
Output is correct |
19 |
Correct |
2 ms |
4052 KB |
Output is correct |
20 |
Correct |
2 ms |
3932 KB |
Output is correct |
21 |
Correct |
2 ms |
3928 KB |
Output is correct |
22 |
Correct |
2 ms |
3932 KB |
Output is correct |
23 |
Correct |
2 ms |
4188 KB |
Output is correct |
24 |
Correct |
3 ms |
4260 KB |
Output is correct |
25 |
Correct |
2 ms |
3932 KB |
Output is correct |
26 |
Correct |
2 ms |
4188 KB |
Output is correct |
27 |
Correct |
2 ms |
4184 KB |
Output is correct |
28 |
Correct |
7 ms |
4952 KB |
Output is correct |
29 |
Correct |
15 ms |
7260 KB |
Output is correct |
30 |
Correct |
4 ms |
4700 KB |
Output is correct |
31 |
Correct |
8 ms |
5724 KB |
Output is correct |
32 |
Correct |
6 ms |
5212 KB |
Output is correct |
33 |
Correct |
36 ms |
10604 KB |
Output is correct |
34 |
Correct |
21 ms |
8028 KB |
Output is correct |
35 |
Correct |
14 ms |
6492 KB |
Output is correct |
36 |
Correct |
3 ms |
4188 KB |
Output is correct |
37 |
Correct |
15 ms |
7112 KB |
Output is correct |
38 |
Correct |
18 ms |
6768 KB |
Output is correct |
39 |
Correct |
13 ms |
5784 KB |
Output is correct |
40 |
Correct |
13 ms |
6024 KB |
Output is correct |
41 |
Correct |
14 ms |
6188 KB |
Output is correct |
42 |
Correct |
6 ms |
4444 KB |
Output is correct |
43 |
Correct |
7 ms |
4460 KB |
Output is correct |
44 |
Correct |
5 ms |
4236 KB |
Output is correct |
45 |
Correct |
249 ms |
30836 KB |
Output is correct |
46 |
Correct |
109 ms |
21136 KB |
Output is correct |
47 |
Correct |
43 ms |
12056 KB |
Output is correct |
48 |
Correct |
17 ms |
7248 KB |
Output is correct |
49 |
Correct |
12 ms |
6492 KB |
Output is correct |
50 |
Correct |
10 ms |
6152 KB |
Output is correct |
51 |
Correct |
38 ms |
10476 KB |
Output is correct |
52 |
Correct |
35 ms |
11196 KB |
Output is correct |
53 |
Correct |
17 ms |
5980 KB |
Output is correct |
54 |
Correct |
6 ms |
4700 KB |
Output is correct |
55 |
Correct |
4 ms |
5468 KB |
Output is correct |
56 |
Correct |
14 ms |
6828 KB |
Output is correct |
57 |
Correct |
20 ms |
7628 KB |
Output is correct |
58 |
Correct |
11 ms |
7812 KB |
Output is correct |
59 |
Correct |
16 ms |
7760 KB |
Output is correct |
60 |
Correct |
17 ms |
8028 KB |
Output is correct |
61 |
Correct |
12 ms |
7772 KB |
Output is correct |
62 |
Correct |
188 ms |
29732 KB |
Output is correct |
63 |
Execution timed out |
1064 ms |
94988 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |