#include "bits/stdc++.h"
using namespace std;
const int MAXN = 4e6 + 10;
const int MOD = 1e9 + 7;
#define ll __int128
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;
}
ll read() { int x; cin >> x; return (ll)x; }
long long bm(long long b, long long p) {
if(p==0) return 1 % MOD;
long long r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
long long inv(long long b) {
return bm(b, MOD-2);
}
long long f[MAXN];
long long nCr(int n, int r) {
long long ans = f[n]; ans *= inv(f[r]); ans %= MOD;
ans *= inv(f[n-r]); ans %= MOD; return ans;
}
long long fib[MAXN], lucas[MAXN];
void precomp() {
for(int i=0; i<MAXN; i++) f[i] = (i == 0 ? 1 % MOD : (f[i-1] * i) % MOD);
lucas[0] = 2;
lucas[1] = 1;
for(int i=2; i<MAXN; i++) lucas[i] = (lucas[i-2] + lucas[i-1]) % MOD;
fib[0] = 0;
fib[1] = 1;
for(int i=2; i<MAXN; i++) fib[i] = (fib[i-2] + fib[i-1]) % MOD;
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void gay(int i) { cout << "Case #" << i << ": "; }
int csb(int l, int r, int k) { // count number of [l, r] such that i & 2^k > 0
if(l > r) return 0;
if(l == 0) {
int s = r / (1ll << (k+1)); // number of complete cycles
int t = r % (1ll << (k+1));
int ans = s * (1ll << k);
ans += (t >= (1ll << k) ? t - (1ll << k) + 1 : 0);
return ans;
}
else return csb(0, r, k) - csb(0, l - 1, k);
}
int lis(vector<int> a) {
int n = a.size();
int bucket[n+1];
for(int i=1; i<=n; i++) bucket[i] = 1e18;
int ans = 1;
for(int x: a) {
auto it = lower_bound(bucket + 1, bucket +n +1, x);
int d = distance(bucket, it);
ans = max(ans, d);
bucket[d] = min(bucket[d], x);
}
return ans;
}
vector<pair<int, short> > adj[MAXN];
void solve(int tc) {
int n, m;
cin >> n >> m;
int nxt = n;
int sq = ceil(sqrt(n));
unordered_map<int, int> link[n];
for(int i=1; i<sq; i++) {
for(int j=0; j<i; j++) {
for(int k=j; k<n; k+=i) {
link[k][i] = nxt;
nxt++;
}
for(int k=j; k<n; k+=i) {
if(k+i < n) {
adj[link[k][i]].push_back({link[k+i][i], 1});
adj[link[k+i][i]].push_back({link[k][i], 1});
}
adj[link[k][i]].push_back({k, 0});
}
}
}
int st, en;
for(int i=0; i<m; i++) {
int b, p;
cin >> b >> p;
if(i == 0) st = b;
else if(i == 1) en = b;
if(p >= sq) {
for(int j=b%p; j<n; j+=p) {
link[j][p] = nxt;
nxt++;
}
for(int j=b%p; j<n; j+=p) {
if(j+p < n) {
adj[link[j][p]].push_back({link[j+p][p], 1});
adj[link[j+p][p]].push_back({link[j][p], 1});
}
adj[link[j][p]].push_back({j, 0});
}
}
adj[b].push_back({link[b][p], 0});
}
priority_queue<pair<int, int>,vector<pair<int, int> >,greater<pair<int, int> > > pq;
int dist[nxt];
bool vis[nxt];
for(int i=0; i<nxt; i++) vis[i] = 0, dist[i] = 1e9;
dist[st] = 0;
pq.push({0, st});
while(pq.size()) {
pair<int, int> t = pq.top(); pq.pop();
if(!vis[t.second]) {
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() {
precomp();
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
// I don't know geometry.
// Teaming is unfair.
Compilation message
skyscraper.cpp: In function 'int lis(std::vector<int>)':
skyscraper.cpp:53:39: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
53 | for(int i=1; i<=n; i++) bucket[i] = 1e18;
| ^~~~
skyscraper.cpp: In function 'void solve(int)':
skyscraper.cpp:129:19: warning: 'en' may be used uninitialized in this function [-Wmaybe-uninitialized]
129 | cout << (dist[en] == 1e9 ? -1 : dist[en]) << "\n";
| ~~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
165 ms |
188148 KB |
Output is correct |
2 |
Correct |
143 ms |
188056 KB |
Output is correct |
3 |
Correct |
161 ms |
188156 KB |
Output is correct |
4 |
Correct |
138 ms |
188160 KB |
Output is correct |
5 |
Correct |
137 ms |
188152 KB |
Output is correct |
6 |
Correct |
142 ms |
188120 KB |
Output is correct |
7 |
Correct |
145 ms |
188176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
188176 KB |
Output is correct |
2 |
Correct |
141 ms |
188124 KB |
Output is correct |
3 |
Correct |
137 ms |
188136 KB |
Output is correct |
4 |
Correct |
153 ms |
188152 KB |
Output is correct |
5 |
Correct |
160 ms |
188132 KB |
Output is correct |
6 |
Correct |
150 ms |
188116 KB |
Output is correct |
7 |
Correct |
146 ms |
188068 KB |
Output is correct |
8 |
Correct |
142 ms |
188088 KB |
Output is correct |
9 |
Correct |
159 ms |
188128 KB |
Output is correct |
10 |
Correct |
144 ms |
188348 KB |
Output is correct |
11 |
Correct |
145 ms |
188612 KB |
Output is correct |
12 |
Correct |
148 ms |
188304 KB |
Output is correct |
13 |
Correct |
149 ms |
188204 KB |
Output is correct |
14 |
Correct |
140 ms |
188792 KB |
Output is correct |
15 |
Correct |
144 ms |
188720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
154 ms |
188160 KB |
Output is correct |
2 |
Correct |
149 ms |
188172 KB |
Output is correct |
3 |
Correct |
137 ms |
188060 KB |
Output is correct |
4 |
Correct |
147 ms |
188240 KB |
Output is correct |
5 |
Correct |
141 ms |
188072 KB |
Output is correct |
6 |
Correct |
141 ms |
188108 KB |
Output is correct |
7 |
Correct |
138 ms |
188108 KB |
Output is correct |
8 |
Correct |
139 ms |
188404 KB |
Output is correct |
9 |
Correct |
150 ms |
188200 KB |
Output is correct |
10 |
Correct |
148 ms |
188260 KB |
Output is correct |
11 |
Correct |
139 ms |
188592 KB |
Output is correct |
12 |
Correct |
138 ms |
188304 KB |
Output is correct |
13 |
Correct |
139 ms |
188368 KB |
Output is correct |
14 |
Correct |
145 ms |
188704 KB |
Output is correct |
15 |
Correct |
138 ms |
188768 KB |
Output is correct |
16 |
Correct |
140 ms |
188560 KB |
Output is correct |
17 |
Correct |
151 ms |
190928 KB |
Output is correct |
18 |
Correct |
157 ms |
195280 KB |
Output is correct |
19 |
Correct |
161 ms |
196752 KB |
Output is correct |
20 |
Correct |
168 ms |
196552 KB |
Output is correct |
21 |
Correct |
142 ms |
188988 KB |
Output is correct |
22 |
Correct |
224 ms |
195580 KB |
Output is correct |
23 |
Correct |
161 ms |
194472 KB |
Output is correct |
24 |
Correct |
169 ms |
196532 KB |
Output is correct |
25 |
Correct |
169 ms |
196888 KB |
Output is correct |
26 |
Correct |
169 ms |
196728 KB |
Output is correct |
27 |
Correct |
166 ms |
196700 KB |
Output is correct |
28 |
Correct |
175 ms |
197352 KB |
Output is correct |
29 |
Correct |
166 ms |
196520 KB |
Output is correct |
30 |
Correct |
160 ms |
196468 KB |
Output is correct |
31 |
Correct |
172 ms |
196684 KB |
Output is correct |
32 |
Correct |
169 ms |
197492 KB |
Output is correct |
33 |
Correct |
192 ms |
200884 KB |
Output is correct |
34 |
Correct |
208 ms |
200888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
146 ms |
188148 KB |
Output is correct |
2 |
Correct |
138 ms |
188172 KB |
Output is correct |
3 |
Correct |
140 ms |
188080 KB |
Output is correct |
4 |
Correct |
145 ms |
188160 KB |
Output is correct |
5 |
Correct |
151 ms |
188144 KB |
Output is correct |
6 |
Correct |
140 ms |
188068 KB |
Output is correct |
7 |
Correct |
137 ms |
188176 KB |
Output is correct |
8 |
Correct |
149 ms |
188108 KB |
Output is correct |
9 |
Correct |
142 ms |
188284 KB |
Output is correct |
10 |
Correct |
140 ms |
188364 KB |
Output is correct |
11 |
Correct |
153 ms |
188676 KB |
Output is correct |
12 |
Correct |
144 ms |
188320 KB |
Output is correct |
13 |
Correct |
142 ms |
188300 KB |
Output is correct |
14 |
Correct |
146 ms |
188772 KB |
Output is correct |
15 |
Correct |
145 ms |
188740 KB |
Output is correct |
16 |
Correct |
146 ms |
188652 KB |
Output is correct |
17 |
Correct |
148 ms |
190936 KB |
Output is correct |
18 |
Correct |
158 ms |
195332 KB |
Output is correct |
19 |
Correct |
169 ms |
196708 KB |
Output is correct |
20 |
Correct |
163 ms |
196576 KB |
Output is correct |
21 |
Correct |
142 ms |
189108 KB |
Output is correct |
22 |
Correct |
158 ms |
195532 KB |
Output is correct |
23 |
Correct |
155 ms |
194452 KB |
Output is correct |
24 |
Correct |
165 ms |
196568 KB |
Output is correct |
25 |
Correct |
169 ms |
196820 KB |
Output is correct |
26 |
Correct |
172 ms |
196752 KB |
Output is correct |
27 |
Correct |
186 ms |
196636 KB |
Output is correct |
28 |
Correct |
163 ms |
197296 KB |
Output is correct |
29 |
Correct |
178 ms |
196492 KB |
Output is correct |
30 |
Correct |
190 ms |
196496 KB |
Output is correct |
31 |
Correct |
161 ms |
196756 KB |
Output is correct |
32 |
Correct |
168 ms |
197628 KB |
Output is correct |
33 |
Correct |
205 ms |
200768 KB |
Output is correct |
34 |
Correct |
194 ms |
200824 KB |
Output is correct |
35 |
Correct |
222 ms |
200600 KB |
Output is correct |
36 |
Correct |
165 ms |
192724 KB |
Output is correct |
37 |
Correct |
253 ms |
207700 KB |
Output is correct |
38 |
Correct |
267 ms |
207492 KB |
Output is correct |
39 |
Correct |
243 ms |
207380 KB |
Output is correct |
40 |
Correct |
262 ms |
207504 KB |
Output is correct |
41 |
Correct |
247 ms |
207636 KB |
Output is correct |
42 |
Correct |
166 ms |
197320 KB |
Output is correct |
43 |
Correct |
164 ms |
197160 KB |
Output is correct |
44 |
Correct |
177 ms |
197128 KB |
Output is correct |
45 |
Correct |
503 ms |
232940 KB |
Output is correct |
46 |
Correct |
567 ms |
232848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
142 ms |
188112 KB |
Output is correct |
2 |
Correct |
145 ms |
188092 KB |
Output is correct |
3 |
Correct |
141 ms |
188132 KB |
Output is correct |
4 |
Correct |
149 ms |
188160 KB |
Output is correct |
5 |
Correct |
138 ms |
188160 KB |
Output is correct |
6 |
Correct |
140 ms |
188120 KB |
Output is correct |
7 |
Correct |
150 ms |
188104 KB |
Output is correct |
8 |
Correct |
149 ms |
188172 KB |
Output is correct |
9 |
Correct |
144 ms |
188100 KB |
Output is correct |
10 |
Correct |
138 ms |
188392 KB |
Output is correct |
11 |
Correct |
144 ms |
188576 KB |
Output is correct |
12 |
Correct |
140 ms |
188224 KB |
Output is correct |
13 |
Correct |
141 ms |
188184 KB |
Output is correct |
14 |
Correct |
150 ms |
188772 KB |
Output is correct |
15 |
Correct |
152 ms |
188804 KB |
Output is correct |
16 |
Correct |
140 ms |
188664 KB |
Output is correct |
17 |
Correct |
144 ms |
190820 KB |
Output is correct |
18 |
Correct |
156 ms |
195284 KB |
Output is correct |
19 |
Correct |
162 ms |
196660 KB |
Output is correct |
20 |
Correct |
159 ms |
196556 KB |
Output is correct |
21 |
Correct |
139 ms |
189012 KB |
Output is correct |
22 |
Correct |
157 ms |
195488 KB |
Output is correct |
23 |
Correct |
158 ms |
194508 KB |
Output is correct |
24 |
Correct |
169 ms |
196504 KB |
Output is correct |
25 |
Correct |
160 ms |
196824 KB |
Output is correct |
26 |
Correct |
162 ms |
196716 KB |
Output is correct |
27 |
Correct |
169 ms |
196684 KB |
Output is correct |
28 |
Correct |
164 ms |
197272 KB |
Output is correct |
29 |
Correct |
165 ms |
196476 KB |
Output is correct |
30 |
Correct |
163 ms |
196472 KB |
Output is correct |
31 |
Correct |
182 ms |
196760 KB |
Output is correct |
32 |
Correct |
166 ms |
197452 KB |
Output is correct |
33 |
Correct |
192 ms |
200852 KB |
Output is correct |
34 |
Correct |
200 ms |
200884 KB |
Output is correct |
35 |
Correct |
232 ms |
200508 KB |
Output is correct |
36 |
Correct |
157 ms |
192684 KB |
Output is correct |
37 |
Correct |
245 ms |
207612 KB |
Output is correct |
38 |
Correct |
256 ms |
207296 KB |
Output is correct |
39 |
Correct |
248 ms |
207324 KB |
Output is correct |
40 |
Correct |
251 ms |
207492 KB |
Output is correct |
41 |
Correct |
247 ms |
207492 KB |
Output is correct |
42 |
Correct |
165 ms |
197264 KB |
Output is correct |
43 |
Correct |
171 ms |
197256 KB |
Output is correct |
44 |
Correct |
184 ms |
197064 KB |
Output is correct |
45 |
Correct |
526 ms |
232860 KB |
Output is correct |
46 |
Correct |
483 ms |
232860 KB |
Output is correct |
47 |
Runtime error |
440 ms |
262144 KB |
Execution killed with signal 9 |
48 |
Halted |
0 ms |
0 KB |
- |