【BZOJ 3561】 DZY Loves Math VI(莫比乌斯反演)

传送门

考虑有ij=gcd(i,j)lcm(i,j)ij=gcd(i,j)*lcm(i,j)
然后枚举gcdgcd
ans=d=1i=1ndj=1md[gcd(i,j)=1](ijd)dans=\sum_{d=1}\sum_{i=1}^{\frac nd }\sum_{j=1}^{\frac m d}[gcd(i,j)=1](ijd)^d
n>mn>m简单莫反得到
=d=1ddk=1mdμ(k)k2di=1ndkidj=1mdkjd=\sum_{d=1}d^d\sum_{k=1}^{\frac md }\mu(k)k^{2d}\sum_{i=1}^{\frac{n}{dk}}i^d\sum_{j=1}^{\frac m{dk}}j^d
f(n)=i=1nidf(n)=\sum_{i=1}^{n}i^d
=d=1ddk=1mdμ(k)k2df(nkd)f(mkd)=\sum_{d=1}d^d\sum_{k=1}^{\frac md }\mu(k)k^{2d}f(\frac{n}{kd})f(\frac{m}{kd})

然后做就完了
复杂度是调和级数的O(nlogn)O(nlogn)

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob)?EOF:*ib++;
}
inline int read(){
    char ch=gc();
    int res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(int x){return (x<0)?x+mod:x;}
cs int N=500005;
int pr[N],mu[N],tot;
bitset<N> vis;
inline void init(cs int len=N-5){
    mu[1]=1;
    for(int i=2;i<=len;i++){
        if(!vis[i])pr[++tot]=i,mu[i]=mod-1;
        for(int j=1;j<=tot&&i*pr[j]<=len;j++){
            vis[i*pr[j]]=1;
            if(i%pr[j]==0)break;
            mu[i*pr[j]]=mod-mu[i];
        }
    }
}
int n,m,f[N],s[N];
int main(){
    #ifdef Stargazer
    freopen("lx.in","r",stdin);
    #endif
    init();
    n=read(),m=read();
    if(n<m)swap(n,m);
    for(int i=1;i<=n;i++)f[i]=1;
    int ans=0;
    for(int d=1;d<=m;d++){
        int ln=n/d,lm=m/d,ret=0;
        for(int i=1;i<=ln;i++)Mul(f[i],i),s[i]=add(s[i-1],f[i]);
        for(int k=1;k<=lm;k++)
            Add(ret,mul(mul(s[ln/k],s[lm/k]),mul(mu[k],mul(f[k],f[k]))));
        Add(ans,mul(ret,ksm(d,d)));
    }
    cout<<ans<<'\n';
}
posted @ 2020-01-21 11:53  Stargazer_cykoi  阅读(125)  评论(0编辑  收藏  举报