【模板】莫比乌斯反演(ACWing215)

很久没做过莫比乌斯反演的题了,发现自己忘记莫比乌斯函数的线性筛法了,贴个模板方便复习吧

有一个埃氏筛做法,为了避免弄混,就只记一个好了

#include<bits/stdc++.h>
using namespace std;

#define go(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll;
#define int long long

const int N=50000+10;

int u[N],sum[N],p[N],tot=0;
bool vis[N];

void mobius(){
    u[1]=1;
    go(i,2,50000){
        if(!vis[i]) p[++tot]=i,u[i]=-1;
        for(int j=1;j<=tot&&p[j]*i<=50000;j++){
            vis[i*p[j]]=1;
      if(i%p[j]==0)break;//u函数有平方因子时为0 
      else u[p[j]*i]=-u[i];//可能有平方因子,也可能没有,但是没有时不影响答案 
        }
    }
    go(i,1,50000) sum[i]=sum[i-1]+u[i]; 
}

signed main(){
    //freopen("input.txt","r",stdin);
    //freopen("put.txt","w",stdout);
    int n;read(n);
    int a,b,c;
    mobius();
    while(n--){
        int ans=0;
        scanf("%lld%lld%lld",&a,&b,&c);
        a/=c,b/=c;
        if(a>b) swap(a,b);
        for(int d=1,nd;d<=a;d=nd+1){
            nd=min(a/(a/d),b/(b/d));
            ans+=a/d*(b/d)*(sum[nd]-sum[d-1]);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

转载于:https://www.cnblogs.com/White-star/p/11390866.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值