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 MAX_N 100005
int N ,M ,Q;
int A[MAX_N] ,B[MAX_N];
int ans[MAX_N];
int Parent[MAX_N];
int Find(int U){
return Parent[U] = (Parent[U] == U ? U : Find(Parent[U]));
}
void Union(int U ,int V){
U = Find(U) ,V = Find(V);
Parent[U] = V;
}
int main()
{
cin >> N >> M >> Q;
for(int i=0; i<Q; i++)
cin >> A[i] >> B[i];
iota(Parent ,Parent+N+1 ,0);
for(int i=M; i; i--)
{
for(int j=i+i; j<=N; j+=i)
Union(i ,j);
for(int j=0; j<Q; j++)
if(!ans[j] && Find(A[j]) == Find(B[j]))
ans[j] = M-i+1;
}
for(int j=0; j<Q; j++)
cout << ans[j] << endl;
}
# | 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... |