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 fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e5+10;
int pai[MAXN], peso[MAXN], val[MAXN];
int find(int x){ return pai[x]==x ? x : find(pai[x]); }
void join(int a, int b, int t){
a=find(a); b=find(b);
if(a==b) return;
if(peso[a]>peso[b]){
pai[b]=a;
val[b]=t;
}
else if(peso[a]<peso[b]){
pai[a]=b;
val[a]=t;
}
else{
pai[a]=b;
val[a]=t;
peso[b]++;
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// freopen("wormhole.in","r",stdin);
// freopen("wormhole.out","w",stdout);
int n, m, q; cin >> n >> m >> q;
memset(val, INF, sizeof val);
for(int i=1;i<=n;i++) pai[i]=i;
vector<vector<int>> divs(m+1);
for(int i=1;i<=n;i++){
for(int j=1;j*j<=i && j<=m;j++){
if(i%j!=0) continue;
divs[j].pb(i);
if(j*j!=i && i/j<=m) divs[i/j].pb(i);
}
}
int cnt=1;
for(int i=m;i>=1;i--){
for(int j=1;j<(int)divs[i].size();j++) join(divs[i][0], divs[i][j], cnt);
cnt++;
}
while(q--){
int a, b; cin >> a >> b;
int ans=0;
while(a!=b){
if(val[a]<=val[b]) ans=max(ans, val[a]), a=pai[a];
else ans=max(ans, val[b]), b=pai[b];
}
cout << ans << "\n";
}
return 0;
}
# | 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... |
# | 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... |