题解:
对数等于 u ( x ) ∗ C ( D ( x ) , 3 ) u(x)*C(D(x),3) u(x)∗C(D(x),3)
#include <bits/stdc++.h>
using namespace std;
#define maxn 1000000 + 5
int n, maxai, i, ai, a[maxn], p[maxn], dp[maxn], j;
long long ret, kol;
bool vis[maxn];
int prime[maxn],mu[maxn];
void init_mu(int n){
int cnt=0;
mu[1]=1;
for(int i=2;i<n;i++){
if(!vis[i]){
prime[cnt++]=i;
mu[i]=-1;
}
for(int j=0;j<cnt&&i*prime[j]<n;j++){
vis[i*prime[j]]=1;
if(i%prime[j]==0) {mu[i*prime[j]]=0;break;}
else { mu[i*prime[j]]=-mu[i];}
}
}
}
int main (int argc, char * const argv[]) {
init_mu(maxn);
// Input reading
scanf("%d", &n);
maxai = 0;
for(i = 1; i <= n; i++) {
scanf("%d", &ai);
++a[ai];
maxai = max(maxai, ai);
}
// Mobius function calculation
for(i = 2; i <= maxai; i++) if (!p[i]) {
j = i;
while (j <= maxai) {
if (!p[j]) p[j] = i;
j += i;
}
}
// Calculation M(X) * C(D(X), 3), for all X where this expression is nonzero
for(i = 1; i <= maxai; i++) {
// Mobuis function
if (!mu[i]) continue;
// Calculating D(X). Can be done naively, because [N/1] + [N/2] + [N/3] + ... + [N/N] -> N log N
j = i; kol = 0;
while (j <= maxai) {
kol += a[j];
j += i;
}
// Adding the number of the combinations to the final answer
ret += kol * (kol - 1) * (kol - 2) / 6 * mu[i];
}
cout << ret << endl;
return 0;
}