#include "bits/stdc++.h"
using namespace std;
#define double long double
const int MAXN = 9500000;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
vector<pair<int, int> >adj[MAXN];
int n, m, s, nxt;
void construct(int b, int p) {
for(int j=b%p; j<n; j+=p) {
if(j+p < n) adj[nxt + j/p].push_back({nxt + j/p + 1, 1});
if(j-p >= 0) adj[nxt + j/p].push_back({nxt + j/p - 1, 1});
adj[nxt + j/p].push_back({j, 0});
}
adj[b].push_back({nxt + b/p, 0});
for(int j=b%p; j<n; j+=p) {
nxt++;
}
}
void solve(int tc) {
cin >> n >> m;
s = 80;//sqrt(n);
nxt = n;
int st, en;
int bruh[s+1][s];
for(int i=1; i<=s; i++) {
for(int j=0; j<i; j++) {
bruh[i][j] = -1;
}
}
for(int i=0; i<m; i++) {
int b, p;
cin >> b >> p;
if(i == 0) st = b;
if(i == 1) en = b;
if(p <= s && bruh[p][b%p] == -1) {
bruh[p][b%p] = nxt;
construct(b, p);
}
else if(p > s) {
for(int j=b%p; j<n; j+=p) {
if(b != j) adj[b].push_back({j, abs(b-j) / p});
}
}
else {
adj[b].push_back({bruh[p][b%p] + b/p, 0});
}
}
int k = nxt;
int dist[k];
bool vis[k];
for(int i=0; i<k; i++) dist[i] = 1e9, vis[i] = 0;
dist[st] = 0;
priority_queue<pair<int,int>, vector<pair<int,int> >,greater<pair<int,int> > >pq;
pq.push({0, st});
while(pq.size()) {
pair<int, int> t = pq.top();
pq.pop();
if(!vis[t.second]) {
if(t.second == en) break;
vis[t.second] = 1;
for(pair<int, int> x: adj[t.second]) {
if(!vis[x.first] && dist[x.first] > dist[t.second] + x.second) {
dist[x.first] = dist[t.second] + x.second;
pq.push({dist[x.first], x.first});
}
}
}
}
cout<<(dist[en]==1e9?-1:dist[en])<<"\n";
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
// 勿忘初衷
Compilation message
skyscraper.cpp: In function 'void solve(int)':
skyscraper.cpp:76:7: warning: 'en' may be used uninitialized in this function [-Wmaybe-uninitialized]
76 | if(t.second == en) break;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
223352 KB |
Output is correct |
2 |
Correct |
98 ms |
223408 KB |
Output is correct |
3 |
Correct |
96 ms |
223316 KB |
Output is correct |
4 |
Correct |
97 ms |
223332 KB |
Output is correct |
5 |
Correct |
99 ms |
223444 KB |
Output is correct |
6 |
Correct |
98 ms |
223356 KB |
Output is correct |
7 |
Correct |
97 ms |
223308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
223324 KB |
Output is correct |
2 |
Correct |
97 ms |
223388 KB |
Output is correct |
3 |
Correct |
101 ms |
223544 KB |
Output is correct |
4 |
Correct |
98 ms |
223308 KB |
Output is correct |
5 |
Correct |
98 ms |
223308 KB |
Output is correct |
6 |
Correct |
97 ms |
223392 KB |
Output is correct |
7 |
Correct |
97 ms |
223308 KB |
Output is correct |
8 |
Correct |
98 ms |
223348 KB |
Output is correct |
9 |
Correct |
104 ms |
223340 KB |
Output is correct |
10 |
Correct |
98 ms |
223440 KB |
Output is correct |
11 |
Correct |
99 ms |
223516 KB |
Output is correct |
12 |
Correct |
98 ms |
223436 KB |
Output is correct |
13 |
Correct |
97 ms |
223468 KB |
Output is correct |
14 |
Correct |
102 ms |
223740 KB |
Output is correct |
15 |
Correct |
99 ms |
223692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
223380 KB |
Output is correct |
2 |
Correct |
99 ms |
223344 KB |
Output is correct |
3 |
Correct |
99 ms |
223368 KB |
Output is correct |
4 |
Correct |
105 ms |
223344 KB |
Output is correct |
5 |
Correct |
99 ms |
223344 KB |
Output is correct |
6 |
Correct |
98 ms |
223332 KB |
Output is correct |
7 |
Correct |
98 ms |
223312 KB |
Output is correct |
8 |
Correct |
97 ms |
223328 KB |
Output is correct |
9 |
Correct |
96 ms |
223308 KB |
Output is correct |
10 |
Correct |
98 ms |
223448 KB |
Output is correct |
11 |
Correct |
100 ms |
223528 KB |
Output is correct |
12 |
Correct |
98 ms |
223348 KB |
Output is correct |
13 |
Correct |
98 ms |
223356 KB |
Output is correct |
14 |
Correct |
98 ms |
223684 KB |
Output is correct |
15 |
Correct |
99 ms |
223692 KB |
Output is correct |
16 |
Correct |
99 ms |
223564 KB |
Output is correct |
17 |
Correct |
99 ms |
223976 KB |
Output is correct |
18 |
Correct |
98 ms |
223564 KB |
Output is correct |
19 |
Correct |
100 ms |
223484 KB |
Output is correct |
20 |
Correct |
99 ms |
223540 KB |
Output is correct |
21 |
Correct |
98 ms |
223400 KB |
Output is correct |
22 |
Correct |
100 ms |
223564 KB |
Output is correct |
23 |
Correct |
98 ms |
223668 KB |
Output is correct |
24 |
Correct |
100 ms |
223864 KB |
Output is correct |
25 |
Correct |
98 ms |
223624 KB |
Output is correct |
26 |
Correct |
100 ms |
223744 KB |
Output is correct |
27 |
Correct |
100 ms |
223672 KB |
Output is correct |
28 |
Correct |
104 ms |
224272 KB |
Output is correct |
29 |
Correct |
108 ms |
226596 KB |
Output is correct |
30 |
Correct |
104 ms |
224212 KB |
Output is correct |
31 |
Correct |
104 ms |
225148 KB |
Output is correct |
32 |
Correct |
104 ms |
224604 KB |
Output is correct |
33 |
Correct |
126 ms |
229544 KB |
Output is correct |
34 |
Correct |
115 ms |
229604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
223324 KB |
Output is correct |
2 |
Correct |
98 ms |
223344 KB |
Output is correct |
3 |
Correct |
98 ms |
223308 KB |
Output is correct |
4 |
Correct |
99 ms |
223328 KB |
Output is correct |
5 |
Correct |
98 ms |
223328 KB |
Output is correct |
6 |
Correct |
99 ms |
223312 KB |
Output is correct |
7 |
Correct |
99 ms |
223316 KB |
Output is correct |
8 |
Correct |
98 ms |
223392 KB |
Output is correct |
9 |
Correct |
97 ms |
223428 KB |
Output is correct |
10 |
Correct |
98 ms |
223476 KB |
Output is correct |
11 |
Correct |
100 ms |
223620 KB |
Output is correct |
12 |
Correct |
100 ms |
223440 KB |
Output is correct |
13 |
Correct |
98 ms |
223464 KB |
Output is correct |
14 |
Correct |
99 ms |
223660 KB |
Output is correct |
15 |
Correct |
102 ms |
223644 KB |
Output is correct |
16 |
Correct |
103 ms |
223544 KB |
Output is correct |
17 |
Correct |
100 ms |
223880 KB |
Output is correct |
18 |
Correct |
99 ms |
223564 KB |
Output is correct |
19 |
Correct |
99 ms |
223556 KB |
Output is correct |
20 |
Correct |
100 ms |
223560 KB |
Output is correct |
21 |
Correct |
101 ms |
223468 KB |
Output is correct |
22 |
Correct |
99 ms |
223512 KB |
Output is correct |
23 |
Correct |
107 ms |
223664 KB |
Output is correct |
24 |
Correct |
101 ms |
223820 KB |
Output is correct |
25 |
Correct |
99 ms |
223620 KB |
Output is correct |
26 |
Correct |
104 ms |
223780 KB |
Output is correct |
27 |
Correct |
99 ms |
223700 KB |
Output is correct |
28 |
Correct |
101 ms |
224304 KB |
Output is correct |
29 |
Correct |
109 ms |
226544 KB |
Output is correct |
30 |
Correct |
102 ms |
224220 KB |
Output is correct |
31 |
Correct |
105 ms |
225060 KB |
Output is correct |
32 |
Correct |
107 ms |
224536 KB |
Output is correct |
33 |
Correct |
120 ms |
229616 KB |
Output is correct |
34 |
Correct |
116 ms |
229568 KB |
Output is correct |
35 |
Correct |
108 ms |
226228 KB |
Output is correct |
36 |
Correct |
100 ms |
223948 KB |
Output is correct |
37 |
Correct |
110 ms |
228536 KB |
Output is correct |
38 |
Correct |
109 ms |
227844 KB |
Output is correct |
39 |
Correct |
111 ms |
227872 KB |
Output is correct |
40 |
Correct |
115 ms |
227860 KB |
Output is correct |
41 |
Correct |
110 ms |
227788 KB |
Output is correct |
42 |
Correct |
102 ms |
224264 KB |
Output is correct |
43 |
Correct |
102 ms |
224196 KB |
Output is correct |
44 |
Correct |
102 ms |
224024 KB |
Output is correct |
45 |
Correct |
145 ms |
235024 KB |
Output is correct |
46 |
Correct |
127 ms |
234808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
223324 KB |
Output is correct |
2 |
Correct |
98 ms |
223380 KB |
Output is correct |
3 |
Correct |
99 ms |
223420 KB |
Output is correct |
4 |
Correct |
100 ms |
223352 KB |
Output is correct |
5 |
Correct |
98 ms |
223404 KB |
Output is correct |
6 |
Correct |
98 ms |
223356 KB |
Output is correct |
7 |
Correct |
99 ms |
223312 KB |
Output is correct |
8 |
Correct |
101 ms |
223308 KB |
Output is correct |
9 |
Correct |
100 ms |
223344 KB |
Output is correct |
10 |
Correct |
98 ms |
223432 KB |
Output is correct |
11 |
Correct |
99 ms |
223536 KB |
Output is correct |
12 |
Correct |
98 ms |
223388 KB |
Output is correct |
13 |
Correct |
98 ms |
223364 KB |
Output is correct |
14 |
Correct |
99 ms |
223736 KB |
Output is correct |
15 |
Correct |
99 ms |
223656 KB |
Output is correct |
16 |
Correct |
102 ms |
223504 KB |
Output is correct |
17 |
Correct |
101 ms |
223960 KB |
Output is correct |
18 |
Correct |
99 ms |
223636 KB |
Output is correct |
19 |
Correct |
99 ms |
223608 KB |
Output is correct |
20 |
Correct |
102 ms |
223564 KB |
Output is correct |
21 |
Correct |
100 ms |
223400 KB |
Output is correct |
22 |
Correct |
99 ms |
223524 KB |
Output is correct |
23 |
Correct |
99 ms |
223576 KB |
Output is correct |
24 |
Correct |
100 ms |
223744 KB |
Output is correct |
25 |
Correct |
101 ms |
223680 KB |
Output is correct |
26 |
Correct |
100 ms |
223820 KB |
Output is correct |
27 |
Correct |
99 ms |
223768 KB |
Output is correct |
28 |
Correct |
103 ms |
224296 KB |
Output is correct |
29 |
Correct |
122 ms |
226524 KB |
Output is correct |
30 |
Correct |
107 ms |
224436 KB |
Output is correct |
31 |
Correct |
104 ms |
225156 KB |
Output is correct |
32 |
Correct |
103 ms |
224544 KB |
Output is correct |
33 |
Correct |
124 ms |
229560 KB |
Output is correct |
34 |
Correct |
126 ms |
229692 KB |
Output is correct |
35 |
Correct |
108 ms |
226208 KB |
Output is correct |
36 |
Correct |
100 ms |
223988 KB |
Output is correct |
37 |
Correct |
111 ms |
228496 KB |
Output is correct |
38 |
Correct |
111 ms |
227904 KB |
Output is correct |
39 |
Correct |
111 ms |
228000 KB |
Output is correct |
40 |
Correct |
111 ms |
227852 KB |
Output is correct |
41 |
Correct |
110 ms |
227788 KB |
Output is correct |
42 |
Correct |
103 ms |
224300 KB |
Output is correct |
43 |
Correct |
102 ms |
224276 KB |
Output is correct |
44 |
Correct |
102 ms |
224048 KB |
Output is correct |
45 |
Correct |
140 ms |
234772 KB |
Output is correct |
46 |
Correct |
127 ms |
234820 KB |
Output is correct |
47 |
Correct |
143 ms |
245196 KB |
Output is correct |
48 |
Correct |
120 ms |
230712 KB |
Output is correct |
49 |
Correct |
108 ms |
226812 KB |
Output is correct |
50 |
Correct |
108 ms |
227724 KB |
Output is correct |
51 |
Correct |
120 ms |
233292 KB |
Output is correct |
52 |
Correct |
132 ms |
234540 KB |
Output is correct |
53 |
Correct |
124 ms |
226836 KB |
Output is correct |
54 |
Correct |
102 ms |
225100 KB |
Output is correct |
55 |
Correct |
102 ms |
226320 KB |
Output is correct |
56 |
Correct |
105 ms |
226272 KB |
Output is correct |
57 |
Correct |
136 ms |
245984 KB |
Output is correct |
58 |
Correct |
116 ms |
227500 KB |
Output is correct |
59 |
Correct |
158 ms |
229072 KB |
Output is correct |
60 |
Correct |
114 ms |
229816 KB |
Output is correct |
61 |
Correct |
113 ms |
228684 KB |
Output is correct |
62 |
Correct |
131 ms |
238444 KB |
Output is correct |
63 |
Correct |
154 ms |
250024 KB |
Output is correct |
64 |
Correct |
157 ms |
257712 KB |
Output is correct |
65 |
Runtime error |
137 ms |
262144 KB |
Execution killed with signal 9 |
66 |
Halted |
0 ms |
0 KB |
- |