HDU 5072 Coprime(容斥)
2015-09-29 17:49
447 查看
Problem 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 ≤ 105), denoting the number of people. The next line contains n distinct integers a1, a2, . . . , an(1 ≤ ai ≤ 105) separated by
a single space, where ai stands for the id number of the i-th person.
Output
For each test case, output the answer in a line.
Sample Input
Sample Output
Source
2014 Asia AnShan Regional Contest
分析:从反面考虑,找出3元组中互质对数为1和2的
假设和第i个数不互质的个数为x(包含x),那么其个数为:x*(n-x-1)
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 ≤ 105), denoting the number of people. The next line contains n distinct integers a1, a2, . . . , an(1 ≤ ai ≤ 105) separated by
a single space, where ai 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
Source
2014 Asia AnShan Regional Contest
分析:从反面考虑,找出3元组中互质对数为1和2的
假设和第i个数不互质的个数为x(包含x),那么其个数为:x*(n-x-1)
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int INF=0x3f3f3f3f; typedef long long LL; const int maxn=1e5+100; int t,n,tot=0; int num[maxn+100]; vector<int>v[maxn]; int prim[maxn+100],vis[maxn+100]; int A[maxn]; void get_prim() { for(int i=2;i*i<=maxn;i++) if(!vis[i]) { for(int j=i*i;j<=maxn;j+=i) vis[j]=true; } for(int i=2;i<=maxn;i++) if(!vis[i]) prim[tot++]=i; } void solve(int id,int x) { for(int i=0;i<tot&&prim[i]*prim[i]<=x;i++) { if(x%prim[i]==0) { while(x%prim[i]==0) x/=prim[i]; v[id].push_back(prim[i]); } } if(x>1) v[id].push_back(x); int c=v[id].size(); for(int i=1;i<(1<<c);i++) { int s=1; for(int j=0;j<c;j++) { if(i&(1<<j)) s*=v[id][j]; } num[s]++; } } int main() { get_prim(); scanf("%d",&t); while(t--) { scanf("%d",&n); CLEAR(num,0); for(int i=1;i<=n;i++) { v[i].clear(); scanf("%d",&A[i]); solve(i,A[i]); } LL S=0; for(int i=1;i<=n;i++) { int c=v[i].size(); int sum=0; for(int j=1;j<(1<<c);j++) { int s=1,cnt=0; for(int k=0;k<c;k++) { if(j&(1<<k)) { s*=v[i][k]; cnt++; } } if(cnt&1) sum+=num[s]; else sum-=num[s]; } if(sum<=0) continue; S+=1LL*(sum-1)*(n-sum); } LL ans=1LL*n*(n-1)*(n-2)/6; printf("%lld\n",ans-S/2); } return 0; }
相关文章推荐
- 在Visual Studio 2015下配置和测试OpenCV
- CentOS进程管理
- 递归的grep
- rz和sz 和他们的参数们
- PHP计划任务:如何使用Linux的Crontab执行PHP脚本(转载)
- OC 中"烦人"的小特性之getter和setter以及property编译指令
- 【linux学习笔记七】关机重启命令
- 【Hadoop基础】hadoop fs 命令
- copy,mutableCopy 理解
- Linux命令总结
- Linux下的内核测试工具——perf使用简介
- Openfire用户密码加密解密
- Property Animation学习
- 修改tomcat默认的端口号
- OpenCV学习笔记__使用FLANN进行特征点匹配
- Linux内核源码分析--内核启动之(2)Image内核启动(汇编部分)(Linux-3.0 ARMv7) 【转】
- MyEclipse中配置自己的JRE和tomcat
- 在linux下编写动态链接库的步骤
- Linux内核源码分析--内核启动之(1)zImage自解压过程(Linux-3.0 ARMv7) 【转】
- linux操作界面配置