Coprime

传送门

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

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const int maxn = 1e5 + 5;
int mob[maxn], p[maxn];
int a[maxn], num[maxn], has[maxn], cp[maxn];
bool prime[maxn];
int ma, n;

/*
莫比乌斯,emmm,都是抄的 
*/

void Mobius()
{
    int pnum = 0;
    memset(prime, true, sizeof(prime));
    mob[1] = 1;
    for(int i = 2; i < maxn; i++)
    {
        if(prime[i])
        {
            p[pnum ++] = i;
            mob[i] = -1;
        }
        for(int j = 0; j < pnum && i * p[j] < maxn; j++)
        {
            prime[i * p[j]] = false;
            if(i % p[j] == 0)
            {
                mob[i * p[j]] = 0;
                break;
            }
            mob[i * p[j]] = -mob[i];
        }
    }
}

ll cal()
{
    ll all = (ll) n * (n - 1) * (n - 2) / 6;
    memset(cp, 0, sizeof(cp));
    memset(num, 0, sizeof(num));
    for(int i = 1; i <= ma; i++)
    {
        for(int j = i; j <= ma; j += i)
            num[i] += has[j];
        for(int j = i; j <= ma; j += i)
            cp[j] += mob[i] * num[i];
    }
    ll ans = 0;
    for(int i = 0; i < n; i++)
        if(a[i] != 1)
            ans += (ll) cp[a[i]] * (n - cp[a[i]] - 1);
    return all - ans / 2;
}

int main()
{
    Mobius();
    int T;
    scanf("%d", &T);
    while(T--)
    {
        memset(has, 0, sizeof(has));
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
            has[a[i]] ++;
            ma = max(a[i], ma);
        }
        printf("%I64d\n", cal());
    }
}
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>

using namespace std;
typedef long long ll;

#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)

const int maxn=1e5+10;
vector<int> vec;
int cnt[maxn],val[maxn],n;


/*
真真切切的体验到了,卡常数(当然是在我的算法不够优化的前提下)
1.能用int,别用long long
2.对于找到sqrt(n),我们还是直接算 i*i吧,太难受了,一下子200ms下去了
*/
/*
题意:问我们在n个数中,有多少 三元组是 都互质,或者都不互质
思路:
  考虑白书上的,单色三角形的计数法则(逆向考虑有多少个不满足条件的,然后C(n,3)减掉即可
1.每个顶点,假设 与M个数互质,则有n-1-m个数不互质,那么就会产生 m*(n-1-m)个 非单色三角形
2.每个非单色三角形都会有两个顶点,连着互质和不互质的两条边,所以算了两次,/2就好了

所以问题变成了,怎样计算与n个数中与某个数字互质的个数?
考虑这个数的素因子,然后容斥定理
*/
inline void get_prime(int X){
//    printf("xxx:%lld\n",X);
   // int m=sqrt(X+0.5);
    for(int i=1;i*i<=X;i++){
        if(X%i==0){
        //    printf("x:%ll i:%lld\n",X,i);
            cnt[i]++;
            if(X!=i*i){
                cnt[X/i]++;
            }
        }
    }
}

//计算n个数中与val[id]互质的数量
inline ll get_ans(int id){
    vec.clear();
    //int m=sqrt(val[id]+0.5),v=val[id];
    int v=val[id];
    for(int i=2;i*i<=v;i++){
        if(v%i==0){
            vec.push_back(i);
            while(v%i==0)v/=i;
        }
    }
    if(v>1) vec.push_back(v);

    ll ans=n;
    int sz=vec.size();ll ma=1ll<<sz;
    rep(i,1,ma){
        ll tmp=1;int t=0;
        rep(j,0,sz){
            if((1<<j)&i){
                t++,tmp*=vec[j];
            }
        }
       // printf("tmp:%lld cnt:%lld\n",tmp,cnt[tmp]);
        if(t&1) ans-=cnt[tmp];
        else ans+=cnt[tmp];
    }
    return ans;
}


int main(){
    int T;
    scanf("%d",&T);
    rep(kase,1,T+1){
       memset(cnt,0,sizeof(cnt));

       scanf("%d",&n);
       rep(i,1,n+1)scanf("%d",&val[i]);

       rep(i,1,n+1)get_prime(val[i]);
       ll ans=0,num1,num2;
       rep(i,1,n+1){
          num1=get_ans(i);
          num2=n-1-num1;
          num2=max(num2,0ll);
          ans+=num1*num2;
        //  printf("i:%d num1:%lld num2:%lld\n",i,num1,num2);
       }
       ll nn=n;
       printf("%lld\n",nn*(nn-1)*(nn-2)/6-ans/2);
    }
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值