#include<bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define int ll
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"
const ld PI = 3.1415926535;
const int N = 30000+5;
const int M = 7e6 + 1;
int mod = 998244353;
const int infi = INT_MAX;
const ll infl = LLONG_MAX;
const int P = 31;
int mult(int a, int b) {
return a * 1LL * b % mod;
}
int sum(int a, int b) {
if (a + b < 0)
return a + b + mod;
if (a + b >= mod)
return a + b - mod;
return a + b;
}
ll binpow(ll a, ll n) {
if (n == 0)
return 1;
if (n % 2 == 1) {
return binpow(a, n - 1) * a % mod;
} else {
ll b = binpow(a, n / 2);
return b * b % mod;
}
}
int n, m, x[N], p[N], d[N];
map<pii, vector<int>> ps;
vector<pii> g[N];
void solve(){
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> x[i] >> p[i];
ps[{x[i] % p[i], p[i]}].pb(x[i]);
}
for(auto e : ps){
int st = e.F.F, d = e.F.S;
vector<int> xs = e.S;
sort(all(xs));
xs.resize(unique(all(xs)) - xs.begin());
for(int i = 0; i < sz(xs) - 1; i++){
int l = xs[i], r = xs[i + 1];
for(int j = l; j <= r; j += d){
g[l].pb({j, (j - l) / d});
g[r].pb({j, (r - j) / d});
}
}
for(int j = st; j <= xs[0]; j += d){
g[xs[0]].pb({j, (xs[0] - j) / d});
}
for(int j = xs[sz(xs) - 1]; j < n; j += d){
g[xs[sz(xs) - 1]].pb({j, (j - xs[sz(xs) - 1]) / d});
}
}
for(int i = 0; i < n; i++)
d[i] = infi;
d[x[0]] = 0;
set<pii> st;
st.insert({0, x[0]});
while(!st.empty()){
int v = st.begin()->S;
st.erase(st.begin());
for(auto e : g[v]){
int u = e.F, w = e.S;
if(d[u] > d[v] + w){
st.erase({d[u], u});
d[u] = d[v] + w;
st.insert({d[u], u});
}
}
}
cout << (d[x[1]] == infi ? -1 : d[x[1]]) << '\n';
}
signed main() {
mispertion;
int t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
0 ms |
1272 KB |
Output is correct |
3 |
Correct |
1 ms |
1284 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 |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1112 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 |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
0 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1628 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 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 |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1112 KB |
Output is correct |
13 |
Correct |
1 ms |
1112 KB |
Output is correct |
14 |
Correct |
1 ms |
1628 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
2 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
1372 KB |
Output is correct |
19 |
Correct |
1 ms |
1372 KB |
Output is correct |
20 |
Correct |
1 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1628 KB |
Output is correct |
25 |
Correct |
2 ms |
1624 KB |
Output is correct |
26 |
Correct |
1 ms |
1368 KB |
Output is correct |
27 |
Correct |
1 ms |
1440 KB |
Output is correct |
28 |
Correct |
2 ms |
1884 KB |
Output is correct |
29 |
Correct |
4 ms |
2564 KB |
Output is correct |
30 |
Correct |
2 ms |
1628 KB |
Output is correct |
31 |
Correct |
3 ms |
2060 KB |
Output is correct |
32 |
Correct |
2 ms |
1628 KB |
Output is correct |
33 |
Correct |
7 ms |
3560 KB |
Output is correct |
34 |
Correct |
7 ms |
3776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1112 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
0 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1172 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1628 KB |
Output is correct |
15 |
Correct |
2 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
2 ms |
1628 KB |
Output is correct |
18 |
Correct |
1 ms |
1372 KB |
Output is correct |
19 |
Correct |
1 ms |
1372 KB |
Output is correct |
20 |
Correct |
1 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
1 ms |
1628 KB |
Output is correct |
23 |
Correct |
1 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1628 KB |
Output is correct |
25 |
Correct |
2 ms |
1628 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
1 ms |
1372 KB |
Output is correct |
28 |
Correct |
2 ms |
1884 KB |
Output is correct |
29 |
Correct |
4 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
1736 KB |
Output is correct |
31 |
Correct |
2 ms |
1932 KB |
Output is correct |
32 |
Correct |
2 ms |
1628 KB |
Output is correct |
33 |
Correct |
7 ms |
3560 KB |
Output is correct |
34 |
Correct |
8 ms |
3780 KB |
Output is correct |
35 |
Correct |
18 ms |
7772 KB |
Output is correct |
36 |
Correct |
3 ms |
2364 KB |
Output is correct |
37 |
Correct |
15 ms |
8524 KB |
Output is correct |
38 |
Correct |
22 ms |
9804 KB |
Output is correct |
39 |
Correct |
21 ms |
9816 KB |
Output is correct |
40 |
Correct |
21 ms |
9820 KB |
Output is correct |
41 |
Correct |
21 ms |
9812 KB |
Output is correct |
42 |
Correct |
4 ms |
2524 KB |
Output is correct |
43 |
Correct |
4 ms |
2528 KB |
Output is correct |
44 |
Correct |
4 ms |
2528 KB |
Output is correct |
45 |
Correct |
24 ms |
13972 KB |
Output is correct |
46 |
Correct |
24 ms |
13916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 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 |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1628 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
2 ms |
1628 KB |
Output is correct |
18 |
Correct |
1 ms |
1372 KB |
Output is correct |
19 |
Correct |
1 ms |
1372 KB |
Output is correct |
20 |
Correct |
1 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1376 KB |
Output is correct |
22 |
Correct |
1 ms |
1376 KB |
Output is correct |
23 |
Correct |
1 ms |
1376 KB |
Output is correct |
24 |
Correct |
2 ms |
1628 KB |
Output is correct |
25 |
Correct |
2 ms |
1632 KB |
Output is correct |
26 |
Correct |
1 ms |
1380 KB |
Output is correct |
27 |
Correct |
1 ms |
1372 KB |
Output is correct |
28 |
Correct |
3 ms |
1888 KB |
Output is correct |
29 |
Correct |
4 ms |
2560 KB |
Output is correct |
30 |
Correct |
2 ms |
1636 KB |
Output is correct |
31 |
Correct |
3 ms |
1936 KB |
Output is correct |
32 |
Correct |
2 ms |
1688 KB |
Output is correct |
33 |
Correct |
7 ms |
3568 KB |
Output is correct |
34 |
Correct |
7 ms |
3628 KB |
Output is correct |
35 |
Correct |
16 ms |
7700 KB |
Output is correct |
36 |
Correct |
3 ms |
2408 KB |
Output is correct |
37 |
Correct |
16 ms |
8656 KB |
Output is correct |
38 |
Correct |
22 ms |
9828 KB |
Output is correct |
39 |
Correct |
22 ms |
9900 KB |
Output is correct |
40 |
Correct |
24 ms |
9832 KB |
Output is correct |
41 |
Correct |
23 ms |
9988 KB |
Output is correct |
42 |
Correct |
5 ms |
2524 KB |
Output is correct |
43 |
Correct |
4 ms |
2528 KB |
Output is correct |
44 |
Correct |
4 ms |
2528 KB |
Output is correct |
45 |
Correct |
28 ms |
13924 KB |
Output is correct |
46 |
Correct |
28 ms |
13920 KB |
Output is correct |
47 |
Correct |
40 ms |
22612 KB |
Output is correct |
48 |
Correct |
20 ms |
9672 KB |
Output is correct |
49 |
Correct |
17 ms |
7768 KB |
Output is correct |
50 |
Correct |
12 ms |
6616 KB |
Output is correct |
51 |
Correct |
53 ms |
14660 KB |
Output is correct |
52 |
Correct |
55 ms |
15220 KB |
Output is correct |
53 |
Correct |
29 ms |
9900 KB |
Output is correct |
54 |
Correct |
4 ms |
2652 KB |
Output is correct |
55 |
Correct |
5 ms |
3708 KB |
Output is correct |
56 |
Correct |
8 ms |
4832 KB |
Output is correct |
57 |
Correct |
46 ms |
18392 KB |
Output is correct |
58 |
Correct |
10 ms |
4472 KB |
Output is correct |
59 |
Correct |
10 ms |
5256 KB |
Output is correct |
60 |
Correct |
12 ms |
6528 KB |
Output is correct |
61 |
Correct |
12 ms |
5668 KB |
Output is correct |
62 |
Correct |
26 ms |
16268 KB |
Output is correct |
63 |
Correct |
97 ms |
45960 KB |
Output is correct |
64 |
Correct |
102 ms |
53700 KB |
Output is correct |
65 |
Correct |
138 ms |
72388 KB |
Output is correct |
66 |
Correct |
262 ms |
124100 KB |
Output is correct |
67 |
Correct |
244 ms |
123444 KB |
Output is correct |