hdu 5072 Coprime

Coprime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3395    Accepted Submission(s): 1302


 

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

 

 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6667 6666 6665 6664 6663 

题目大意:给出n个数,求有多少个三元组满足任意两个数互素或者任意两个数都不互素。

解题思路: 同色三角形定理和容斥。

对于一段连续的区间,我们要想知道区间有多少个数与k不互质,只需要做一下除法就行了。

这是对于连续的区间的求法。当然对于不连续的区间可以预处理一个前缀和。表示集合中有

多少个数是概数的倍数。

答案为:从n个数中任意取三个-不满足条件的。

假设每一对互素的数之间连一条红色的边,不 互素的数之间连一条蓝色的边,则我们要求的就是不是

同色三角形的个数。

由于不是同色三角形中有两个定点连接了两条异色的边,并且这两条异色的边唯一的确定了一个三角形。

所以不满足条件的就是对于每个定点  蓝色边*红色边的数量。

由于对于每个三角形算了两次,所以要除以2。

#include<bits/stdc++.h>
#define LL long long
using namespace std;
#define pb(x) push_back(x)

const int N = 2e5+5;
const int maxn = 1e5+100;
int a[N],num[N];


void prework( )
{
    for(int i=2;i<=maxn;i++)
    {
        for(int j=i*2;j<=maxn;j+=i)
            num[i]+=num[j];
    }
}

int b[1005];
int div_fact(int k)
{
    int cnt=0;
    for(int i=2;i*i<=k;)
    {
        if(k%i==0)
        {
            b[cnt++]=i;
            while(k%i==0)k/=i;
        }
        if(i==2)i++;
        else i+=2;
    }
    if(k!=1)b[cnt++]=k;
    return cnt;
}

int notcoprime(int k)
{
    int cnt=div_fact(k);
    int fac=1,sig=1;
    int ans=0;
    for(int i=0;i<(1<<cnt);i++)
    {
        fac=1;
        sig=0;
        for(int j=0;j<cnt;j++)
        {
            if((1<<j)&i)
            {
                fac*=b[j];
                sig++;
            }
        }
        sig=(sig%2==0?-1:1);
        if(fac!=1)
        {
            ans+=sig*num[fac];
        }
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        memset(num,0,sizeof(num));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            num[a[i]]++;
        }
        prework( );
        LL ans=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]==1)continue;
            int k=notcoprime(a[i]);
            ans+=1LL*(k-1)*(n-k);
        }
        LL c=1LL*n*(n-1)*(n-2)/6-ans/2;
        printf("%lld\n",c);
    }
}

补充另外一种求x与集合s里边有多少个互质的。

s里边的每个数,只对他的因子是有贡献的。我们要找到x与集合里边不互质的然后减去。

所以可以用如下方法:

1.将s里边的每个数质因数分解,分解成不同质数的集合,然后枚举这些质数能够产生哪些乘积,

这些乘积就是s的因子。

2.将x质因数分解,利用容斥求解。

#include<bits/stdc++.h>
#define LL long long
using namespace std;
#define pb(x) push_back(x)

const int N = 2e5+5;
const int maxn = 1e5+100;
int a[N],num[N];
int b[1005];

int div_fact(int k)
{
    int cnt=0;
    for(int i=2;i*i<=k;)
    {
        if(k%i==0)
        {
            b[cnt++]=i;
            while(k%i==0)k/=i;
        }
        if(i==2)i++;
        else i+=2;
    }
    if(k!=1)b[cnt++]=k;
    return cnt;
}

void pre_work(int k)
{
    int cnt=div_fact(k);
    for(int i=0;i<(1<<cnt);i++)
    {
        int fac=1;
        int sig=0;
        for(int j=0;j<cnt;j++)
        {
            if((1<<j)&i)
            {
                fac*=b[j];
            }
        }
        num[fac]++;
    }
}

int notcoprime(int k)
{
    int cnt=div_fact(k);
    int fac=1,sig=1;
    int ans=0;
    for(int i=0;i<(1<<cnt);i++)
    {
        fac=1;
        sig=0;
        for(int j=0;j<cnt;j++)
        {
            if((1<<j)&i)
            {
                fac*=b[j];
                sig++;
            }
        }
        sig=(sig%2==0?-1:1);
        if(fac!=1)
        {
            ans+=sig*num[fac];
        }
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        memset(num,0,sizeof(num));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1;i<=n;i++)
        {
            pre_work(a[i]);
        }
        LL ans=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]==1)continue;
            int k=notcoprime(a[i]);
            ans+=1LL*(k-1)*(n-k);
        }
        LL c=1LL*n*(n-1)*(n-2)/6-ans/2;
        printf("%lld\n",c);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值