#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;
#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif
/************ for topcoder by zz1215 *******************/
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a) for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b) for( int i = (a) ; i >= (b) ; i --)
#define S64(a) scanf(in64,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((a)<<1)+1)
#define pb push_back
#define pf push_front
#define X first
#define Y second
#define CL(Q) while(!Q.empty())Q.pop()
#define MM(name,what) memset(name,what,sizeof(name))
#define MC(a,b) memcpy(a,b,sizeof(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define read freopen("out.txt","r",stdin)
#define write freopen("out2.txt","w",stdout)
const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int maxn = 100111;
int n;
int a[maxn];
bool have[maxn];
int s[maxn];
vector<int>p;
vector<int>v;
i64 dfs_ans;
bool isp(int x){
for (int i = 0; p[i] * p[i] <= x; i++){
if (x%p[i] == 0){
return false;
}
}
return true;
}
void getv(int x){
v.clear();
for (int i = 0;p[i]*p[i]<=x; i++){
if (x%p[i] == 0){
v.push_back(p[i]);
while (x%p[i] == 0){
x /= p[i];
}
}
}
if (x != 1){
v.push_back(x);
}
}
void dfs(int now=1,int id=0,int cnt=0){
if (id == v.size()){
if (now > 1){
if (cnt & 1){
dfs_ans += s[now];
}
else{
dfs_ans -= s[now];
}
}
}
else{
dfs(now*v[id], id + 1, cnt + 1);
dfs(now, id + 1, cnt);
}
}
i64 start(){
MM(have, false);
for (int i = 1; i <= n; i++){
have[a[i]] = true;
}
MM(s, 0);
s[1] = n;
for (int x = 2; x < maxn; x++){
for (int i = x; i < maxn; i += x){
if (have[i]){
s[x]++;
}
}
}
i64 ans = 0;
for (int i = 1; i <= n; i++){
getv(a[i]);
dfs_ans = 0;
dfs();
if(a[i]!=1) ans += (i64)(dfs_ans - 1)*(i64)(n - dfs_ans);
}
ans /= 2;
return ans;
}
void initp(){
p.clear();
p.push_back(2);
for (int i = 3; i < maxn; i++){
if (isp(i)){
p.push_back(i);
}
}
}
int main(){
initp();
int T;
cin >> T;
while (T--){
cin >> n;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
i64 ans = (i64)n*(i64)(n-1)*(i64)(n-2);
ans /= 6;
ans = ans - start();
cout << ans << endl;
}
return 0;
}
hdu 5072 2014ACM/ICPC鞍山C题
最新推荐文章于 2016-04-08 18:48:29 发布