Coprime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 631 Accepted Submission(s): 285
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.
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.
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
题意:
给你N个不同的数,从中拿出三个出来,要求三个都互质或者3个都不互质。N个数拿出三种的总数是C(N,3)我们可以反过来看,就是3个数中有1对互质或者2对互质,再把总数减去这个即是题目要求的解。现在我们只需要求每个数与其他数不互质的个数乘上其他与他互质的个数就可以求出这个值。当然有重复计算了一次所以结果要求除上2。求一个数与其他数不互质的个数需要用到容止原理,先求出这个数(假设是a)的所有素因子,然后枚举这些素因子只能用一次能够组成的数(假设为b),再找出原来数组中被b整除的数,如果b的素因子个数为奇数就相加,为偶数就减去。这就是容斥的过程。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define eps 1e-8
#define M 100005
using namespace std;
int n,a[M+5],p[M+5],c[M+5];
void init()
{
int b[100];
int cnt=0;
scanf("%d",&n);
memset(c,0,sizeof(c));
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
int k=a[i];
cnt=0;
for (int j=1;p[j]*p[j]<=k;j++)
{
if (k % p[j]==0)
{
b[cnt++]=p[j];
while (k % p[j]==0)
k/=p[j];
}
}
if (k!=1)
b[cnt++]=k;
for (int j=1;j<(1<<cnt);j++)
{
int s=1;
for (int l=0;l<cnt;l++)
{
if (j & (1<<l))
s*=b[l];
}
c[s]++;
}
}
}
void solve()
{
LL ans=n;
ans=ans*(n-1)*(n-2)/6;
LL sum=0;
for (int i=1;i<=n;i++)
{
int k=a[i];
int b[100];
int cnt=0;
LL ret=0;
for (int j=1;p[j]*p[j]<=k;j++)
{
if (k % p[j]==0)
{
b[cnt++]=p[j];
while (k % p[j]==0)
k/=p[j];
}
}
if (k!=1)
b[cnt++]=k;
for (int j=1;j<(1<<cnt);j++)
{
int gg=0;
int s=1;
for (int l=0;l<cnt;l++)
{
if (j & (1<<l))
{
gg++;
s*=b[l];
}
}
if (gg % 2==1) ret+=c[s];
else ret-=c[s];
}
if (ret==0) continue;
sum+=(ret-1)*(n-ret);
//cout<<i<<" "<<ret<<" "<<sum<<endl;
}
cout<<ans - sum/2<<endl;
}
int main()
{
int T;
//freopen("out1.txt","w",stdout);
scanf("%d",&T);
memset(p,0,sizeof(p));
memset(a,0,sizeof(a));
int t=0;
for (int i=2;i<=M;i++)
{
if (!a[i])
p[++t]=i;
for (int j=1;p[j]!=0&&(n=p[j]*i)<M;j++)
{
a[n]=1;
if (i % p[j]==0) break;
}
}
while (T--)
{
init();
solve();
}
return 0;
}