G - Coprime

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

There are n people standing in a line. Each of them has a unique id number.

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

The first line contains an integer T (T ≤ 5), denoting the number of the test cases.

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

For each test case, output the answer in a line.
 

Sample Input

1
5
1 3 9 10 2
 

Sample Output

4
 

题意:给出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 容斥原理的更多相关文章

  1. 2014鞍山现场赛C题HDU5072(素筛+容斥原理)

    Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  2. HDU-5072 补集转化+容斥原理

    题意:给n个数,求满足一下条件的三元组(a,b,c)数量:a,b,c两两互质或者a,b,c两两不互质. 解法:这道题非常巧妙地运用补集转化和容斥原理.首先我们令这n个数为n个点,然后两两之间连边如果是 ...

  3. 容斥原理+补集转化+MinMax容斥

    容斥原理的思想大家都应该挺熟悉的,然后补集转化其实就是容斥原理的一种应用. 一篇讲容斥的博文https://www.cnblogs.com/gzy-cjoier/p/9686787.html 当我们遇 ...

  4. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

  5. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

  6. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  7. BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3028  Solved: 1460[Submit][Sta ...

  8. ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)

    二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...

  9. HDU5838 Mountain(状压DP + 容斥原理)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5838 Description Zhu found a map which is a N∗M ...

随机推荐

  1. HDU-1754I Hate It 线段树区间最值

    这道题比较基本,就是用线段树维护区间最值,可以算是模板吧-.. I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768 ...

  2. MVC传值汇总

     方法一: Url传参是通过Get的方式,一般我们都是通过一定规则的Url来传参.比如下面的URL. http://localhost/contorller/action/?Params1=a& ...

  3. TYVJ1939 玉蟾宫

    背景 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地. 描述 这片土地被分成N*M个格子,每个格子里写着'R'或者'F',R代 ...

  4. Python之MySQL

    本文我们为大家介绍 Python3 使用 PyMySQL 连接数据库,并实现简单的增删改查. 什么是 PyMySQL? PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一 ...

  5. MyEclipse------制作通讯录

    addinfo.java public class addinfo extends HttpServlet { private String url="jdbc:mysql://localh ...

  6. VirtualBox安装debian的详细方法步骤

    下面是用VirtualBox安装Debian6的方法和步骤 l 新建一个文件夹,用于存放虚拟硬盘,如Debian l 打开VirtualBox,点击新建 l 输入虚拟机名称,Debian_6 l 给虚 ...

  7. ios 关联对象运用 objc_setAssociatedObject

    点按钮的时候,给alertView添加一个关联对象(被点击这个按钮), objc_setAssociatedObject(alert, &kRepresentedObject, sender, ...

  8. iOS Xcode 调试技巧 全局断点这样加才有意思

    http://blog.sina.com.cn/s/blog_876a2c9901016ezh.html

  9. mac 下终端访问文件出现“Permission Denied”解决方案

    mac 下终端访问文件出现“Permission Denied”解决方案: 一个文件有3种权限,读.写.可执行,你这个文件没有可执行权限,需要加上可执行权限. 1. 终端下先 cd到该文件的目录下 2 ...

  10. [LeetCode] Binary Tree Preorder/Inorder/Postorder Traversal

    前中后遍历 递归版 /* Recursive solution */ class Solution { public: vector<int> preorderTraversal(Tree ...