HDU 5072 Coprime (莫比乌斯反演+容斥+同色三角形)

78 篇文章 0 订阅
56 篇文章 0 订阅


Coprime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 1469    Accepted Submission(s): 579

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5072

题目大意:给n个互不相同的数,从中选三个,求三个中两两都互质或者两两都不互质的组数

题目分析:开始想歪了,三个数两两都互质并不是说三个数的最大公约数为1,所以正着不好算,考虑算反面即三个中只有一对互质或者只有两对互质的情况,再用C(n,3)减即可,我们把三个数当做三角形的三个顶点A,B,C,枚举顶点A,不妨设A与B互质,A与C不互质,则这样就组成了一对反面情况,至于B,C可以互质也可以不互质,因此算出的结果要除2,具体的说,如果B,C互质,可以交换A,C,如果B,C不互质,可以交换A,B,这样都满足A与B互质,A与C不互质,因此设与A互质的数有k个,则A对答案的贡献为k * (n - k - 1) / 2,计算每个数字对反面情况的贡献数得到反面总的情况数,拿总数减即可,还要注意的是A的值不能为1,因为没有与1不互质的数,接下来就是如何计算k的值,枚举k的因子,它所有因子倍数的个数乘因子对应的莫比乌斯函数值就是k,这里其实直接包含了容斥的过程,它某个因子的倍数可能也是另外一个因子的倍数,举个例子,比如假设最大值为20,A为12,则:
因子 :                       1     2     3    4    6   12
莫比乌斯函数值:      1    -1    -1    0   1    0
因子倍数的个数:     20   10    6    5    3    1
因此答案为20 - 10 - 6 + 3 = 7,即在1到20中与12互质的数有7个,1,5,7,11,13,17,19

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long 
using namespace std;
int const MAX = 1e5 + 5;
int mob[MAX], p[MAX];
int a[MAX], num[MAX], has[MAX], cp[MAX];
bool prime[MAX];
int ma, n;

int Mobius()
{
    int pnum = 0;
    memset(prime, true, sizeof(prime));
    mob[1] = 1;
    for(int i = 2; i < MAX; i++)
    {
        if(prime[i])
        {
            p[pnum ++] = i;
            mob[i] = -1;
        }
        for(int j = 0; j < pnum && i * p[j] < MAX; j++)
        {
            prime[i * p[j]] = false;
            if(i % p[j] == 0)
            {
                mob[i * p[j]] = 0;
                break;
            }
            mob[i * p[j]] = -mob[i];
        }
    }
}

ll cal()
{
    ll all = (ll) n * (n - 1) * (n - 2) / 6;
    memset(cp, 0, sizeof(cp));
    memset(num, 0, sizeof(num));
    for(int i = 1; i <= ma; i++)
    {
        for(int j = i; j <= ma; j += i)
            num[i] += has[j];
        for(int j = i; j <= ma; j += i)
            cp[j] += mob[i] * num[i];
    }
    ll no = 0;
    for(int i = 0; i < n; i++)
        if(a[i] != 1)
            no += (ll) cp[a[i]] * (n - cp[a[i]] - 1);
    return all - no / 2;
}

int main()
{
    Mobius();
    int T;
    scanf("%d", &T);
    while(T--)
    {
        memset(has, 0, sizeof(has));
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
            has[a[i]] ++;
            ma = max(a[i], ma);
        }
        printf("%I64d\n", cal());
    }
}





 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值