Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define maxm 200001
- #define ll long long
- using namespace std;
- ll n, g, q;
- vector<ll> origin(maxm), dest(maxm), root(maxm), child(maxm);
- ll findroot(ll x){
- while(x != root[x])
- x = root[x];
- return x;
- }
- void _union(ll x, ll y){
- ll rootx = findroot(x);
- ll rooty = findroot(y);
- if(rootx == rooty)
- return;
- if(child[rootx] < child[rooty]){
- root[rootx] = root[rooty];
- child[rooty] += child[rootx];
- }
- else{
- root[rooty] = root[rootx];
- child[rootx] += child[rooty];
- }
- }
- void solve(){
- if(!g){
- for(ll i=0; i<q; ++i)
- cout<<1<<endl;
- return;
- }
- for(ll i=1; i<=n; ++i){
- root[i] = i;
- child[i] = 1;
- }
- for(ll i=g+1; i<=n; ++i){
- if(root[i] != i)
- continue;
- for(ll j=2*i; j<=n; j+=i)
- _union(i, j);
- }
- for(ll i=0; i<q; ++i)
- cout<<(findroot(origin[i]) == findroot(dest[i]))<<endl;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin>>n>>g>>q;
- for(ll i=0; i<q; ++i)
- cin>>origin[i];
- cin>>q;
- for(ll i=0; i<q; ++i)
- cin>>dest[i];
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement