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
 

 题意:从一个数组中找出所有 3个数 都 相互互质 和 相互不互质 的总个数

思路:首先先转化为求 与一个数互质和不互质的个数,然后将互质和不互质相乘,然后总数减去即可。

     接下来就是怎么求互质和不互质,用到了容斥原理来求,模板套一下就可以了

     sum【i】数组表示i这个数是数组中的元素的因子的个数

     该题还可以先求出所有的素数,范围可以自己确定,然后在分解质因数的时候可以起到优化的作用,如注释掉的部分

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 100006
#define ll long long
ll n;
ll a[N];
//ll prime[N];
//ll num[N];
//ll k=0;
ll fac[N];//分解质因数的质因数
ll sum[N];
ll have[N];
/*void init()
{
memset(num,0,sizeof(num));
for(int i=2;i<N;i++)
{
if(!num[i])
{
prime[k++]=i;
for(int j=i;j<N;j+=i)
{
num[j]=1;
}
}
}
}
*/
ll solve()
{
ll ans=;
for(ll i=;i<n;i++)
{
ll m=a[i];
ll num=;
ll cnt=;
for(ll j=;j*j<=m;j++)
{
if(m%j==)
{
fac[num++]=j;
while(m%j==)
m/=j;
}
}
if(m>) fac[num++]=m;
for(ll j=;j<(<<num);j++)
{
ll w=;
ll tmp=;
for(ll k=;k<num;k++)
{
if((<<k)&j)
{
tmp=tmp*fac[k];
w++;
}
}
if(w&) cnt+=sum[tmp];
else cnt-=sum[tmp];
}
if(cnt==) continue;
ans+=(cnt-)*(n-cnt);
}
return ans/;
}
int main()
{
//init();
int t;
scanf("%d",&t);
while(t--)
{
scanf("%I64d",&n);
memset(have,,sizeof(have));
memset(sum,,sizeof(sum)); for(ll i=;i<n;i++) { scanf("%I64d",&a[i]); have[a[i]]=; } for(ll i=;i<N;i++)
{
for(ll j=i;j<N;j+=i)
{
if(have[j])
sum[i]++;
}
}
ll ans=n*(n-)*(n-)/;
ans=ans-solve();
printf("%I64d\n",ans);
}
return ;
}

hdu 5072 Coprime (容斥)的更多相关文章

  1. HDU - 4135 Co-prime 容斥定理

    题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n ...

  2. HDU 4135 Co-prime (容斥+分解质因子)

    <题目链接> 题目大意: 给定区间[A,B](1 <= A <= B <= 10 15)和N(1 <=N <= 10 9),求出该区间中与N互质的数的个数. ...

  3. hdu 4135 Co-prime(容斥)

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  4. hdu 5514 Frogs(容斥)

    Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  5. HDU 5213 分块 容斥

    给出n个数,给出m个询问,询问 区间[l,r] [u,v],在两个区间内分别取一个数,两个的和为k的对数数量. $k<=2*N$,$n <= 30000$ 发现可以容斥简化一个询问.一个询 ...

  6. HDU 2588 思维 容斥

    求满足$1<=X<=N ,(X,N)>=M$的个数,其中$N, M (2<=N<=1000000000, 1<=M<=N)$. 首先,假定$(x, n)=m$ ...

  7. hdu 5072 Coprime

    http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出 n 个互不相同的数,求满足以下条件的三元无序组的个数:要么两两互质要么两两不互质. 思路:根据 ...

  8. HDU 1695 GCD 容斥

    GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...

  9. HDU 5514 Frogs 容斥定理

    Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...

随机推荐

  1. lession2:使用HTTP Cookie 管理器来传递cookies值

    在实际进行压力测试的时候,经常会出现使用cookie传递值的情况,此时就需要使用[HTTP Cookie 管理器]来传递cookie值. 1.参照lession1中,创建线程组.sampler及聚合报 ...

  2. 关于使用axis调用webservice接口方法

    1.概述: 我们有时候会调用webserviec接口,我们向接口发送请求参数,从接口接收返回值. 2.形式: package client; import org.apache.axis.client ...

  3. Mac Dock 效果及原理(勾股定理)

    这个是苹果机上的 Dock 效果,Windows 上也有一款专门的模拟软件——RocketDock. 代码如下: <!doctype html> <html> <head ...

  4. Gson序列化对象时排除字段

    import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; /** *Gson序列化对象排除属性 ...

  5. android键盘事件

    在main.xml文件中代码如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  6. Ngnix安装

    一.pcre安装 yum install pcre 或 https://sourceforge.net/projects/pcre/files/pcre/8.37/ 手动下载后上传至linux 1.y ...

  7. 关于Lambda表达式的理解

    在.NET 1.0的时候,大家都知道我们经常用到的是委托.有了委托呢,我们就可以像传递变量一样的传递方法.在一定程序上来讲,委托是一种强类型的托管的方法指 针,曾经也一时被我们用的那叫一个广泛呀,但是 ...

  8. 解决:用PivotGridControl 与 chartControl 配合使用,Series最大只显示10条

    修改 PivotGridControl  控件的 OptionsChartDataSource.MaxAllowedSeriesCount 的值就可以了  默认为10条

  9. Javascript高级程序设计读书笔记(第10章 DOM)

    第10章 DOM 10.1  节点层次 每个节点都有一个nodeType属性,用于表明节点的类型.任何节点类型必是下面中的一个: Node.Element_NODE(1); NODE.ATTRIBUT ...

  10. php+js 瀑布流源码

    官方网站:更多源码 新浪微博:QQ公众号 QQ:各种源码 602902342 大牛技术群: 452207697 下载地址:http://pan.baidu.com/s/1bnNipI3 密码: h93 ...