bzoj2154 (洛谷P1829) : Crash的数字表格 (莫比乌斯反演)

bzoj2154 (洛谷P1829) : Crash的数字表格

题意

i=1nj=1mlcm(i,j) ∑ i = 1 n ∑ j = 1 m l c m ( i , j )

题解

显然

lcm(i,j)=ijgcd(i,j) l c m ( i , j ) = i j g c d ( i , j )

原式变成

i=1nj=1mijgcd(i,j) ∑ i = 1 n ∑ j = 1 m i j g c d ( i , j )

我们换成去枚举 gcd g c d ,可以得到

k=1min(n,m)1ki=1nj=1mij[gcd(i,j)==k] ∑ k = 1 m i n ( n , m ) 1 k ∑ i = 1 n ∑ j = 1 m i j [ g c d ( i , j ) == k ]

k=1min(n,m)1ki=1nkj=1mkijk2ε(gcd(i,j)==1) ∑ k = 1 m i n ( n , m ) 1 k ∑ i = 1 n k ∑ j = 1 m k i j k 2 ε ( g c d ( i , j ) == 1 )

k=1min(n,m)ki=1nkj=1mkijε(gcd(i,j)==1) ∑ k = 1 m i n ( n , m ) k ∑ i = 1 n k ∑ j = 1 m k i j ε ( g c d ( i , j ) == 1 )

k=1min(n,m)ki=1nkj=1mkd|gcd(i,j)μ(d)ij ∑ k = 1 m i n ( n , m ) k ∑ i = 1 n k ∑ j = 1 m k ∑ d | g c d ( i , j ) μ ( d ) i j

再去枚举gcd,得到

k=1min(n,m)kd=1min(nk,mk)μ(d)i=1nkdidj=1mkdjd ∑ k = 1 m i n ( n , m ) k ∑ d = 1 m i n ( n k , m k ) μ ( d ) ∑ i = 1 n k d i d ∑ j = 1 m k d j d

k=1min(n,m)kd=1min(nk,mk)μ(d)d2i=1nkdij=1mkdj ∑ k = 1 m i n ( n , m ) k ∑ d = 1 m i n ( n k , m k ) μ ( d ) ⋅ d 2 ∑ i = 1 n k d i ∑ j = 1 m k d j

k=1min(n,m)kd=1min(nk,mk)μ(d)d2sum(1,nkd)sum(1,mkd) ∑ k = 1 m i n ( n , m ) k ∑ d = 1 m i n ( n k , m k ) μ ( d ) ⋅ d 2 ⋅ s u m ( 1 , n k d ) ⋅ s u m ( 1 , m k d )


代码

#include 
#include 
#include 
using namespace std;
#define mod 20101009
#define N 10000010
#define ll long long
int prime[N],n,m,tot=0;
ll mu[N],ans=0;
bool notprime[N];
void mobius(){
    mu[1]=1;
    memset(notprime,false,sizeof(notprime));
    for(int i=2;i<=n;i++){
        if(!notprime[i]){
            mu[i]=-1;
            prime[++tot]=i;
        }for(int j=1;j<=tot,prime[j]*i<=n;j++){
            notprime[prime[j]*i]=true;
            if(i%prime[j]==0){
                mu[i*prime[j]]=0;
                break;
            }mu[prime[j]*i]=-mu[i];
        }
    }for(int i=2;i<=n;i++) mu[i]=(mu[i-1]+mu[i]*i%mod*i%mod)%mod;
}
ll sum(int x,int y){return ((ll)(x+y)*(y-x+1)/2)%mod;}
ll f(int a,int b){
    if(a>b) swap(a,b);
    ll ans1=0;
    for(int i=1,last=1;i<=a;i=last+1){
        last=min(a/(a/i),b/(b/i));
        ans1=(ans1+(mu[last]-mu[i-1]+mod)%mod*sum(1,a/i)%mod*sum(1,b/i)%mod)%mod;
    }return ans1;
}
int main(){
    scanf("%d%d",&n,&m);
    if(n>m) swap(n,m);
    mobius();
    for(int i=1,last=1;i<=n;i=last+1){
        last=min(n/(n/i),m/(m/i));
        ans=(ans+f(n/i,m/i)%mod*sum(i,last))%mod; 
    }printf("%lld",ans);
    return 0;
} 

你可能感兴趣的:(莫比乌斯反演,数学)