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 ...
随机推荐
- poj 2155 Matrix---树状数组套树状数组
二维树状数组模版,唯一困难,看题!!(其实是我英语渣) Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22098 ...
- Bzoj3893 [Usaco2014 Dec]Cow Jog
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 302 Solved: 157 Description The cows are out exerci ...
- set集合类型 redis
向名称为key的set中添加元素: 命令:sadd #不允许有重复的值 2 删除名称为key的set中的元素: 命令:srem 3 随机返回并删除名称称为ke ...
- TFS 配置 报表权限问题
通过[项目门户网站]访问,却被提示ReportService权限不足时, 提示:处理报表时出错. (rsProcessingAborted) 对数据集 “dsiteration” 执行查询失败 同样 ...
- vim tab 和4个空格
在.vimrc中添加以下代码后,重启vim即可实现按TAB产生4个空格:set ts=4 (注:ts是tabstop的缩写,设TAB宽4个空格)set expandtab 对于已保存的文件,可以使用 ...
- Log4j 2使用教程
Log4j 2的好处就不和大家说了,如果你搜了2,说明你对他已经有一定的了解,并且想用它,所以这里直接就上手了. 1. 去官方下载log4j 2,导入jar包,基本上你只需要导入下面两个jar包就可以 ...
- Visual Studio 2013 中 mysql 使用 EF6
1.web.config <configSections> <!-- For more information on Entity Framework configuration, ...
- 新浪微博客户端(3)-封装UIBarButtonItem
单独给NavigationBar上的两个NavigationItem设置图片显得比较麻烦,下面对创建单个UIBarButtonItem的过程进行封装. UIBarButtonItem+Extensio ...
- 怎样把excel一列分成多列
1,选定要分列的列. 2,点击“数据”-“分列”. 3,在选项栏中设置如图 4,选择分隔符 4,看,分开了吧!
- 移动端Web开发注意点
不用考虑浏览器兼容性 移动端开发主要对象是手持设备,其中绝大部分是IOS和Android系统,so,在开发此类页面时不必纠结IE和其他一些2B浏览器的兼容性,webkit是本次开发重点. 当然,不同版 ...