您的位置:首页 > 运维架构

HDU 5072 Coprime(容斥)

2015-09-29 17:49 447 查看
Problem Description

There are n people standing in a line. Each of them has a unique id number.

Now the Ragnarok is coming. We should choose 3 people to defend the evil. As a group, the 3 people should be able to communicate. They are able to communicate if and only if their id numbers are pairwise coprime or pairwise not coprime. In other words, if their
id numbers are a, b, c, then they can communicate if and only if [(a, b) = (b, c) = (a, c) = 1] or [(a, b) ≠ 1 and (a, c) ≠ 1 and (b, c) ≠ 1], where (x, y) denotes the greatest common divisor of x and y.

We want to know how many 3-people-groups can be chosen from the n people.



Input

The first line contains an integer T (T ≤ 5), denoting the number of the test cases.

For each test case, the first line contains an integer n(3 ≤ n ≤ 105), denoting the number of people. The next line contains n distinct integers a1, a2, . . . , an(1 ≤ ai ≤ 105) separated by
a single space, where ai stands for the id number of the i-th person.



Output

For each test case, output the answer in a line.



Sample Input

1
5
1 3 9 10 2




Sample Output

4




Source

2014 Asia AnShan Regional Contest


分析:从反面考虑,找出3元组中互质对数为1和2的
假设和第i个数不互质的个数为x(包含x),那么其个数为:x*(n-x-1)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
const int maxn=1e5+100;
int t,n,tot=0;
int num[maxn+100];
vector<int>v[maxn];
int prim[maxn+100],vis[maxn+100];
int A[maxn];
void get_prim()
{
    for(int i=2;i*i<=maxn;i++)
        if(!vis[i])
        {
            for(int j=i*i;j<=maxn;j+=i)
                vis[j]=true;
        }
    for(int i=2;i<=maxn;i++)
        if(!vis[i]) prim[tot++]=i;
}
void solve(int id,int x)
{
    for(int i=0;i<tot&&prim[i]*prim[i]<=x;i++)
    {
        if(x%prim[i]==0)
        {
            while(x%prim[i]==0)
                x/=prim[i];
            v[id].push_back(prim[i]);
        }
    }
    if(x>1)
        v[id].push_back(x);
    int c=v[id].size();
    for(int i=1;i<(1<<c);i++)
    {
        int s=1;
        for(int j=0;j<c;j++)
        {
            if(i&(1<<j))
                s*=v[id][j];
        }
        num[s]++;
    }
}
int main()
{
    get_prim();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        CLEAR(num,0);
        for(int i=1;i<=n;i++)
        {
            v[i].clear();
            scanf("%d",&A[i]);
            solve(i,A[i]);
        }
        LL S=0;
        for(int i=1;i<=n;i++)
        {
            int c=v[i].size();
            int sum=0;
            for(int j=1;j<(1<<c);j++)
            {
                int s=1,cnt=0;
                for(int k=0;k<c;k++)
                {
                    if(j&(1<<k))
                    {
                        s*=v[i][k];
                        cnt++;
                    }
                }
                if(cnt&1)
                    sum+=num[s];
                else
                    sum-=num[s];
            }
            if(sum<=0) continue;
            S+=1LL*(sum-1)*(n-sum);
        }
        LL ans=1LL*n*(n-1)*(n-2)/6;
        printf("%lld\n",ans-S/2);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: