#include <bits/stdc++.h>
#define fi first
#define si second
#define int long long
using namespace std;
const int N = 5e4 + 4;
int dist[N],n,m;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
vector<int> g[N];
pair<int,int> a[N];
signed main()
{
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
cin >> a[i].fi >> a[i].si;
a[i].fi++;
g[a[i].fi].push_back(a[i].si);
}
for(int i = 1; i <= n; i++) dist[i] = 1e18;
q.push({0,a[1].fi});
dist[a[1].first] = 0;
while(!q.empty())
{
pair<int,int> node = q.top();q.pop();
int u = node.si, d = node.fi;
//if(d >= 200) {continue;}
if(dist[u] < d) continue;
for(int p : g[u])
{ int t = d;
for(int i = u + p; i <= n; i += p)
{ t++;
if(dist[i] > t)
{
dist[i] = t;
q.push({dist[i],i});
}
}
t = d;
for(int i = u - p; i > 0; i -= p)
{
t++;
if(dist[i] > t)
{
dist[i] = t;
q.push({dist[i],i});
}
}
}
}
if(dist[a[2].first] == 1e18) cout << -1;
else cout << dist[a[2].first];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2552 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
2 ms |
2556 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
3 ms |
2384 KB |
Output is correct |
13 |
Correct |
2 ms |
2384 KB |
Output is correct |
14 |
Correct |
2 ms |
2384 KB |
Output is correct |
15 |
Correct |
2 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
2 ms |
2536 KB |
Output is correct |
12 |
Correct |
2 ms |
2384 KB |
Output is correct |
13 |
Correct |
2 ms |
2384 KB |
Output is correct |
14 |
Correct |
2 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
2 ms |
2556 KB |
Output is correct |
17 |
Correct |
2 ms |
2552 KB |
Output is correct |
18 |
Correct |
1 ms |
2504 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
4 ms |
2640 KB |
Output is correct |
21 |
Correct |
2 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
1 ms |
2384 KB |
Output is correct |
24 |
Correct |
2 ms |
2384 KB |
Output is correct |
25 |
Correct |
3 ms |
2640 KB |
Output is correct |
26 |
Correct |
4 ms |
2552 KB |
Output is correct |
27 |
Correct |
4 ms |
2384 KB |
Output is correct |
28 |
Correct |
2 ms |
2384 KB |
Output is correct |
29 |
Correct |
2 ms |
3064 KB |
Output is correct |
30 |
Correct |
2 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2640 KB |
Output is correct |
32 |
Correct |
2 ms |
2812 KB |
Output is correct |
33 |
Correct |
3 ms |
2896 KB |
Output is correct |
34 |
Correct |
3 ms |
2896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2556 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2552 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
2 ms |
2384 KB |
Output is correct |
13 |
Correct |
2 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
2 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2552 KB |
Output is correct |
20 |
Correct |
4 ms |
2660 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
2552 KB |
Output is correct |
23 |
Correct |
1 ms |
2384 KB |
Output is correct |
24 |
Correct |
2 ms |
2612 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
4 ms |
2384 KB |
Output is correct |
27 |
Correct |
4 ms |
2384 KB |
Output is correct |
28 |
Correct |
2 ms |
2384 KB |
Output is correct |
29 |
Correct |
2 ms |
2896 KB |
Output is correct |
30 |
Correct |
2 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2640 KB |
Output is correct |
32 |
Correct |
2 ms |
2640 KB |
Output is correct |
33 |
Correct |
3 ms |
2896 KB |
Output is correct |
34 |
Correct |
3 ms |
2896 KB |
Output is correct |
35 |
Correct |
10 ms |
3152 KB |
Output is correct |
36 |
Correct |
3 ms |
2640 KB |
Output is correct |
37 |
Correct |
9 ms |
3152 KB |
Output is correct |
38 |
Correct |
12 ms |
3320 KB |
Output is correct |
39 |
Correct |
12 ms |
3324 KB |
Output is correct |
40 |
Correct |
12 ms |
3112 KB |
Output is correct |
41 |
Correct |
11 ms |
3320 KB |
Output is correct |
42 |
Correct |
63 ms |
3020 KB |
Output is correct |
43 |
Correct |
48 ms |
3020 KB |
Output is correct |
44 |
Correct |
48 ms |
3020 KB |
Output is correct |
45 |
Correct |
10 ms |
3576 KB |
Output is correct |
46 |
Correct |
11 ms |
3424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2472 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2556 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
2 ms |
2384 KB |
Output is correct |
12 |
Correct |
2 ms |
2384 KB |
Output is correct |
13 |
Correct |
2 ms |
2384 KB |
Output is correct |
14 |
Correct |
2 ms |
2384 KB |
Output is correct |
15 |
Correct |
2 ms |
2384 KB |
Output is correct |
16 |
Correct |
2 ms |
2384 KB |
Output is correct |
17 |
Correct |
2 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
4 ms |
2640 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
2 ms |
2384 KB |
Output is correct |
24 |
Correct |
2 ms |
2384 KB |
Output is correct |
25 |
Correct |
3 ms |
2640 KB |
Output is correct |
26 |
Correct |
5 ms |
2384 KB |
Output is correct |
27 |
Correct |
4 ms |
2384 KB |
Output is correct |
28 |
Correct |
2 ms |
2384 KB |
Output is correct |
29 |
Correct |
3 ms |
2896 KB |
Output is correct |
30 |
Correct |
2 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2640 KB |
Output is correct |
33 |
Correct |
3 ms |
2896 KB |
Output is correct |
34 |
Correct |
3 ms |
2896 KB |
Output is correct |
35 |
Correct |
10 ms |
3152 KB |
Output is correct |
36 |
Correct |
4 ms |
2640 KB |
Output is correct |
37 |
Correct |
8 ms |
3152 KB |
Output is correct |
38 |
Correct |
12 ms |
3152 KB |
Output is correct |
39 |
Correct |
11 ms |
3192 KB |
Output is correct |
40 |
Correct |
12 ms |
3152 KB |
Output is correct |
41 |
Correct |
11 ms |
3152 KB |
Output is correct |
42 |
Correct |
48 ms |
3160 KB |
Output is correct |
43 |
Correct |
47 ms |
2968 KB |
Output is correct |
44 |
Correct |
48 ms |
3152 KB |
Output is correct |
45 |
Correct |
11 ms |
3408 KB |
Output is correct |
46 |
Correct |
11 ms |
3352 KB |
Output is correct |
47 |
Correct |
18 ms |
4040 KB |
Output is correct |
48 |
Correct |
10 ms |
3152 KB |
Output is correct |
49 |
Correct |
10 ms |
3152 KB |
Output is correct |
50 |
Correct |
8 ms |
2896 KB |
Output is correct |
51 |
Correct |
28 ms |
6344 KB |
Output is correct |
52 |
Correct |
26 ms |
4300 KB |
Output is correct |
53 |
Correct |
19 ms |
4300 KB |
Output is correct |
54 |
Correct |
3 ms |
2764 KB |
Output is correct |
55 |
Correct |
3 ms |
2764 KB |
Output is correct |
56 |
Correct |
654 ms |
4008 KB |
Output is correct |
57 |
Correct |
37 ms |
8644 KB |
Output is correct |
58 |
Correct |
552 ms |
3276 KB |
Output is correct |
59 |
Correct |
598 ms |
3288 KB |
Output is correct |
60 |
Correct |
572 ms |
3276 KB |
Output is correct |
61 |
Correct |
536 ms |
3284 KB |
Output is correct |
62 |
Correct |
14 ms |
3664 KB |
Output is correct |
63 |
Correct |
29 ms |
6344 KB |
Output is correct |
64 |
Correct |
31 ms |
9152 KB |
Output is correct |
65 |
Correct |
35 ms |
9408 KB |
Output is correct |
66 |
Correct |
46 ms |
9164 KB |
Output is correct |
67 |
Correct |
52 ms |
9152 KB |
Output is correct |