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 = 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 = 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 (stderr)
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;
| ^~
# | 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... |