# hdu5072 互素数对统计

C(n,3)-ans即为最终结果

f[i]求解过程：

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
#define ll long long
#define sf scanf
#define pf printf
#define INF 1<<29
#define maxn 100010
#define mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
const ll mol=1000000007;
using namespace std;

int t,n,a[maxn];
int factor[maxn];
int prime[maxn],cnt;
ll ans;
int is_prime[maxn];
int numprime;
int baseprime[maxn];

void make_prime(){
for(int i=2;i<=1000;i++) is_prime[i]=1;
numprime=0;
for(int i=2;i<1000;i++){
if(is_prime[i]){
baseprime[numprime++]=i;
for(int j=i*i;j<1000;j+=i) is_prime[j]=0;
}
}
}

for(int i=1;i*i<=x;i++)
if(x%i==0){
factor[i]++;
if(x!=i*i) factor[x/i]++;
}
}

void divided(int x){
cnt=0;
for(int i=0;i<numprime;i++){
if(baseprime[i]*baseprime[i]>x) break;
if(x%baseprime[i]==0) prime[cnt++]=baseprime[i];
while(x%baseprime[i]==0) x/=baseprime[i];
}
if(x!=1) prime[cnt++]=x;//加入大于sqrt(x)的素因子！
}

ll solve(){
ll ans=0;
for(int i=1;i<(1<<cnt);i++){
int num=0,tmp=1;
for(int j=0;j<cnt;j++)
if((1<<j)&i) { tmp*=prime[j]; num++; }
if(num&1) ans=ans+factor[tmp];
else ans=ans-factor[tmp];
}
return ans;
}

int main(){
//freopen("a.txt","r",stdin);
make_prime();
scanf("%d",&t);
while(t--){
scanf("%d",&n);
ll N=n;
ans=0;mem(factor,0);
for(int i=1;i<=n;i++) { scanf("%d",&a[i]); add(a[i]); }
for(int i=1;i<=n;i++){
divided(a[i]);
ll tmp=solve();
if(tmp) tmp--;//tmp是与a[i]不互素的数的个数，因为包含a[i]本身，所以要减1,而f[i]=N-1-tmp
ans=ans+(N-1-tmp)*tmp;
}
ans=N*(N-1)*(N-2)/6-ans/2;
printf("%lld\n",ans);
}
return 0;
}


const int maxn = 100000+10;
bool not_prime[maxn];
int n,a[maxn],beinum[maxn],f[maxn];
vector<int> prime;
vector<int> factor[maxn],num[maxn];

void make_prime(){
for(int i=2;i<=maxn-5;i++){
if(!not_prime[i]) prime.push_back(i);
for(int j=0;j<=prime.size()&&i*prime[j]<=maxn-5;j++){
not_prime[i*prime[j]]=1;
if(!(i%prime[j])) break;
}
}
}

void Factor(int x,int id){
for(int i=0;i<prime.size()&&prime[i]*prime[i]<=x;i++){
if(x%prime[i]==0){
factor[id].push_back(prime[i]);
int cnt=0;
while(x%prime[i]==0) { x/=prime[i]; cnt++; }
num[id].push_back(cnt);
}
}
if(x!=1) { factor[id].push_back(x),num[id].push_back(1); }

}

void jilu(int i){
int cnt=num[i].size(),tmp;
for(int j=0;j<(1<<cnt);j++){
tmp=1;
for(int k=0;k<cnt;k++) if((1<<k)&j) tmp*=factor[i][k];
beinum[tmp]++;
}
}

int main(){
make_prime();
scanf("%d",&n);
for(int i=1;i<=n;i++) { scanf("%d",&a[i]); Factor(a[i],i); jilu(i); }
for(int i=1;i<=n;i++){
int cnt=num[i].size();
for(int j=0;j<(1<<cnt);j++){
int tmp=1,s=0;
for(int k=0;k<cnt;k++) if((1<<k)&j) tmp*=factor[i][k],s++;
if(s&1) f[i]-=beinum[tmp];
else f[i]+=beinum[tmp];
}
if(i==1) f[i]--;
}
for(int i=1;i<=n;i++) printf("%d\n",f[i]);
}