#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 |
1 |
Correct |
17 ms |
11100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
14288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
195 ms |
26048 KB |
Output is correct |
2 |
Correct |
227 ms |
26696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
314 ms |
33736 KB |
Output is correct |
2 |
Correct |
278 ms |
31296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
177 ms |
21704 KB |
Output is correct |
2 |
Correct |
145 ms |
21700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
174 ms |
22988 KB |
Output is correct |
2 |
Correct |
171 ms |
23916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
247 ms |
28360 KB |
Output is correct |
2 |
Correct |
191 ms |
23264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
307 ms |
33804 KB |
Output is correct |
2 |
Correct |
333 ms |
33224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
404 ms |
35576 KB |
Output is correct |
2 |
Correct |
383 ms |
36548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
467 ms |
39376 KB |
Output is correct |
2 |
Correct |
420 ms |
39168 KB |
Output is correct |