HDU5072 容斥原理
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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.
Input
For each test case, the first line contains an integer n(3 ≤ n ≤ 10 5), denoting the number of people. The next line contains n distinct integers a 1, a 2, . . . , a n(1 ≤ a i ≤ 10 5) separated by a single space, where a i stands for the id number of the i-th person.
Output
Sample Input
5
1 3 9 10 2
Sample Output
题意:给出n(3 ≤ n ≤ 105)个数字,每个数ai满足1 ≤ ai ≤ 105,求有多少对(a,b,c)
满足[(a, b) = (b, c) = (a, c) = 1] or [(a, b) ≠ 1 and (a, c) ≠ 1 and (b, c) ≠ 1],
都互素或都不互素。
思路:由于数据范围不大10^5以内,总组合数C(n,3) longlong不会爆。
abc两两互质和两两不互质,就对应着两个互质另两个不互质,这两个集合构成了全集U。
不妨把前者称为集合A,后者称为集合B,那么A并B等于U,且A交B为空。U的大小为C(n,3)。
如果a,b,c不符合条件,必然有一对互质,一对不互质,不妨设a,b互质,b,c不互质,
于是我们可以枚举b来统计所有的三元组:如果a,c互质那么这样的三元组中b,c可以互换位置;
如果a,c不互质,那么a,b可以互换位置。每个答案被算了两遍。
所以只要枚举每个b,统计出k个和它不互质的,那么剩下n-1-k个就是和它互质的,
那么三元组就有k*(n-1-k)/2种。
对于b不超过10^5,质因子的个数不超过6个(2*3*5*7*11*13 *17>10^5)。
用状压搜索质因子组成的每个因数,如果某数是该因数的倍数,
那么就说明该数和b是不互质的。利用容斥原理统计出与b不互质的数的综述。
由于数据范围不超过10^5,预处理筛除出每个质数和每个质因子,复杂度为nlogn。
对于具体的n个数,再筛出在n个数中以他们为倍数的数的个数也是nlogn。(代码中用cntExtend[]记录)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = ;
int prim[maxn],isprim[maxn];
int primnum=;
void initprim(){
memset(isprim,-,sizeof isprim);
isprim[]=isprim[]=;
prim[primnum++] = ;
for(int i=;i<maxn;i+=){
isprim[i]=;
}
for(int i=;i<maxn;i+=){
if(isprim[i]){
prim[primnum++]=i;
for(int j=i+i;j<maxn;j+=i){
isprim[j]=;
}
}
}
}
int has[maxn];
int factor[maxn][];
int factornum[maxn];
void getfactor(){
for(int num=;num<maxn;num++){
int n=num,cnt = ;
for(int i=;i<primnum;i++){
if(isprim[n]) {
factor[num][cnt++]=n;
break;
}
if(n%prim[i]==){
factor[num][cnt++]=prim[i];
while(n%prim[i]==){
n/=prim[i];
}
}
}
factornum[num]=cnt;
}
}
int num[maxn],cntExtend[maxn];
void factorExtend(int len){
memset(cntExtend,,sizeof cntExtend);
for(int i=; i<maxn; i++){
for(int j=i; j<maxn; j+=i){
if(has[j])
cntExtend[i]++;
}
}
}
LL solve(int len){
LL re = ;
for(int i=; i<len; i++){
int n = num[i];
if(n==) continue;
int facnum = factornum[n];
LL sum=;
for(int k=(<<facnum)-; k>; k--){
int mul=,b=;
for(int j=; j<facnum; j++){
if((<<j) & k) {
mul*=factor[n][j];
b^=;
}
}
if(b){
sum+=cntExtend[mul]-;
}else{
sum-=cntExtend[mul]-;
}
}
re+=(sum)*(len--sum);
}
return re;
} int main(){
int T,n,x;
initprim();
getfactor();
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(has,,sizeof has);
for(int i=;i<n;i++){
scanf("%d",&x);
has[x]++;
num[i]=x;
}
factorExtend(n);
LL ans = (LL)n*(n-)*(n-)/ - solve(n)/;
printf("%I64d\n",ans);
} }
HDU5072 容斥原理的更多相关文章
- 2014鞍山现场赛C题HDU5072(素筛+容斥原理)
Coprime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- HDU-5072 补集转化+容斥原理
题意:给n个数,求满足一下条件的三元组(a,b,c)数量:a,b,c两两互质或者a,b,c两两不互质. 解法:这道题非常巧妙地运用补集转化和容斥原理.首先我们令这n个数为n个点,然后两两之间连边如果是 ...
- 容斥原理+补集转化+MinMax容斥
容斥原理的思想大家都应该挺熟悉的,然后补集转化其实就是容斥原理的一种应用. 一篇讲容斥的博文https://www.cnblogs.com/gzy-cjoier/p/9686787.html 当我们遇 ...
- hdu4059 The Boss on Mars(差分+容斥原理)
题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设 则 为一阶差分. 二阶差分: n阶差分: 且可推出 性质: 1. ...
- hdu2848 Visible Trees (容斥原理)
题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3028 Solved: 1460[Submit][Sta ...
- ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)
二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...
- HDU5838 Mountain(状压DP + 容斥原理)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5838 Description Zhu found a map which is a N∗M ...
随机推荐
- NOI题库--图论 宗教信仰
1526:宗教信仰 总时间限制: 5000ms 内存限制: 65536kB 描述 世界上有许多宗教,你感兴趣的是你学校里的同学信仰多少种宗教. 你的学校有n名学生(0 < n <= 500 ...
- springMVC实现防止重复提交
参考文档:http://my.oschina.net/mushui/blog/143397
- POJ1002 487-3279
Description 企业喜欢用容易被记住的电话号码.让电话号码容易被记住的一个办法是将它写成一个容易记住的单词或者短语.例如,你需要给滑铁卢大学打电话时,可以拨打TUT-GLOP.有时,只将电话号 ...
- P1391 走廊泼水节
时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 话说,中中带领的OIER们打算举行一次冬季泼水节,当然这是要秘密进行的,绝对不可以让中中知道.不过中中可是老 ...
- jquery------使用jQuery的委托方法
index.jsp <div id="gallery"> <div class="photo"> <img src="a ...
- java 小记
1.获取web项目根目录的绝对路径 request.getContextPath() 获取项目名称,如 /BiYeSheJi getServletContext().getRealPath(& ...
- --hdu 2124 Repair the Wall(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2124 Ac code : #include<stdio.h> #include<st ...
- --hdu 1050 Moving Tables(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 AC code: #include<stdio.h> #include<str ...
- app 后端技术
app 后端技术 一直以来工作的方向是web server,对app server没有什么了解.虽然没有接触过移动app开发,但对app后端技术还是挺有探索欲望的,app应用和web应用在前端的用户习 ...
- Linux中iptables设置详细(转)
无论如何,iptables是一个需要特别谨慎设置的东西,万一服务器不在你身边,而你贸然设置导致无法SSH,那就等着被老板骂吧,呵呵... 以下内容是为了防止这种情况发生而写的,当然很初级,不过一般服务 ...