hdu 5072 Coprime

Coprime

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

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

先用容斥求出和a[i] 互质的个数num ,然后不符合条件的 就是 num*(n-1-num);

把所有不符合的加起来/2, 就是所有不符合的组数,最后用总的组数减去不合法的就好了

对于 num 的求法:

先用cnt[i] 表示。因子里面有 i 的数的个数。

对于a[i] ,先分解质因子,然后就可以用容斥求了,具体看代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<ctime>
#include<bitset>
#define LL long long
#define INF 999999
#define maxn 500010
using namespace std;

int cnt[maxn] ,a[maxn] ;
vector<int>q;
int main()
{
    int i ,j , n ,m , k , v ;
    LL ans,sum;
    int T ,len,u;
    cin >>T ;
    while(T--)
    {
        scanf("%d",&n) ;
        for( i =1 ; i <= n ;i++)
            scanf("%d",&a[i]) ;
        memset(cnt,0,sizeof(cnt)) ;
        for( i = 1 ; i <= n ;i++)
        {
            for( j = 1 ; j*j <= a[i] ;j++)if(a[i]%j==0)
            {
                cnt[j]++ ;
                if(a[i]/j != j )cnt[a[i]/j]++;
            }
        }
        ans = 0 ;
        for( i = 1 ; i <= n ;i++)
        {
            q.clear();
            m = a[i];
            for( j = 2 ; j*j <= m ;j++)if(m%j==0)
            {
                q.push_back(j) ;
                while(m%j==0) m /= j ;
            }
            if(m != 1) q.push_back(m) ;
            k = q.size() ;
            len = (1<<k) ;
            int hehe=0;
            sum=0;
            u=1;
            for( j = 1 ;j < len ;j++)
            {
                hehe=0;
                u=1;
                for( v = 0 ; v < k ;v++ )
                    if((1<<v)&j)
                {
                    hehe++ ;
                    u *= q[v] ;
                }
                if(hehe&1) sum += cnt[u] ;
                else sum -= cnt[u] ;
            }
            if(sum)sum--;
            ans += sum*(n-1-sum) ;
        }
        ans/=2;
        LL hehe = (LL)n*(n-1)*(n-2)/6 ;
        printf("%I64d\n",hehe-ans) ;
    }
    return 0 ;
}

时间: 2024-12-07 12:22:51

hdu 5072 Coprime的相关文章

hdu 5072 Coprime(数论)

题目链接:hdu 5072 Coprime 题目大意:给定N个数,问能选出多少个3元组,要么[(a, b) = (b, c) = (a, c) = 1] or [(a, b) ≠ 1 and (a, c) ≠ 1 and (b, c) ≠ 1]. 解题思路:这题可以换个角度想,可以将三个数看做三角形的三条边,互质即边的颜色为1,否则为0,那么要求的即为 三条边颜色相同的三角形有多少个. 总的三角形的个数可求,那么如果求出三条边不完全相同的三角形个数,相减一下即可. 枚举顶点,然后确定以该点形成的

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

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

hdu 5072 Coprime(同色三角形+容斥)

pid=5072">http://acm.hdu.edu.cn/showproblem.php?pid=5072 单色三角形模型 现场赛和队友想了3个小时,最后发现想跑偏了.感觉好可惜的一道题,要是知道这个模型....就能够轻松的拿银了啊. . . 题意不再赘述,就是求同色三角形的个数.总的三角形的个数是C(n,3),仅仅需减去不同色的三角形就可以.对于每一个点(数),与它互质的连红边,不互质的连蓝边,那么对于该点不同色三角形个数为蓝边数*红边数/2,由于同一个三角形被计算了两次. 那么同

HDU 5072 Coprime (单色三角形+容斥原理)

题目链接:Coprime 题面: Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1181    Accepted Submission(s): 471 Problem Description There are n people standing in a line. Each of them has a uniq

ACM学习历程—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

hdu 5072 Coprime (容斥)

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 communi

hdu 5072 Coprime 容斥原理

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1509    Accepted Submission(s): 592 Problem Description There are n people standing in a line. Each of them has a unique id number. Now the Ragn

HDU 5072 Coprime 同色三角形问题

好吧,我承认就算当时再给我五个小时我也做不出来. 首先解释同色三角形问题: 给出n(n >= 3)个点,这些点中的一些被涂上了红色,剩下的被涂上了黑色.然后将这些点两两相连,于是每三个点都会组成一个三角形, 即总共有sum = C(3,n)个三角形.对于一个三角形,如果三个点颜色一样则称其为同色三角形. 那么一个很直观的思路就是容斥,sum - 非同色三角形个数ans. ans = (sigma (Xi*Yi) ) / 2;(1 <= i <= n,Xi,Yi分别表示与第 i 个点相连的

HDU 5072 Coprime (单色三角形问题+容斥原理)

我们先来介绍一下单色三角形问题,如下 单色三角形 在空间中给出了n个点.这些点任三点不共线,并且每两个点之间都有一条线相连,每一条线不是红的就是黑的.在这些点和线组成的三角形中,如果一个三角形的三条边的颜色都相同,那么我们就称这个三角形为单色三角形.现给出所有涂红色的线,试求出单色三角形的数目. 任务: 请写一个程序: 从文本文件中读入点数和对红色连线的描述: 找出该图中红色三角形的数目: 把结果输出到文件TRO.OUT中. 输入格式: 在文本文件TRO.IN的第一行包括一个整数n,3 <= n