#pragma GCC optimize("O3")
#include<bits/stdc++.h>
#define ll long long
const int nmax = 1e5 + 5, N = 5e5 * 2;
const ll oo = 1e18;
const int lg = 19, M = 4e3;
#define pii pair<ll, ll>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';cout << endl
using namespace std;
int n, m;
pii a[nmax];
vector<int> adj[nmax];
ll dp[nmax];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
cin >> n >> m;
for(int i = 0; i < m; ++i){
cin >> a[i].fi >> a[i].se;
adj[a[i].fi].push_back(a[i].se);
}
priority_queue<pii, vector<pii>, greater<>> q;
memset(dp, 0x3f, sizeof dp);
dp[a[0].fi] = 0;
q.push({0, a[0].fi});
while(q.size()){
pii tmp = q.top();q.pop();
int u = tmp.se;
if(dp[u] != tmp.fi) continue;
for(auto &pw : adj[u]){
int w = tmp.fi;
for(int v = u + pw; v < n; v += pw){
if (dp[v] > ++w){
dp[v] = w;
q.push({dp[v], v});
}
}
w = tmp.fi;
for(int v = u - pw; v >= 0; v -= pw){
if (dp[v] > ++w){
dp[v] = w;
q.push({dp[v], v});
}
}
}
}
// debug(dp, n);
if(dp[a[1].fi] < dp[n]) cout << dp[a[1].fi];
else cout << -1;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4688 KB |
Output is correct |
2 |
Correct |
1 ms |
4688 KB |
Output is correct |
3 |
Correct |
2 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4688 KB |
Output is correct |
5 |
Correct |
2 ms |
4688 KB |
Output is correct |
6 |
Correct |
2 ms |
4688 KB |
Output is correct |
7 |
Correct |
2 ms |
4688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4688 KB |
Output is correct |
2 |
Correct |
2 ms |
4688 KB |
Output is correct |
3 |
Correct |
2 ms |
4688 KB |
Output is correct |
4 |
Correct |
1 ms |
4688 KB |
Output is correct |
5 |
Correct |
2 ms |
4688 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
2 ms |
4688 KB |
Output is correct |
8 |
Correct |
2 ms |
4688 KB |
Output is correct |
9 |
Correct |
2 ms |
4688 KB |
Output is correct |
10 |
Correct |
2 ms |
4688 KB |
Output is correct |
11 |
Correct |
2 ms |
4688 KB |
Output is correct |
12 |
Correct |
2 ms |
4688 KB |
Output is correct |
13 |
Correct |
2 ms |
4688 KB |
Output is correct |
14 |
Correct |
2 ms |
4688 KB |
Output is correct |
15 |
Correct |
3 ms |
4860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4688 KB |
Output is correct |
2 |
Correct |
2 ms |
4688 KB |
Output is correct |
3 |
Correct |
2 ms |
4688 KB |
Output is correct |
4 |
Correct |
2 ms |
4688 KB |
Output is correct |
5 |
Correct |
2 ms |
4688 KB |
Output is correct |
6 |
Correct |
2 ms |
4540 KB |
Output is correct |
7 |
Correct |
2 ms |
4688 KB |
Output is correct |
8 |
Correct |
2 ms |
4688 KB |
Output is correct |
9 |
Correct |
2 ms |
4688 KB |
Output is correct |
10 |
Correct |
2 ms |
4688 KB |
Output is correct |
11 |
Correct |
2 ms |
4788 KB |
Output is correct |
12 |
Correct |
2 ms |
4688 KB |
Output is correct |
13 |
Correct |
2 ms |
4688 KB |
Output is correct |
14 |
Correct |
2 ms |
4580 KB |
Output is correct |
15 |
Correct |
2 ms |
4688 KB |
Output is correct |
16 |
Correct |
2 ms |
4688 KB |
Output is correct |
17 |
Correct |
3 ms |
4688 KB |
Output is correct |
18 |
Correct |
2 ms |
4688 KB |
Output is correct |
19 |
Correct |
2 ms |
4940 KB |
Output is correct |
20 |
Correct |
5 ms |
4688 KB |
Output is correct |
21 |
Correct |
2 ms |
4688 KB |
Output is correct |
22 |
Correct |
2 ms |
4688 KB |
Output is correct |
23 |
Correct |
2 ms |
4688 KB |
Output is correct |
24 |
Correct |
3 ms |
4688 KB |
Output is correct |
25 |
Correct |
2 ms |
4688 KB |
Output is correct |
26 |
Correct |
4 ms |
4688 KB |
Output is correct |
27 |
Correct |
4 ms |
4584 KB |
Output is correct |
28 |
Correct |
2 ms |
4688 KB |
Output is correct |
29 |
Correct |
4 ms |
4944 KB |
Output is correct |
30 |
Correct |
3 ms |
4944 KB |
Output is correct |
31 |
Correct |
3 ms |
4944 KB |
Output is correct |
32 |
Correct |
2 ms |
5112 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4688 KB |
Output is correct |
2 |
Correct |
2 ms |
4688 KB |
Output is correct |
3 |
Correct |
2 ms |
4744 KB |
Output is correct |
4 |
Correct |
2 ms |
4688 KB |
Output is correct |
5 |
Correct |
2 ms |
4688 KB |
Output is correct |
6 |
Correct |
3 ms |
4688 KB |
Output is correct |
7 |
Correct |
2 ms |
4856 KB |
Output is correct |
8 |
Correct |
2 ms |
4856 KB |
Output is correct |
9 |
Correct |
2 ms |
4688 KB |
Output is correct |
10 |
Correct |
2 ms |
4688 KB |
Output is correct |
11 |
Correct |
2 ms |
4688 KB |
Output is correct |
12 |
Correct |
2 ms |
4688 KB |
Output is correct |
13 |
Correct |
2 ms |
4688 KB |
Output is correct |
14 |
Correct |
2 ms |
4688 KB |
Output is correct |
15 |
Correct |
2 ms |
4688 KB |
Output is correct |
16 |
Correct |
3 ms |
4860 KB |
Output is correct |
17 |
Correct |
3 ms |
4688 KB |
Output is correct |
18 |
Correct |
2 ms |
4688 KB |
Output is correct |
19 |
Correct |
2 ms |
4688 KB |
Output is correct |
20 |
Correct |
4 ms |
4856 KB |
Output is correct |
21 |
Correct |
2 ms |
4688 KB |
Output is correct |
22 |
Correct |
2 ms |
4688 KB |
Output is correct |
23 |
Correct |
2 ms |
4688 KB |
Output is correct |
24 |
Correct |
2 ms |
4688 KB |
Output is correct |
25 |
Correct |
3 ms |
4688 KB |
Output is correct |
26 |
Correct |
5 ms |
4688 KB |
Output is correct |
27 |
Correct |
4 ms |
4688 KB |
Output is correct |
28 |
Correct |
2 ms |
4688 KB |
Output is correct |
29 |
Correct |
3 ms |
4944 KB |
Output is correct |
30 |
Correct |
2 ms |
4956 KB |
Output is correct |
31 |
Correct |
2 ms |
4944 KB |
Output is correct |
32 |
Correct |
2 ms |
4944 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
5 ms |
5200 KB |
Output is correct |
36 |
Correct |
2 ms |
4688 KB |
Output is correct |
37 |
Correct |
7 ms |
5120 KB |
Output is correct |
38 |
Correct |
8 ms |
4944 KB |
Output is correct |
39 |
Correct |
8 ms |
5200 KB |
Output is correct |
40 |
Correct |
6 ms |
5200 KB |
Output is correct |
41 |
Correct |
6 ms |
5368 KB |
Output is correct |
42 |
Correct |
45 ms |
5200 KB |
Output is correct |
43 |
Correct |
53 ms |
5200 KB |
Output is correct |
44 |
Correct |
45 ms |
5200 KB |
Output is correct |
45 |
Correct |
7 ms |
5456 KB |
Output is correct |
46 |
Correct |
7 ms |
5152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4688 KB |
Output is correct |
2 |
Correct |
2 ms |
4688 KB |
Output is correct |
3 |
Correct |
2 ms |
4688 KB |
Output is correct |
4 |
Correct |
2 ms |
4872 KB |
Output is correct |
5 |
Correct |
2 ms |
4688 KB |
Output is correct |
6 |
Correct |
2 ms |
4688 KB |
Output is correct |
7 |
Correct |
2 ms |
4688 KB |
Output is correct |
8 |
Correct |
2 ms |
4688 KB |
Output is correct |
9 |
Correct |
3 ms |
4688 KB |
Output is correct |
10 |
Correct |
2 ms |
4688 KB |
Output is correct |
11 |
Correct |
2 ms |
4688 KB |
Output is correct |
12 |
Correct |
2 ms |
4688 KB |
Output is correct |
13 |
Correct |
2 ms |
4688 KB |
Output is correct |
14 |
Correct |
2 ms |
4688 KB |
Output is correct |
15 |
Correct |
2 ms |
4688 KB |
Output is correct |
16 |
Correct |
2 ms |
4688 KB |
Output is correct |
17 |
Correct |
3 ms |
4688 KB |
Output is correct |
18 |
Correct |
2 ms |
4688 KB |
Output is correct |
19 |
Correct |
2 ms |
4688 KB |
Output is correct |
20 |
Correct |
5 ms |
4688 KB |
Output is correct |
21 |
Correct |
2 ms |
4688 KB |
Output is correct |
22 |
Correct |
2 ms |
4688 KB |
Output is correct |
23 |
Correct |
2 ms |
4688 KB |
Output is correct |
24 |
Correct |
3 ms |
4688 KB |
Output is correct |
25 |
Correct |
3 ms |
4856 KB |
Output is correct |
26 |
Correct |
4 ms |
4688 KB |
Output is correct |
27 |
Correct |
4 ms |
4688 KB |
Output is correct |
28 |
Correct |
2 ms |
4688 KB |
Output is correct |
29 |
Correct |
3 ms |
4944 KB |
Output is correct |
30 |
Correct |
2 ms |
5112 KB |
Output is correct |
31 |
Correct |
2 ms |
4944 KB |
Output is correct |
32 |
Correct |
2 ms |
4944 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
6 ms |
5200 KB |
Output is correct |
36 |
Correct |
2 ms |
4688 KB |
Output is correct |
37 |
Correct |
5 ms |
5200 KB |
Output is correct |
38 |
Correct |
7 ms |
5352 KB |
Output is correct |
39 |
Correct |
7 ms |
5112 KB |
Output is correct |
40 |
Correct |
7 ms |
5380 KB |
Output is correct |
41 |
Correct |
11 ms |
4944 KB |
Output is correct |
42 |
Correct |
44 ms |
5200 KB |
Output is correct |
43 |
Correct |
56 ms |
5200 KB |
Output is correct |
44 |
Correct |
45 ms |
5200 KB |
Output is correct |
45 |
Correct |
6 ms |
5456 KB |
Output is correct |
46 |
Correct |
7 ms |
5472 KB |
Output is correct |
47 |
Correct |
15 ms |
6344 KB |
Output is correct |
48 |
Correct |
6 ms |
5200 KB |
Output is correct |
49 |
Correct |
6 ms |
5200 KB |
Output is correct |
50 |
Correct |
4 ms |
5200 KB |
Output is correct |
51 |
Correct |
22 ms |
8644 KB |
Output is correct |
52 |
Correct |
20 ms |
6612 KB |
Output is correct |
53 |
Correct |
12 ms |
6344 KB |
Output is correct |
54 |
Correct |
3 ms |
5076 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
566 ms |
6396 KB |
Output is correct |
57 |
Correct |
40 ms |
10944 KB |
Output is correct |
58 |
Correct |
431 ms |
5324 KB |
Output is correct |
59 |
Correct |
660 ms |
5456 KB |
Output is correct |
60 |
Correct |
471 ms |
5456 KB |
Output is correct |
61 |
Correct |
537 ms |
5200 KB |
Output is correct |
62 |
Correct |
8 ms |
5968 KB |
Output is correct |
63 |
Correct |
27 ms |
8188 KB |
Output is correct |
64 |
Correct |
26 ms |
11456 KB |
Output is correct |
65 |
Correct |
31 ms |
11464 KB |
Output is correct |
66 |
Correct |
42 ms |
11212 KB |
Output is correct |
67 |
Correct |
42 ms |
11456 KB |
Output is correct |