hdu 5072 Coprime 容斥原理

Coprime

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


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

Source

求从n个数字中选出a,b,c三个数字,要么两两互质,要么两两不互质,求组合数。

那么解 =  所有组合数 - 互质和不互质同时存在的组合数

那么求出与a[i]不互质的数的个数,就能算出与a[i]互质的数的个数

求法:统计每个数字能做为那么数字的因素

对于一个数字,分解质因数,然后用容斥原理算出与这个数不互质的数的个数。

那么对于a[i],选择一个互质的数的情况*选择一个不互质的数的情况 = 得到的是不满足条件的且包含a[i]的情况。

由于对每个数字都进行处理,最后的结果是不满足情况的数量*2




#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
#define ll long long
#define maxn 100007

int check[maxn];
vector<int> prin[maxn];
int num[maxn];
void init(){
    memset(check,0,sizeof(check));
    for(int i = 2;i < maxn; i++){
        if(check[i] == 0) {
            for(int j = i;j < maxn; j+=i){
                check[j] = 1;
                prin[j].push_back(i);
            }
        }
    }
}

int main(){
    init();
    int n,t,u,s;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        memset(check,0,sizeof(check));
        memset(num,0,sizeof(num));
        for(int i = 0;i < n; i++){
            scanf("%d",&check[i]);
            num[check[i]]++;
        }
        for(int i = 1;i < maxn; i++)
            for(int j = i+i; j < maxn; j+= i)
                num[i] += num[j];
        ll res = 0,ans = 0;
        for(int i = 0;i < n; i++){
            u = check[i];
            res = 0,s = prin[u].size();
            for(int j = 1;j < (1<<s); j++){
                int v = 1,f = 0;
                for(int k = 0;k < s; k++){
                    if((1<<k)&j){
                        f++;
                        v *= prin[u][k];
                    }
                }
                if(f&1) res += num[v]-1;
                else res -= num[v]-1;
            }
            if(res)
            ans += res*(n-res-1);
        }
        ans = 1ll*n*(n-1)*(n-2)/6-ans/2;
        printf("%I64d\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值