Coprime HDU - 5072


Coprime  HDU - 5072 


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


ti题意: 给你 n 个数,让你求  取出三个数,三个这三个数两两互质或者两两不互质  的方案数。


思路: 显然直接求方案数没有头绪,我们考虑先求出总的方案数 "C(n,3) ", 然后减去不符合条件的,

那么对于不符合条件的: 可以简化三元为二元,即 不符合条件的必定是一组 gcd(a,b)=1  一组gcd(b,c) !=1;

那么我们可以直接枚举  b,  对于每一个 b  找出与他不互质的数的个数 k-1,(除去他自己)  与他互质的数的个数有

(n-k); 

那么方案数 ans=(n-k)*(k-1)/2 ;

那么对于  K  ,我们可以用容斥求解;

那么最后的答案就是  tot=C(n,3)- ans';

具体看 :点击打开链接

注意预处理, 可能会  T ;

代码有些不好理解,看了好久~~~


#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include <bits/stdc++.h>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#define maxn 100010
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MOD 1000000007
#define ll long long
using namespace std;

int A[maxn];
int n;
int len1;
bool vis[maxn],Vis[maxn];
int pri[maxn];
int fac[maxn];
int F[maxn][maxn/1000];
int siz[maxn];
int ret[maxn];
void init()
{
    len1=0;
    memset(vis,0,sizeof vis);
    for(int i=2;i<maxn;i++)
    {
        if(!vis[i])
            pri[len1++]=i;
        for(int j=0;j<len1&&i*pri[j]<maxn;i++)
        {
            vis[i*pri[j]]=0;
            if(i%pri[j]==0)  break;
        }
    }
}
void Init()
{
    int len;
    for(int i=1;i<maxn;i++)
    {
        int x=i;
        len=0;
        for(int k=0;pri[k]*pri[k]<=x;k++)
        {
            if(x%pri[k] ==0 )
            {
                F[i][len++]=pri[k];
                x/=pri[k];
                while(x%pri[k]==0)
                    x/=pri[k];
            }
        }
        if(x>1)        F[i][len++]=x;
        siz[i]=len;
    }
}
ll func()
{
    memset(ret,0,sizeof ret);
    for(int i=1;i<maxn;i++)
        for(int j=i;j<maxn;j+=i)
        {
            if(Vis[j])
                ret[i]++;
        }
	ll ans=0;
	for(int i=0;i<n;i++)
    {
        int cnt=siz[A[i]];
        int st=1<<cnt;
        ll tmp=0;
        for(int k=1;k<st;k++)
        {
            int val = 1;
            int w = 0;
            for(int j = 0;j < cnt;j++)
            {
                if(k & (1<<j))
                {
                    val *= F[A[i]][j];
                    w ^= 1;
                }
            }
            if(w)  tmp += ret[val]-1;
            else tmp -= ret[val]-1;
        }
        ans+=tmp*=(n-tmp-1);
    }
    return ans;
}
int main()
{
    int T;
    scanf("%d",&T);
    init();
    Init();
    while(T--)
    {
        memset(Vis,0,sizeof Vis);
        scanf("%d",&n);
        for(int i = 0;i < n;i++)
            scanf("%d",&A[i]),Vis[A[i]]=1;
        ll ans = func();
        ll tot =(ll) n*(n-2)*(n-1)/6;
        printf("%lld\n",tot-ans/2);
    }
    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值