其他分享
首页 > 其他分享> > 洛谷试炼场 4-23 莫比乌斯反演

洛谷试炼场 4-23 莫比乌斯反演

作者:互联网


layout: post
title: 洛谷试炼场 4-23 莫比乌斯反演
author: "luowentaoaa"
catalog: true
mathjax: true
tags:
- 莫比乌斯反演
- 数论
- 洛谷


P2257 YY的GCD

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e7+50;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
bool vis[maxn];
ll sum[maxn];
int prim[maxn];
int mu[maxn],g[maxn];
int cnt;
void get_mu(int n){
    mu[1]=1;
    for(int i=2;i<=n;i++)
    {
        if(!vis[i]){mu[i]=-1;prim[++cnt]=i;}
        for(int j=1;j<=cnt&&prim[j]*i<=n;j++)
        {
            vis[i*prim[j]]=1;
            if(i%prim[j]==0)break;
            else mu[prim[j]*i]=-mu[i];
        }
    }
    for(int j=1;j<=cnt;j++)
        for(int i=1;i*prim[j]<=n;i++)g[i*prim[j]]+=mu[i];
    for(int i=1;i<=n;i++)sum[i]=sum[i-1]+g[i];
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    get_mu(10000000);
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        if(n>m)swap(n,m);
        ll ans=0;
        for(int l=1,r;l<=n;l=r+1){
            r=min(n/(n/l),m/(m/l));
            ans+=1LL*(n/l)*(m/l)*(sum[r]-sum[l-1]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

[POI2007]ZAP-Queries

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e5+50;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
bool vis[maxn];
ll sum[maxn];
int prim[maxn];
int mu[maxn],g[maxn];
int cnt;
void get_mu(int n){
    mu[1]=1;
    for(int i=2;i<=n;i++)
    {
        if(!vis[i]){mu[i]=-1;prim[++cnt]=i;}
        for(int j=1;j<=cnt&&prim[j]*i<=n;j++)
        {
            vis[i*prim[j]]=1;
            if(i%prim[j]==0)break;
            else mu[prim[j]*i]=-mu[i];
        }
    }
    for(int i=1;i<=n;i++)sum[i]=sum[i-1]+mu[i];
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    get_mu(50000);
    int t;
    cin>>t;
    while(t--){
        int a,b,d;
        cin>>a>>b>>d;
        int mx=min(a/d,b/d);
        ll ans=0;
        for(int l=1,r;l<=mx;l=r+1){
            r=min((a/d)/((a/d)/l),(b/d)/((b/d)/l));
            ans+=(ll)((a/d)/l)*((b/d)/l)*(sum[r]-sum[l-1]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

P3327 [SDOI2015]约数个数和

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e5+50;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
bool vis[maxn];
ll sum[maxn];
int prim[maxn];
int mu[maxn];
ll g[maxn];
int cnt;
void get_mu(int n){
    mu[1]=1;
    for(int i=2;i<=n;i++)
    {
        if(!vis[i]){mu[i]=-1;prim[++cnt]=i;}
        for(int j=1;j<=cnt&&prim[j]*i<=n;j++)
        {
            vis[i*prim[j]]=1;
            if(i%prim[j]==0)break;
            else mu[prim[j]*i]=-mu[i];
        }
    }
    for(int i=1;i<=n;i++)sum[i]=sum[i-1]+mu[i];
    for(int i=1;i<=n;i++){
        ll ans=0;
        for(int l=1,r;l<=i;l=r+1){
            r=(i/(i/l));
            ans+=1LL*(r-l+1)*1LL*(i/l);
        }
        g[i]=ans;
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    get_mu(50000);
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        int mx=min(n,m);
        ll ans=0;
        for(int l=1,r;l<=mx;l=r+1){
            r=min(n/(n/l),m/(m/l));
            ans+=(1LL*g[n/l]*1LL*g[m/l])*1LL*(sum[r]-sum[l-1]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

[P2522 HAOI2011]Problem b

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e5+50;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
bool vis[maxn];
ll sum[maxn];
int prim[maxn];
int mu[maxn];
ll g[maxn];
int cnt;
int k;
void get_mu(int n){
    mu[1]=1;
    for(int i=2;i<=n;i++)
    {
        if(!vis[i]){mu[i]=-1;prim[++cnt]=i;}
        for(int j=1;j<=cnt&&prim[j]*i<=n;j++)
        {
            vis[i*prim[j]]=1;
            if(i%prim[j]==0)break;
            else mu[prim[j]*i]=-mu[i];
        }
    }
    for(int i=1;i<=n;i++)sum[i]=sum[i-1]+mu[i];
}
ll get(int a,int b){
    int mx=min(a,b);
    ll ans=0;
    for(int l=1,r;l<=mx;l=r+1){
        r=min(a/(a/l),b/(b/l));
        ans+=(1ll*a/(1ll*l*k))*(1ll*b/(1ll*l*k))*(sum[r]-sum[l-1]);
    }
    return ans;
}
// 适用于正负整数
template <class T>
inline bool read(T &ret)
{
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return 0; //EOF
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return 1;
}
inline void out(ll x)
{
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
int main()
{
   /* std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);*/
    get_mu(50000);
    int t;
    //cin>>t;
    read(t);
    while(t--){
        int a,b,c,d;
       // cin>>a>>b>>c>>d>>k;
       read(a);read(b);read(c);read(d);read(k);
        out(get(b,d)-get(b,c-1)-get(a-1,d)+get(a-1,c-1));
        puts("");
    }
    return 0;
}

标签:试炼,洛谷,23,int,ll,get,mu,maxn,const
来源: https://www.cnblogs.com/luowentao/p/10372389.html