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. 【转】tkinter实现的文本编辑器

    此代码是看完如下教学视频实现的,所以算是[转载]吧: 效果:                                                     代码: # -*- encodin ...

  2. java 位运算权限管控(转载)

    这里笔者介绍一种很常用,也比较专业的权限控制思路.这里用java语言描述,其实都差不多的.要换成其他的语言主,自己转一下就可以了.为了方便起见,我们这里定义a^b为:a的b次方.这里,我们为每一个操作 ...

  3. 工厂方法模式(java 设计模式)

    1.工厂方法模式的定义 工厂方法模式使用的频率非常高, 在我们日常的开发中总能见到它的身影. 其定义为:Define an interface for creating an object,but l ...

  4. Web ADF 编程步骤.

    从Web Controls 开始(工具来中的 ArcGIS Web Controls). 访问Resource Manager. 找到待访问的 Resource. 决定 Resource支持哪个 Fu ...

  5. 浅谈MDX处理空值NULL及格式化结果

    MDX查询结果中往往会含有"NULL"值,这是某维度下对应的的量值不存在导致的,为了让报表呈现更好的效果,在有些情况下,需要将"NULL"的切片值置换成0,这些 ...

  6. AngularJs练习Demo18 Resource

    @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...

  7. 高级 JsRender 模板功能

    转自:http://msdn.microsoft.com/zh-cn/magazine/hh975379.aspx 尽管模板很强大,但有时模板引擎提供的现成标准功能无法满足您的需求. 您可能要转换数据 ...

  8. static在类中的定义,和enum的用法

    class A { // static int a = 1;//错误,静态变量在类外定义 static int a; static const int b = 1;//如果是静态成员常量,则可以在类内 ...

  9. uva 10212 - The Last Non-zero Digit.

    #include <cstdio> #define ll long long const ll MOD = 1e9; int main() { ll N, M; while(scanf(& ...

  10. 织梦DEDECMS网站首页如何实现分页翻页

    织梦DEDECMS模板网站首页如何实现首页分页和翻页 方法如下:(三种方法,自己选择一种来实现分页吧) 第一种:调用ajax和参数的(不推荐)1.必须在DEDE首页模板中的<head>&l ...