#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3yqVz8E ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
struct dsu{
int _n;
vi si,par,leb;
dsu(int n=0){
init(n);
}
void init(int n=0){
_n=n;
par=vi(_n,0);
si=vi(_n,0);
leb=vi(_n,-1);
rep(i,n){
si[i]=1;
par[i]=i;
}
}
int parent(int u){return par[u]=(par[u]==u?u:parent(par[u]));}
bool same(int u,int v){return parent(u)==parent(v);}
void merge(int u,int v){
u=parent(u),v=parent(v);
if(!same(u,v)){
if(si[u]<si[v]) swap(u,v);
leb[u]=max(leb[u],leb[v]);
_n-=1;
si[u]+=si[v];
par[v]=u;
}
}
int size(int v=-1){return v==-1?_n:si[parent(v)];}
};
signed main(){
_3yqVz8E;
int n,m,q;
cin>>n>>m>>q;
vec(vec(pii)) qry(n);
rep(i,q){
int u,v;
cin>>u>>v;
u-=1,v-=1;
qry[v].pb(pii(i,u));
}
dsu uf(n);
vi pns(q,m+1);
for(int i=m;i>=1;i--){
for(int j=i;j<=n;j+=i){
uf.merge(i-1,j-1);
}
for(int j=i;j<=n;j+=i){
int v=j-1;
for(auto p:qry[v]){
int u=p.se;
// if(p.fi==1) print("a",v,u,i);
if(uf.same(v,u)){
pns[p.fi]=min(pns[p.fi],m-i+1);
}
}
}
}
rep(i,q){
cout<<(pns[i])<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
2280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
3084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
2776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
3352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
74 ms |
4556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
56 ms |
5744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
113 ms |
6828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
152 ms |
8184 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |