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 ...
随机推荐
- js阻止表单重复提交
//校验表单的数据 function newFatherModuleVerify() { var moduelName = $('#fatherModule_moduelName').val(); a ...
- [NOIP2009] 提高组 洛谷P1073 最优贸易
题目描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分 为双向通行的道路 ...
- C#获得系统打开的端口和状态
实际是通过c#编程方式调用了CMD命令行,然后调用netstat命令,然后将CMD命令的输出流转到了C#控制台程序上.也可以将结果输出到文件. using System; using System.C ...
- STL Iterators
Summary of Chapter 33 STL Iterators from The C++ Programming Language 4th. Ed., Bjarne Stroustrup. - ...
- POJ2676Sudoku(类似于八皇后)
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16444 Accepted: 8035 Special ...
- PHP防止重复提交表单(helloweba网站经典实例)
<?php session_start(); header("Content-Type:text/html;charset:utf8"); function set_toke ...
- pthread 学习系列 case1-- 共享进程数据 VS 进程
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h& ...
- linux crontab介绍
第1列分钟1-59第2列小时1-23(0表示子夜)第3列日1-31第4列月1-12第5列星期0-6(0表示星期天)第6列要运行的命令 下面是crontab的格式:分 时 日 月 星期 要运行的命令 这 ...
- Windows 下配置使用MemCached(转载)
工具: memcached-1.2.6-win32-bin.zip MemCached服务端程序(for win) Memcached Manager win下的Mem ...
- 如何有效的保护 JAVA 程序
从头到尾保护 JAVA 目前关于 JAVA 程序的加密方式不外乎 JAVA 模糊处理(Obfuscator)和运用 ClassLoader 方法进行加密处理这两种方式(其他的方式亦有,但大多是这两种的 ...