# Coprime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 142    Accepted Submission(s): 62

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
1 5 1 3 9 10 2

Sample Output
4

Source

code:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>

#define sqr(x) ((x)*(x))
#define LL long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define eps 1e-10
using namespace std;
int mul[100005];
int mu[100005];
int cnt[100005];
int a[100005];
int cop[100005];
int com[100005];
int pri[100005],pn=0;
int main()
{
memset(com,0,sizeof com);
mu[1]=1;
for (int i=2;i<=100000;i++)
{
if (com[i]==0)
{
pri[++pn]=i;
mu[i]=-1;
}
for (int j=1;j<=pn&&pri[j]*i<=100000;j++)
{
if (i%pri[j])
{
mu[i*pri[j]]=-mu[i];
com[i*pri[j]]=1;
}
else
{
mu[i*pri[j]]=0;
com[i*pri[j]]=1;
break;
}
}
}
int T;
scanf("%d",&T);
while (T--)
{
int n;
scanf("%d",&n);
memset(cnt,0,sizeof cnt);
memset(mul,0,sizeof mul);
memset(cop,0,sizeof cop);
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
cnt[a[i]]++;
}
for (int i=1;i<=100000;i++)
for (int j=i;j<=100000;j+=i)
mul[i]+=cnt[j];

for (int i=1;i<=100000;i++)
for (int j=i;j<=100000;j+=i)
cop[j]+=mu[i]*mul[i];
long long ans=(long long)n*(n-1)*(n-2)/6;
// printf("%lld\n",ans);
long long cn=0;
for (int i=1;i<=n;i++)
{
if (a[i]==1) continue;
cn+=(long long)cop[a[i]]*(long long)(n-1-cop[a[i]]);
if ((long long)cop[a[i]]*(long long)(n-1-cop[a[i]])<0) printf("QUQ");
}
// printf("%I64d\n",ans);
// printf("%I64d\n",cn);
ans-=cn/2;
printf("%I64d\n",ans);
}

return 0;
}
;