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. 使用GCM服务(Google Cloud Messaging)实现Android消息推送

    最近在网上查了关于很多Android消息推送的资料,其中主要有四种方法. 1) 使用GCM服务(Google Cloud Messaging) 2) 使用XMPP协议(Openfire + Spark ...

  2. EditText设置可以编辑和不可编辑状态

    1.首先想到在xml中设置android:editable="false",但是如果想在代码中动态设置可编辑状态,没有找到对应的函数 2.然后尝试使用editText.setFoc ...

  3. google在线測试练习题1

    Problem You receive a credit C at a local store and would like to buy two items. You first walk thro ...

  4. HDU 2435 There is a war (网络流-最小割)

    There is a war Problem Description       There is a sea.       There are N islands in the sea.       ...

  5. openstack单元測试用组件一览

    声明: 本博客欢迎转发,但请保留原作者信息! 博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! 组件一览 hacking 一 ...

  6. Java学习笔记——JDBC读取properties属性文件

    Java 中的 properties 文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件. 文件的内容是格式是"键=值"(key-valu ...

  7. Java基础知识强化62:Arrays工具类之概述和使用

    1. Arrays工具类: Arrays这个类包含操作数组(比如排序和查找)的各种方法. 2. Arrays的方法: (1)toString方法:把数组转成字符串 public static Stri ...

  8. lvs+keep搭建高可用web服务

    title: lvs+keep搭建高可用web服务 date: 2015-11-26 22:11:55 tags: --- 第一部分 概念 负载均衡 生产环境下必不可少的基础手段当前大部分互联网都使用 ...

  9. 华硕笔记本怎么设置u盘启动(两种方法)

    华硕笔记本怎么设置u盘启动(两种方法) 华硕笔记本怎么设置u盘启动.我想用U盘安装系统但是 我不知道如何设置U盘启动,那么该如何设置呢?下面和大家分享一下我的经验,希望能够帮到大家.如果你的系统是预装 ...

  10. JQ简单图片轮播

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...