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 ll long long
int const N=1e5+5;
int const mod=1e9+7;
set<int> com[N];
int rep[N];
set<int> qr[N];
map<pair<int,int>,int> mp;
vector<pair<int,int>> queries;
int main(){
int n,m,q;
cin>>n>>m>>q;
for (int i = 0; i < q; ++i)
{
int a,b;
cin>>a>>b;
qr[a].insert(b);
qr[b].insert(a);
queries.push_back({a,b});
}
for(int i=1;i<=n;i++){
rep[i]=i;
com[i].insert(i);
}
for(int i=m;i>=1;i--){
for(int j=2*i;j<=n;j+=i){
if(rep[i]==rep[j])
continue;
// cout<<i<<' '<<j<<endl;
int u=rep[i],v=rep[j];
if(com[u].size()<com[v].size())
swap(u,v);
for(int c:com[v]){
// cout<<"com"<<endl;
// cout<<c<<endl;
// rep[c]=u;
// com[u].insert(i);
// cout<<"QR"<<endl;
for(auto q:qr[c]){
// cout<<q<<endl;
if(mp.find({c,q})==mp.end() && com[u].find(q)!=com[u].end()){
mp[{c,q}]=i;
mp[{q,c}]=i;
}
}
}
for(int c:com[v]){
com[u].insert(c);
rep[c]=u;
}
com[v].clear();
}
}
for(auto p:queries){
if(mp[p]==0)
cout<<-1<<endl;
else
cout<<(m+1)-mp[p]<<endl;
}
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... |