This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define double long double
const int MAXN = 14000000;
const int MAXN2 = 10400000;
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 << ": "; }
int n, m, s, nxt;
pair<int, int> cmds[MAXN];
int adj[MAXN];
int id;
int ps[MAXN2];
short cur[MAXN2];
int dq[MAXN];
int dist[MAXN2];
bitset<MAXN2>vis;
void add_command(int u, int v) {
cmds[id] = {u,v};
ps[u]++;
id++;
}
void construct(int b, int p) {
for(int j=b%p; j<n; j+=p) {
if(j+p < n) add_command(nxt + j/p, (nxt + j/p + 1) * 2 + 1); //adj[nxt + j/p].push_back((nxt + j/p + 1) * 2 + 1);
if(j-p >= 0) add_command(nxt + j/p, (nxt + j/p - 1) * 2 + 1); //adj[nxt + j/p].push_back((nxt + j/p - 1) * 2 + 1);
add_command(nxt + j/p, j * 2); //adj[nxt + j/p].push_back(j * 2 + 0);
}
add_command(b, (nxt + b/p) * 2); //adj[b].push_back((nxt + b/p) * 2 + 0);
for(int j=b%p; j<n; j+=p) {
nxt++;
}
}
void solve(int tc) {
cin >> n >> m;
s = 50;//ceil(sqrt(n));
nxt = n;
int st, en;
unordered_map<int, int> bruh[30001];
for(int i=0; i<m; i++) {
int b, p;
cin >> b >> p;
if(i == 0) st = b;
if(i == 1) en = b;
if(!bruh[p].count(b%p) && p<=s) {
bruh[p][b%p] = nxt;
construct(b, p);
}
else if(p<=s) {
add_command(b, (bruh[p][b%p] + b/p) * 2);
}
else {
for(int j=b%p; j<n; j+=p) {
if(j+p < n && j >= b) add_command(nxt + j/p, (nxt + j/p + 1) * 2 + 1); //adj[nxt + j/p].push_back((nxt + j/p + 1) * 2 + 1);
if(j-p >= 0 && j <= b) add_command(nxt + j/p, (nxt + j/p - 1) * 2 + 1); //adj[nxt + j/p].push_back((nxt + j/p - 1) * 2 + 1);
add_command(nxt + j/p, j * 2); //adj[nxt + j/p].push_back(j * 2 + 0);
}
add_command(b, (nxt + b/p) * 2); //adj[b].push_back((nxt + b/p) * 2 + 0);
for(int j=b%p; j<n; j+=p) {
nxt++;
}
}
}
for(int i=1; i<MAXN2; i++) ps[i]+=ps[i-1];
for(int i=0; i<MAXN2; i++) cur[i]=0;
for(int i=0; i<id; i++) {
int u = cmds[i].first, v = cmds[i].second;
adj[cur[u]+(u==0?0:ps[u-1])] = v;
cur[u]++;
}
int k = nxt;
for(int i=0; i<k; i++) dist[i] = 1e9, vis[i] = 0;
dist[st] = 0;
int head=0, tail=1;
dq[0]=st;
while(head!=tail) {
int f = dq[head];
head=(head+1)%MAXN;
if(!vis[f]) {
vis[f] = 1;
if(f==en)break;
int l, r;
if(f==0) l=0, r=ps[0]-1;
else l=ps[f-1], r=ps[f]-1;
for(int y=l; y<=r; y++) {
int x = adj[y];
if(!vis[x / 2] && dist[x / 2] > dist[f] + (x % 2)) {
dist[x / 2] = dist[f] + (x % 2);
if(x % 2 == 0) {
head=(head+MAXN-1)%MAXN;
dq[head]=x/2;
}
else dq[tail++]=x/2;
}
}
}
}
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 (stderr)
skyscraper.cpp: In function 'void solve(int)':
skyscraper.cpp:96:7: warning: 'en' may be used uninitialized in this function [-Wmaybe-uninitialized]
96 | if(f==en)break;
| ^~
skyscraper.cpp:90:8: warning: 'st' may be used uninitialized in this function [-Wmaybe-uninitialized]
90 | dq[0]=st;
| ~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |