hdu 5072 Coprime (容斥)
Problem Description
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.
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.
5
1 3 9 10 2
题意:从一个数组中找出所有 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 (容斥)的更多相关文章
- HDU - 4135 Co-prime 容斥定理
题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n ...
- HDU 4135 Co-prime (容斥+分解质因子)
<题目链接> 题目大意: 给定区间[A,B](1 <= A <= B <= 10 15)和N(1 <=N <= 10 9),求出该区间中与N互质的数的个数. ...
- hdu 4135 Co-prime(容斥)
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- hdu 5514 Frogs(容斥)
Frogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- HDU 5213 分块 容斥
给出n个数,给出m个询问,询问 区间[l,r] [u,v],在两个区间内分别取一个数,两个的和为k的对数数量. $k<=2*N$,$n <= 30000$ 发现可以容斥简化一个询问.一个询 ...
- HDU 2588 思维 容斥
求满足$1<=X<=N ,(X,N)>=M$的个数,其中$N, M (2<=N<=1000000000, 1<=M<=N)$. 首先,假定$(x, n)=m$ ...
- hdu 5072 Coprime
http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出 n 个互不相同的数,求满足以下条件的三元无序组的个数:要么两两互质要么两两不互质. 思路:根据 ...
- HDU 1695 GCD 容斥
GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...
- HDU 5514 Frogs 容斥定理
Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...
随机推荐
- 论js闭包的重要性
很久没写博客了,今天发现了一个很有意思的问题,写下来分享一下 话不多说,贴前端代码: <script type="text/javascript" src="js/ ...
- ContextLoaderListener初始化的前后文和DispatcherServlet初始化的上下文关系
ContextLoaderListener初始化的上下文加载的Bean是对于整个应用程序共享的,不管是使用什么表现层技术,一般如DAO层.Service层Bean: DispatcherServlet ...
- linux服务器内存占用太高-释放内存
修改/proc/sys/vm/drop_caches,释放Slab占用的cache内存空间(参考drop_caches的官方文档): Writing to this will cause the ke ...
- 用IO流发送Http请求
package com.j1.mai.action; import java.io.BufferedReader; import java.io.DataOutputStream; import ja ...
- CSharp命名风格
1.大小写约定 为了区分一个标识符中的多个单词,把标识符中的每个单词的首字母大写.不要用下划线来区分单词,或者在标识符中任何地方使用下划线,有两种方式适合大写标识符的字母: PascalCasing( ...
- C#6.0语法糖
using System; using static System.Math;//using static,仅仅引入类中的静态方法 namespace _6._0Syntax { class Prog ...
- 《javascript设计模式》--接口
关于javascript设计模式书中的接口,记录如下 //TODO 增加了一个判断条件,可以只在生产环境中调用 接口var Interface = function(name,methods){ i ...
- ie6下:png图片不透明 和 背景图片为png的节点的内部标签单击事件不响应
1.png图片不透明 少量图片时:使用滤镜: _background:none; _filter:prodig:DXImageTransform.Microsoft.AlphaImageLoader( ...
- MySQL按照汉字的拼音排序(转)
按照汉字的拼音排序,用的比较多是在人名的排序中,按照姓氏的拼音字母,从A到Z排序: 如果存储姓名的字段采用的是GBK字符集,那就好办了,因为GBK内码编码时本身就采用了拼音排序的方法(常用一级汉字37 ...
- proxifier 注册码
https://www.proxifier.com/index.htm L6Z8A-XY2J4-BTZ3P-ZZ7DF-A2Q9C(Portable Edition) 5EZ8G-C3WL5-B56Y ...