hdu 5072 Coprime


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 ≤ 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


题意:找出一个3元集合使集合中的两两互质,或两两不互质的这样的集合的个数。

思路:将每个数都化成点,互质的边为1,不互质的边为0,这样只需找到,边同为1或者同为0的三角形有多少个就行。那我们就先把数进行素因子分解,求得所有数的素因子都有多少个。然后再枚举每个数的素因子情况,进行容斥原理求解出来与其互质的数有多少,然后总数就是sum*(n-1-sum)。

然后再累加起来就行。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
long long cnt[100005];
struct sa{
    long long fen[20];
    long long k;
}p[100500];
bool phi[100500];
long long a[100005];
long long ans;
int main()
{
    for(long long i=0;i<100005;i++)
    p[i].k=0;
    memset(phi,0,sizeof(phi));
    phi[1]=1;
    for(long long i=2;i<100005;i++){
        if(!phi[i]){
            for(long long j=i;j<100005;j+=i){
                phi[j]=1;
                p[j].fen[p[j].k]=i;
                p[j].k++;
            }
        }
    }//素因子分解
    long long t;
    scanf("%I64d",&t);
    while(t--){
        long long n;
        memset(cnt,0,sizeof(cnt));
        scanf("%I64d",&n);
        for(long long i=0;i<n;i++){
            scanf("%I64d",&a[i]);
            for(long long j=1;j<(1<<p[a[i]].k);j++){
                long long tmp=1;
                for(long long k=0;k<p[a[i]].k;k++)
                if(j&(1<<k))tmp*=p[a[i]].fen[k];
                cnt[tmp]++;
            }
        }
        long long ans=0;
        for(long long i=0;i<n;i++){
            long long sum=0;
            for(long long j=1;j<(1<<p[a[i]].k);j++){
                long long tmp=1;
                long long flag=0;
                for(long long k=0;k<p[a[i]].k;k++)
                if(j&(1<<k)){
                    tmp*=p[a[i]].fen[k];
                    flag++;
                }
                if(flag&1)sum+=cnt[tmp];
                else sum-=cnt[tmp];
            }
            if(sum==0)continue;
            ans+=(sum-1)*(n-sum);
        }
        cout<<n*(n-1)*(n-2)/6-ans/2<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值