#include <bits/stdc++.h>
using namespace std;
const int N = 30'000 + 10;
int n, m;
int b[N], p[N];
vector<int> power[N];
int f[N];
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < m; ++i) cin >> b[i] >> p[i];
for (int i = 0; i < m; ++i) power[b[i]].push_back(p[i]);
memset(f, 63, sizeof f);
using pii = pair<int, int>;
priority_queue<pii, vector<pii>, greater<>> q;
q.push({f[b[0]] = 0, b[0]});
while (q.size()) {
auto [d, u] = q.top(); q.pop();
if (d != f[u]) continue;
for (const auto& pw : power[u]) {
{
int w = d;
for (int v = u + pw; v < n; v += pw) if (f[v] > ++w) q.push({f[v] = w, v});
}
{
int w = d;
for (int v = u - pw; v >= 0; v -= pw) if (f[v] > ++w) q.push({f[v] = w, v});
}
}
}
cout << (f[b[1]] < f[n] ? f[b[1]] : -1) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
2 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1168 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1368 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1168 KB |
Output is correct |
4 |
Correct |
2 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1112 KB |
Output is correct |
6 |
Correct |
2 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1168 KB |
Output is correct |
9 |
Correct |
2 ms |
1112 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
2 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
2 ms |
1272 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1184 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
4 ms |
1372 KB |
Output is correct |
21 |
Correct |
2 ms |
1180 KB |
Output is correct |
22 |
Correct |
2 ms |
1116 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
4 ms |
1116 KB |
Output is correct |
27 |
Correct |
4 ms |
1116 KB |
Output is correct |
28 |
Correct |
2 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1372 KB |
Output is correct |
30 |
Correct |
2 ms |
1372 KB |
Output is correct |
31 |
Correct |
2 ms |
1372 KB |
Output is correct |
32 |
Correct |
3 ms |
1368 KB |
Output is correct |
33 |
Correct |
4 ms |
1372 KB |
Output is correct |
34 |
Correct |
3 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
2 ms |
1172 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1112 KB |
Output is correct |
8 |
Correct |
2 ms |
1112 KB |
Output is correct |
9 |
Correct |
1 ms |
1112 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1112 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
2 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1112 KB |
Output is correct |
17 |
Correct |
2 ms |
1372 KB |
Output is correct |
18 |
Correct |
2 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
4 ms |
1372 KB |
Output is correct |
21 |
Correct |
2 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
4 ms |
1116 KB |
Output is correct |
27 |
Correct |
5 ms |
1184 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
2 ms |
1172 KB |
Output is correct |
31 |
Correct |
2 ms |
1372 KB |
Output is correct |
32 |
Correct |
2 ms |
1372 KB |
Output is correct |
33 |
Correct |
4 ms |
1372 KB |
Output is correct |
34 |
Correct |
3 ms |
1324 KB |
Output is correct |
35 |
Correct |
5 ms |
1884 KB |
Output is correct |
36 |
Correct |
3 ms |
1368 KB |
Output is correct |
37 |
Correct |
5 ms |
1700 KB |
Output is correct |
38 |
Correct |
6 ms |
1884 KB |
Output is correct |
39 |
Correct |
6 ms |
1884 KB |
Output is correct |
40 |
Correct |
6 ms |
1884 KB |
Output is correct |
41 |
Correct |
6 ms |
1884 KB |
Output is correct |
42 |
Correct |
33 ms |
1972 KB |
Output is correct |
43 |
Correct |
36 ms |
1884 KB |
Output is correct |
44 |
Correct |
33 ms |
2136 KB |
Output is correct |
45 |
Correct |
6 ms |
2136 KB |
Output is correct |
46 |
Correct |
6 ms |
2012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1172 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1112 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1180 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
2 ms |
1116 KB |
Output is correct |
16 |
Correct |
2 ms |
1172 KB |
Output is correct |
17 |
Correct |
2 ms |
1216 KB |
Output is correct |
18 |
Correct |
4 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1112 KB |
Output is correct |
20 |
Correct |
4 ms |
1368 KB |
Output is correct |
21 |
Correct |
2 ms |
1176 KB |
Output is correct |
22 |
Correct |
2 ms |
1116 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1368 KB |
Output is correct |
26 |
Correct |
3 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1112 KB |
Output is correct |
28 |
Correct |
2 ms |
1372 KB |
Output is correct |
29 |
Correct |
2 ms |
1368 KB |
Output is correct |
30 |
Correct |
2 ms |
1372 KB |
Output is correct |
31 |
Correct |
2 ms |
1372 KB |
Output is correct |
32 |
Correct |
1 ms |
1372 KB |
Output is correct |
33 |
Correct |
3 ms |
1372 KB |
Output is correct |
34 |
Correct |
3 ms |
1624 KB |
Output is correct |
35 |
Correct |
7 ms |
1884 KB |
Output is correct |
36 |
Correct |
3 ms |
1372 KB |
Output is correct |
37 |
Correct |
5 ms |
1884 KB |
Output is correct |
38 |
Correct |
8 ms |
1884 KB |
Output is correct |
39 |
Correct |
6 ms |
1884 KB |
Output is correct |
40 |
Correct |
6 ms |
1880 KB |
Output is correct |
41 |
Correct |
6 ms |
1884 KB |
Output is correct |
42 |
Correct |
34 ms |
1884 KB |
Output is correct |
43 |
Correct |
35 ms |
1880 KB |
Output is correct |
44 |
Correct |
35 ms |
2036 KB |
Output is correct |
45 |
Correct |
6 ms |
2136 KB |
Output is correct |
46 |
Correct |
6 ms |
2336 KB |
Output is correct |
47 |
Correct |
13 ms |
2516 KB |
Output is correct |
48 |
Correct |
5 ms |
2140 KB |
Output is correct |
49 |
Correct |
4 ms |
2136 KB |
Output is correct |
50 |
Correct |
3 ms |
1880 KB |
Output is correct |
51 |
Correct |
19 ms |
3540 KB |
Output is correct |
52 |
Correct |
18 ms |
3032 KB |
Output is correct |
53 |
Correct |
11 ms |
3028 KB |
Output is correct |
54 |
Correct |
2 ms |
1372 KB |
Output is correct |
55 |
Correct |
2 ms |
1372 KB |
Output is correct |
56 |
Correct |
516 ms |
3036 KB |
Output is correct |
57 |
Correct |
38 ms |
3532 KB |
Output is correct |
58 |
Correct |
562 ms |
1872 KB |
Output is correct |
59 |
Correct |
448 ms |
2136 KB |
Output is correct |
60 |
Correct |
544 ms |
2008 KB |
Output is correct |
61 |
Correct |
414 ms |
2140 KB |
Output is correct |
62 |
Correct |
7 ms |
2780 KB |
Output is correct |
63 |
Correct |
23 ms |
3036 KB |
Output is correct |
64 |
Correct |
25 ms |
4048 KB |
Output is correct |
65 |
Correct |
33 ms |
4308 KB |
Output is correct |
66 |
Correct |
41 ms |
4048 KB |
Output is correct |
67 |
Correct |
46 ms |
4048 KB |
Output is correct |