首页 > 代码库 > hdu 5072

hdu 5072

题意: 求有多少的3元祖,并且每个3元组彼此互质或者不互质,求这样的3元组的个数:
转化为求的n个数中与x互质的数有多少个,可以用容斥原理来做
总结: 一般求因子的倍数的个数,都是用容斥原理

#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL __int64
const int maxn = 1e5+8;
LL a[maxn],cn,numpri[maxn],vis[maxn],dis[maxn];
LL n,m;
LL f[maxn];
void getprim(){
    cn = 0;
    for(LL i=2;i<1000;i++){
        if(!dis[i]){
            numpri[cn++] = i;
            for(LL j=2*i;j<=1000;j+=i)
                dis[j] = 1;
        }
    }
}
LL getans(){
    vector <int > v;

    LL ans1 = 0;
    for(LL i=0;i<n;i++){
            v.clear();
        LL d = a[i],sum = 0;;
        for(LL j=0;j<cn&&numpri[j] * numpri[j] <=d;j++){
            if(d % numpri[j] == 0){
                v.push_back(numpri[j]);
                while(d % numpri[j] == 0)d /= numpri[j];
            }
        }
        if(d!=1)v.push_back(d);
        int len = v.size();
        //cout << len <<endl;
        for(int j=0;j<(1<<len);j++){
            LL op =0,ans = 1;
            for(int k=0;k<len;k++){
                if((1<<k)&j){
                    op++;
                    ans*=v[k];
                }
            }
            if(op&1)sum+=f[ans];
            else sum -= f[ans];
        }
        if(sum <= 0) continue;
        ans1 += (sum - 1) *(n-sum);
    }
    return ans1 /2;
}

int main(){
    LL T;
    scanf("%I64d",&T);
      getprim();
    while(T--){
        memset(f,0,sizeof(f));
        memset(vis,0,sizeof(vis));
        scanf("%I64d",&n);
        for(LL i=0;i<n;i++){
            scanf("%I64d",&a[i]);
            vis[a[i]] = 1;
        }
        for(int i=2;i<maxn;i++){
            for(int j=i;j<maxn;j+=i){
                if(vis[j])f[i]++;
            }
        }
        LL ans = n*(n-1)*(n-2)/6;
        ans -= getans();
        cout << ans<<endl;
    }
}


hdu 5072