#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>*/
#define pb push_back
#define mp make_pair
#define sz(s) ((int)(s.size()))
#define all(s) s.begin(), s.end()
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, n, a) for (int i = n; i >= a; --i)
#define onlycin ios_base::sync_with_stdio(false); cin.tie(0)
#define F first
#define S second
using namespace std;
// using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
/*typedef tree<
pair < int, int >,
null_type,
less< pair < int, int > >,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;*/
// find_by_order() order_of_key()
const int MAXN = (int)5e5+228;
const char nxtl = '\n';
const int mod = (int)1e9+7;
const double eps = (double)1e-7;
template<typename T> inline bool updmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<typename T> inline bool updmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
int n, m, b[MAXN], p[MAXN], d[MAXN];
vector < int > have[MAXN];
int main() {
#ifdef accepted
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
onlycin;
cin >> n >> m;
rep(i, 1, m) {
cin >> b[i] >> p[i];
have[++b[i]].pb(p[i]);
}
rep(i, 1, n) {
sort(all(have[i])); have[i].resize(unique(all(have[i]))-have[i].begin());
}
memset(d, 0x3f, sizeof d);
d[b[1]] = 0;
set < pair < int, int > > S; S.insert(mp(0, b[1]));
while(!S.empty()) {
int v = S.begin()->second;
S.erase(S.begin());
for(auto &to : have[v]) {
int cnt = 0;
for(int go = v + to; go <= n; go += to) {
cnt++;
if(cnt + d[v] < d[go]) {
S.erase(mp(d[go], go));
d[go] = d[v]+cnt;
S.insert(mp(d[go], go));
}
if(binary_search(all(have[go]), to)) break;
}
cnt = 0;
for(int go = v - to; go > 0; go -= to) {
cnt++;
if(cnt + d[v] < d[go]) {
S.erase(mp(d[go], go));
d[go] = d[v]+cnt;
S.insert(mp(d[go], go));
}
if(binary_search(all(have[go]), to)) break;
}
}
}
if(d[b[2]] == d[0]) d[b[2]] = -1;
cout << d[b[2]] << nxtl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14072 KB |
Output is correct |
2 |
Correct |
12 ms |
14072 KB |
Output is correct |
3 |
Correct |
12 ms |
14100 KB |
Output is correct |
4 |
Correct |
15 ms |
14100 KB |
Output is correct |
5 |
Correct |
15 ms |
14104 KB |
Output is correct |
6 |
Correct |
15 ms |
14228 KB |
Output is correct |
7 |
Correct |
14 ms |
14228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14228 KB |
Output is correct |
2 |
Correct |
12 ms |
14228 KB |
Output is correct |
3 |
Correct |
12 ms |
14232 KB |
Output is correct |
4 |
Correct |
12 ms |
14232 KB |
Output is correct |
5 |
Correct |
12 ms |
14232 KB |
Output is correct |
6 |
Correct |
12 ms |
14244 KB |
Output is correct |
7 |
Correct |
12 ms |
14300 KB |
Output is correct |
8 |
Correct |
12 ms |
14300 KB |
Output is correct |
9 |
Correct |
12 ms |
14300 KB |
Output is correct |
10 |
Correct |
13 ms |
14316 KB |
Output is correct |
11 |
Correct |
14 ms |
14316 KB |
Output is correct |
12 |
Correct |
12 ms |
14316 KB |
Output is correct |
13 |
Correct |
12 ms |
14316 KB |
Output is correct |
14 |
Correct |
12 ms |
14316 KB |
Output is correct |
15 |
Correct |
13 ms |
14316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14316 KB |
Output is correct |
2 |
Correct |
12 ms |
14320 KB |
Output is correct |
3 |
Correct |
13 ms |
14320 KB |
Output is correct |
4 |
Correct |
12 ms |
14320 KB |
Output is correct |
5 |
Correct |
14 ms |
14320 KB |
Output is correct |
6 |
Correct |
12 ms |
14320 KB |
Output is correct |
7 |
Correct |
12 ms |
14320 KB |
Output is correct |
8 |
Correct |
13 ms |
14320 KB |
Output is correct |
9 |
Correct |
13 ms |
14320 KB |
Output is correct |
10 |
Correct |
12 ms |
14320 KB |
Output is correct |
11 |
Correct |
12 ms |
14320 KB |
Output is correct |
12 |
Correct |
12 ms |
14320 KB |
Output is correct |
13 |
Correct |
12 ms |
14320 KB |
Output is correct |
14 |
Correct |
12 ms |
14320 KB |
Output is correct |
15 |
Correct |
12 ms |
14320 KB |
Output is correct |
16 |
Correct |
12 ms |
14320 KB |
Output is correct |
17 |
Correct |
14 ms |
14320 KB |
Output is correct |
18 |
Correct |
14 ms |
14320 KB |
Output is correct |
19 |
Correct |
12 ms |
14320 KB |
Output is correct |
20 |
Correct |
14 ms |
14320 KB |
Output is correct |
21 |
Correct |
14 ms |
14320 KB |
Output is correct |
22 |
Correct |
13 ms |
14320 KB |
Output is correct |
23 |
Correct |
15 ms |
14320 KB |
Output is correct |
24 |
Correct |
16 ms |
14476 KB |
Output is correct |
25 |
Correct |
14 ms |
14476 KB |
Output is correct |
26 |
Correct |
15 ms |
14476 KB |
Output is correct |
27 |
Correct |
14 ms |
14476 KB |
Output is correct |
28 |
Correct |
13 ms |
14476 KB |
Output is correct |
29 |
Correct |
18 ms |
14476 KB |
Output is correct |
30 |
Correct |
14 ms |
14564 KB |
Output is correct |
31 |
Correct |
14 ms |
14564 KB |
Output is correct |
32 |
Correct |
14 ms |
14564 KB |
Output is correct |
33 |
Correct |
19 ms |
14564 KB |
Output is correct |
34 |
Correct |
24 ms |
14564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14564 KB |
Output is correct |
2 |
Correct |
12 ms |
14564 KB |
Output is correct |
3 |
Correct |
15 ms |
14564 KB |
Output is correct |
4 |
Correct |
14 ms |
14564 KB |
Output is correct |
5 |
Correct |
12 ms |
14564 KB |
Output is correct |
6 |
Correct |
12 ms |
14564 KB |
Output is correct |
7 |
Correct |
14 ms |
14564 KB |
Output is correct |
8 |
Correct |
12 ms |
14564 KB |
Output is correct |
9 |
Correct |
11 ms |
14564 KB |
Output is correct |
10 |
Correct |
13 ms |
14564 KB |
Output is correct |
11 |
Correct |
13 ms |
14564 KB |
Output is correct |
12 |
Correct |
12 ms |
14564 KB |
Output is correct |
13 |
Correct |
13 ms |
14564 KB |
Output is correct |
14 |
Correct |
14 ms |
14564 KB |
Output is correct |
15 |
Correct |
13 ms |
14564 KB |
Output is correct |
16 |
Correct |
13 ms |
14564 KB |
Output is correct |
17 |
Correct |
14 ms |
14564 KB |
Output is correct |
18 |
Correct |
13 ms |
14564 KB |
Output is correct |
19 |
Correct |
13 ms |
14564 KB |
Output is correct |
20 |
Correct |
14 ms |
14564 KB |
Output is correct |
21 |
Correct |
13 ms |
14564 KB |
Output is correct |
22 |
Correct |
13 ms |
14564 KB |
Output is correct |
23 |
Correct |
14 ms |
14564 KB |
Output is correct |
24 |
Correct |
15 ms |
14564 KB |
Output is correct |
25 |
Correct |
14 ms |
14564 KB |
Output is correct |
26 |
Correct |
13 ms |
14564 KB |
Output is correct |
27 |
Correct |
14 ms |
14564 KB |
Output is correct |
28 |
Correct |
14 ms |
14564 KB |
Output is correct |
29 |
Correct |
18 ms |
14564 KB |
Output is correct |
30 |
Correct |
15 ms |
14564 KB |
Output is correct |
31 |
Correct |
16 ms |
14564 KB |
Output is correct |
32 |
Correct |
14 ms |
14564 KB |
Output is correct |
33 |
Correct |
19 ms |
14564 KB |
Output is correct |
34 |
Correct |
19 ms |
14564 KB |
Output is correct |
35 |
Correct |
23 ms |
14700 KB |
Output is correct |
36 |
Correct |
15 ms |
14700 KB |
Output is correct |
37 |
Correct |
23 ms |
14812 KB |
Output is correct |
38 |
Correct |
25 ms |
14828 KB |
Output is correct |
39 |
Correct |
32 ms |
14828 KB |
Output is correct |
40 |
Correct |
32 ms |
14828 KB |
Output is correct |
41 |
Correct |
25 ms |
14828 KB |
Output is correct |
42 |
Correct |
18 ms |
14828 KB |
Output is correct |
43 |
Correct |
18 ms |
14828 KB |
Output is correct |
44 |
Correct |
18 ms |
14828 KB |
Output is correct |
45 |
Correct |
26 ms |
14828 KB |
Output is correct |
46 |
Correct |
28 ms |
14828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14828 KB |
Output is correct |
2 |
Correct |
13 ms |
14828 KB |
Output is correct |
3 |
Correct |
13 ms |
14828 KB |
Output is correct |
4 |
Correct |
12 ms |
14828 KB |
Output is correct |
5 |
Correct |
13 ms |
14828 KB |
Output is correct |
6 |
Correct |
12 ms |
14828 KB |
Output is correct |
7 |
Correct |
13 ms |
14828 KB |
Output is correct |
8 |
Correct |
13 ms |
14828 KB |
Output is correct |
9 |
Correct |
12 ms |
14828 KB |
Output is correct |
10 |
Correct |
13 ms |
14828 KB |
Output is correct |
11 |
Correct |
13 ms |
14828 KB |
Output is correct |
12 |
Correct |
13 ms |
14828 KB |
Output is correct |
13 |
Correct |
13 ms |
14828 KB |
Output is correct |
14 |
Correct |
16 ms |
14828 KB |
Output is correct |
15 |
Correct |
14 ms |
14828 KB |
Output is correct |
16 |
Correct |
14 ms |
14828 KB |
Output is correct |
17 |
Correct |
16 ms |
14828 KB |
Output is correct |
18 |
Correct |
12 ms |
14828 KB |
Output is correct |
19 |
Correct |
14 ms |
14828 KB |
Output is correct |
20 |
Correct |
12 ms |
14828 KB |
Output is correct |
21 |
Correct |
12 ms |
14828 KB |
Output is correct |
22 |
Correct |
12 ms |
14828 KB |
Output is correct |
23 |
Correct |
16 ms |
14828 KB |
Output is correct |
24 |
Correct |
14 ms |
14828 KB |
Output is correct |
25 |
Correct |
13 ms |
14828 KB |
Output is correct |
26 |
Correct |
13 ms |
14828 KB |
Output is correct |
27 |
Correct |
13 ms |
14828 KB |
Output is correct |
28 |
Correct |
13 ms |
14828 KB |
Output is correct |
29 |
Correct |
16 ms |
14828 KB |
Output is correct |
30 |
Correct |
14 ms |
14828 KB |
Output is correct |
31 |
Correct |
16 ms |
14828 KB |
Output is correct |
32 |
Correct |
15 ms |
14828 KB |
Output is correct |
33 |
Correct |
19 ms |
14828 KB |
Output is correct |
34 |
Correct |
19 ms |
14828 KB |
Output is correct |
35 |
Correct |
21 ms |
14828 KB |
Output is correct |
36 |
Correct |
15 ms |
14828 KB |
Output is correct |
37 |
Correct |
23 ms |
14828 KB |
Output is correct |
38 |
Correct |
25 ms |
14828 KB |
Output is correct |
39 |
Correct |
25 ms |
14828 KB |
Output is correct |
40 |
Correct |
25 ms |
14828 KB |
Output is correct |
41 |
Correct |
25 ms |
14828 KB |
Output is correct |
42 |
Correct |
20 ms |
14828 KB |
Output is correct |
43 |
Correct |
17 ms |
14828 KB |
Output is correct |
44 |
Correct |
18 ms |
14828 KB |
Output is correct |
45 |
Correct |
26 ms |
14828 KB |
Output is correct |
46 |
Correct |
26 ms |
14828 KB |
Output is correct |
47 |
Correct |
55 ms |
15340 KB |
Output is correct |
48 |
Correct |
20 ms |
15340 KB |
Output is correct |
49 |
Correct |
19 ms |
15340 KB |
Output is correct |
50 |
Correct |
17 ms |
15340 KB |
Output is correct |
51 |
Correct |
76 ms |
16436 KB |
Output is correct |
52 |
Correct |
76 ms |
16748 KB |
Output is correct |
53 |
Correct |
37 ms |
17128 KB |
Output is correct |
54 |
Correct |
16 ms |
17128 KB |
Output is correct |
55 |
Correct |
18 ms |
17128 KB |
Output is correct |
56 |
Correct |
23 ms |
17128 KB |
Output is correct |
57 |
Correct |
72 ms |
17128 KB |
Output is correct |
58 |
Correct |
24 ms |
17128 KB |
Output is correct |
59 |
Correct |
24 ms |
17128 KB |
Output is correct |
60 |
Correct |
26 ms |
17128 KB |
Output is correct |
61 |
Correct |
37 ms |
17208 KB |
Output is correct |
62 |
Correct |
28 ms |
17712 KB |
Output is correct |
63 |
Correct |
105 ms |
18680 KB |
Output is correct |
64 |
Correct |
106 ms |
18796 KB |
Output is correct |
65 |
Correct |
136 ms |
19024 KB |
Output is correct |
66 |
Correct |
292 ms |
19248 KB |
Output is correct |
67 |
Correct |
265 ms |
19332 KB |
Output is correct |