ACdreamoj1114(Number theory)莫比乌斯容斥

题意:给n个数,为有多少互质对;

解法:然后求出mou值,然后求出1,2,3...max的倍数的个数,每个出现在gcd中的对数(num[i]*(num[i]-1))/2,乘上mou值进行容斥计算。


代码:

/******************************************************
* author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std;

#define eps 1e-8
#define zero(_) (abs(_)<=eps)
const double pi=acos(-1.0);
typedef long long LL;
const int Max=222225;
const int INF=1000000007;

LL num[Max];
LL rem[Max];
int mou[Max];
int n;
void init()
{
    for(LL i=2; i<Max; i++)
        if(!mou[i])
        {
            mou[i]=i;
            for(LL j=i*i; j<Max; j+=i)
                mou[j]=i;
        }
        mou[1]=1;
        for(int i=2;i<Max;i++)
        {
            if(i/mou[i]%mou[i]==0) mou[i]=0;
            else
            {
                mou[i]=-mou[i/mou[i]];
            }
        }
}
int main()
{
    init();
    while(scanf("%d",&n)==1)
    {
        memset(rem,0,sizeof rem);
        memset(num,0,sizeof num);
        int ma=0;
        for(int i=0; i<n; i++)
        {
            int a;
            scanf("%d",&a);
            ma=max(ma,a);
            rem[a]++;
        }
        ma++;
        for(int i=1; i<ma; i++)
        {
            for(int j=i; j<ma; j+=i)
            {
                num[i]+=rem[j];
            }
        }
        LL ans=0;
        for(int i=1; i<ma; i++)
        {
          ans+=mou[i]*(num[i]*(num[i]-1))/2;
        }
        cout<<ans<<'\n';
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值