hdu5678容斥定理单边三角形

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 ≤ 10 5), denoting the number of people. The next line contains n distinct integers a 1, a 2, . . . , a n(1 ≤ a i ≤ 10 5) separated by a single space, where a i 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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define maxn 100005
ll cnt[maxn];
int a[maxn];
int p[maxn];
bool vis[maxn];
int ans;
int n;
int t;
ll res;

void init()
{
    memset(vis,0,sizeof(vis));
    memset(a,0,sizeof(a));
    memset(cnt,0,sizeof(cnt));
}
void fenjie(ll x)
{ans=0;
    for(int i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            p[ans++]=i;
            while(x%i==0)
                x/=i;
        }
    }
    if(x>1)
        p[ans++]=x;
}
ll solve()
{
ll sum=0;

    for(int i=1;i<(1<<ans);i++)
    {
        ll val=1;
        int f=0;
        for(int j=0;j<ans;j++)
        {
            if(i&(1<<j))
            {
                f++;
                 val*=p[j];
            }
        }
        if(f&1)
            sum+=cnt[val];
        else
            sum-=cnt[val];
    }
    if(sum>0)
        res=res+(ll)(sum-1)*(n-sum);
}
int main()
         {scanf("%d",&t);

         while(t--)
         {init();
             scanf("%d",&n);
             for(int i=1;i<=n;i++)
                {scanf("%d",&a[i]);
                vis[a[i]]=1;
                }
                for(int i=2;i<maxn;i++)
                    for(int j=i;j<maxn;j+=i)
                {
                    if(vis[j])
                        cnt[i]++;
                }
                res=0;

             for(int i =1;i<=n;i++)
             {
                 fenjie(a[i]);
                 solve();
             }
             ll s=(ll)n*(n-1)*(n-2)/6-res/2;
             printf("%lld\n",s);

         }
         return 0;

         }

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/89790872