yzoj2057 x solution to a problem

The meaning of problems: given a set, this set of requirements into two parts, such that all elements of any of a set of elements of another set are two and two prime

violence

Enumerate each element O (n ^ 2) further violence determination gcd = 1, 1 if it is placed in the non-different sets, with disjoint-set number of blocks can be maintained Unicom, the answer is the number of blocks Unicom Save 2 ( no empty set)

Considered on the basis of the optimization of violence, we can find an element with a multiple of its prime factors are not necessarily the same set, we can enumerate the prime factor, and that it is divided in multiple disjoint-set connection with such a plurality of link block is formed, the first prime number can be pretreated with a linear screen, the note is determined to 1.

Code

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7,MAX=1e6+10; 
int T;
long long ans,n,a[MAX],v[MAX],p[MAX],fa[MAX],cnt=0,maxn=-1,book[MAX],sum;
void prime(){
    for(int i=2;i<=MAX;++i){
        if(!v[i]){
            p[++cnt]=i;
            for(int j=i;j<=MAX/i;++j) v[i*j]=1;
        }  
    }
}
long long find(long long x){
    if(fa[x]==x) return x;
    return fa[x]=find(fa[x]);
}
long long clu(long long x,long long y){
    long long tmp=1;
    while(y){
        if(y&1) tmp=(tmp*x)%mod;
        x=(x*x)%mod;
        y=y>>1;
    }
    return tmp%mod;
}
int main(){
    prime();
    scanf("%d",&T);
    while(T--){
        scanf("%lld",&n);
        memset(a,0,sizeof(a));
        memset(book,0,sizeof(book));
        memset(v,0,sizeof(v));
        ans=0,maxn=-1,sum=0;
        for(int i=1;i<=MAX;++i) fa[i]=i;
        for(int i=1;i<=n;++i){
            scanf("%lld",&a[i]);
            maxn=max(maxn,a[i]);
            if(a[i]==1) sum++;
            v[a[i]]++;
        }
        for(int i=1;i<=cnt;++i){
            for(int j=1;j*p[i]<=maxn;++j){
                if(v[j*p[i]]){
                    long long fax=find(p[i]);
                    long long fbx=find(j*p[i]);
                    if(fax!=fbx) fa[fax]=fbx;
                }
            }
        }
        for(int i=1;i<=n;++i){
            if(!book[find(a[i])]) ans++,book[find(a[i])]=1;
        }
        if(sum>1) ans+=(sum-1);
        ans=clu(2,ans);
        ans=(ans-2+mod)%mod;
        printf("%d\n",ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/donkey2603089141/p/11414774.html