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 ...
随机推荐
- [转]ubuntu安装光盘修复grub-rescue引导失败问题
Reference:http://liujianqiao398.blog.163.com/blog/static/181827257201292775649815/ 1.步骤一 以试用方式进入ubun ...
- 通知栏Notification的学习
转:http://blog.csdn.net/yczz/article/details/28416893 在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理 ...
- C/C++ 中的指针
指针(pointer)有两种涵义 一是指C/C++中的一种数据类型(data type). 二是指某个类型为指针的 数据/物件(object). 指针作为一种数据类型,属所谓复合类型(compound ...
- 文件流StreamReader和StreamWriter的使用
using (StreamReader sr = new StreamReader(@"C:\Users\shuai\Desktop\文件流读取.txt", Encoding.De ...
- Centos目录结构详细版
使用linux也有一年多时间了 最近也是一直在维护网站系统主机 下面是linux目录结构说明 本人使用的是centos系统,很久没有发表博文了 近期会整理自己所用所了解知识点,发表linux相关的 ...
- insert 多个values
INSERT INTO `user_mail_attach` VALUES(, , , , , ), (, , , , , ); 这种比写多条insert语句效率高
- mono 3.4.0 make install的时候出现"找不到 Microsoft.Portable.Common.targets 文件”的错误提示解决方法
如果在这时就进行配置安装Mono的话,会在make阶段得到一个“找不到 Microsoft.Portable.Common.targets 文件”的错误提示, 所以需要先进行如下处理: #> c ...
- liunx几台机器直接用ssh链接
1,查看ip sudo ifconfig 2,查看是否安装了ssh server服务 ps -ef |grep ssh 或者ps -aux |grep ssh 看一下里面有没有sshd.如果没有表示 ...
- dedecms删除没有文章的标签
要批量的删除织梦TAG标签,那我们就只能在数据库里做修改了. 登录数据库,在数据库里执行以下SQL语句: delete FROM dede_tagindex where typeid not in ( ...
- cocos2dx的内存管理机制
首先我们必须说一下c++中变量的内存空间的分配问题,我们在c++中写一个类,可以在栈上分配内存空间也可以使用new在堆上分配内存空间,如果类对象是在栈上分配的内存空间,这个内存空间的管理就不是我们的事 ...