【莫比乌斯反演-求序列中数字两两互质的对数】ACdreamoj Number theory 1114

Number theory

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
Problem Description

This time I will give you some straight!

Give you a sequence a1, a2, a3...,an, the question is how many pair(ai, aj) that ai and aj is coprime? (i < j)

Input

The input consists of multiple test cases. Each test case contains two lines.

The first line contains a single interger n.

The second line contains n interger.

All the intergers are between 1 and 222222.

Output

For each test case, you should output one line that contains the answer for the test case.

Sample Input
2
1 2
Sample Output
1
Source
yehuijie
Manager

题意:

给你一个序列a,求出这个序列中互质数的有多少对

解题思路:


莫比乌斯反演,网上有个思路讲的比较清晰:

f(d) 为 gcd 恰好为 d 的数的对数, F(d) 为 gcd 为 d 的倍数的对数, μ(d) 表示莫比乌斯函数

F(d) = ∑ f(n) 其中( n % d == 0 )

莫比乌斯反演一下就可以得到, f(d) = ∑ μ(n / d) * F(n) 其中( n % d == 0)

所以我们最后所要的答案就是 f(1), 也就是 ∑ μ(n) * F(n)

下面代码中,cnt[d]是a这个序列中为d的倍数的数字的个数,num[d]是a这个序列中d的个数,所以F[n] = C(cnt[n], 2).

AC代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

const int MAXN = 222222+10;

typedef long long LL;

LL cnt[MAXN];
int mu[MAXN],prime[MAXN];
int num[MAXN];
bool vis[MAXN];
int pcnt;

/*求解Mobinus函数*/
void Mobinus()
{
    memset(vis,0,sizeof(vis));
    pcnt=0;
    mu[1]=1;
    for(int i=2;i<MAXN;i++){
        if(!vis[i]){
            prime[pcnt++]=i;
            mu[i]=-1;
        }
        for(int j=0;j<pcnt&&i*prime[j]<MAXN;j++){
            vis[i*prime[j]]=1;
            if(i%prime[j])mu[i*prime[j]]=-mu[i];//i是素数的情况
            else {
                mu[i*prime[j]]=0;
                break;
            }
        }
    }
}

int main()
{
    Mobinus();
    int n;
    while(scanf("%d",&n)!=EOF){
        LL Max=-99999999,ans=0;
        LL a[MAXN];
        memset(cnt,0,sizeof(cnt));
        memset(num,0,sizeof(num));
        for(int i=0;i<n;i++){
            scanf("%lld",&a[i]);
            Max=max(a[i],Max);
        }
        for(int i=0;i<n;i++){
            num[a[i]]++;
        }
        for(int i=1;i<=Max;i++){
            for(int j=i;j<=Max;j+=i)
                cnt[i]+=num[j];
        }
        for(int i=1;i<=Max;i++){
            ans+=(mu[i]*cnt[i]*(cnt[i]-1))>>1;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值