#include <bits/stdc++.h>
#define int long long
#define double long double
#define ff first
#define ss second
#define endl '\n'
#define ii pair<int, int>
#define mp make_pair
#define mt make_tuple
#define DESYNC ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define pb push_back
#define vi vector<int>
#define vii vector< ii >
#define EPS 1e-9
#define INF 1e18
#define ROOT 1
#define M 1000000007
const double PI = acos(-1);
using namespace std;
inline int mod(int n, int m){ int ret = n%m; if(ret < 0) ret += m; return ret; }
int gcd(int a, int b){
if(a == 0) return b;
else return gcd(b%a, a);
}
int uf[112345];
int sz[112345];
int mx[112345];
vector<int> g[112345];
int L[112345];
int find(int u){
if(uf[u] == u) return u;
else return find(uf[u]);
}
void init(int n, int m){
for(int i=0; i<=n; i++){
uf[i] = i;
sz[i] = 1;
mx[i] = 0;
}
for(int i=m, j = 1; i >= 1; i--, j++){
mx[i] = j;
}
}
int maxl = 0;
void merge(int x, int y){
int a = find(x);
int b = find(y);
if(a == b) return;
if(sz[a] < sz[b]) swap(a,b);
uf[b] = a;
sz[a] += sz[b];
mx[a]= max(mx[a],mx[b]);
}
void go(int u, int l){
L[u] = l;
maxl = max(maxl, l);
for(int v : g[u]){
go(v, l+1);
}
}
int32_t main(){
DESYNC;
int n,m;
cin >> n >> m;
init(n,m);
for(int i=m; i>=1; i--){
for(int j=i; j<=n; j+=i){
merge(i, j);
}
}
for(int i=1; i<=n; i++){
//cout << "i + uf[i]" << i << " + " << uf[i] << endl;
if(uf[i] != i){
g[uf[i]].pb(i);
}
}
go(find(1), 1);
int q;
cin >> q;
while(q--){
int ans = 0;
int u,v;
cin >> u >> v;
if(u == v){
cout << 0 << endl;
}
else {
while(u != v){
if(L[u] > L[v]){
ans = max(ans, mx[u]);
u = uf[u];
}
else {
ans = max(ans, mx[v]);
v = uf[v];
}
}
ans = max(ans, mx[u]);
cout << ans << endl;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
3072 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
3072 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
3328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
3484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
34 ms |
4332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
4764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
46 ms |
5520 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
49 ms |
5904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
70 ms |
7132 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
76 ms |
8160 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |