bzoj4659 Lcm (莫比乌斯反演)

题意

i=1nj=1mlcm(i,j)(gcd(i,j)) ∑ i = 1 n ∑ j = 1 m l c m ( i , j ) ( g c d ( i , j ) 无 平 方 质 因 子 )

题解

无平方质因子,即 μ2=1 μ 2 = 1
因此式子可以转化为

i=1nj=1mlcm(i,j)(gcd(i,j))=i=1nj=1mlcm(i,j)μ2(gcd(i,j))=i=1nj=1mijgcd(i,j)μ2(gcd(i,j))=d=1min(n,m)1dμ2(d)i=1ndj=1mdijd2[gcd(i,j)==1]gcd=d=1min(n,m)dμ2(d)i=1ndj=1mdij[gcd(i,j)==1]=d=1min(n,m)dμ2(d)i=1ndj=1mdk|gcd(i,j)ijμ(k)=d=1min(n,m)dμ2(d)k=1min(nd,md)i=1nkdj=1mkdijk2μ(k)gcd=d=1min(n,m)dμ2(d)k=1min(nd,md)k2μ(k)i=1nkdij=1mkdj=D=1min(n,m)d|Ddμ2(d)μ(Dd)(Dd)2i=1nDij=1mDjD=kd=D=1min(n,m)d|Dμ2(d)μ(Dd)DdDi=1nDij=1mDj=D=1min(n,m)Dd|Dμ2(d)μ(Dd)Ddi=1nDij=1mDj=D=1min(n,m)Dd|Dμ2(d)μ(Dd)Ddsum(1,nD)sum(1,mD) ∑ i = 1 n ∑ j = 1 m l c m ( i , j ) ( g c d ( i , j ) 无 平 方 质 因 子 ) = ∑ i = 1 n ∑ j = 1 m l c m ( i , j ) μ 2 ( g c d ( i , j ) ) = ∑ i = 1 n ∑ j = 1 m i j g c d ( i , j ) μ 2 ( g c d ( i , j ) ) = ∑ d = 1 m i n ( n , m ) 1 d μ 2 ( d ) ∑ i = 1 n d ∑ j = 1 m d i j d 2 [ g c d ( i , j ) == 1 ] 枚 举 g c d = ∑ d = 1 m i n ( n , m ) d μ 2 ( d ) ∑ i = 1 n d ∑ j = 1 m d i j [ g c d ( i , j ) == 1 ] = ∑ d = 1 m i n ( n , m ) d μ 2 ( d ) ∑ i = 1 n d ∑ j = 1 m d ∑ k | g c d ( i , j ) i j μ ( k ) = ∑ d = 1 m i n ( n , m ) d μ 2 ( d ) ∑ k = 1 m i n ( n d , m d ) ∑ i = 1 n k d ∑ j = 1 m k d i j k 2 μ ( k ) 先 枚 举 g c d = ∑ d = 1 m i n ( n , m ) d μ 2 ( d ) ∑ k = 1 m i n ( n d , m d ) k 2 μ ( k ) ∑ i = 1 n k d i ∑ j = 1 m k d j = ∑ D = 1 m i n ( n , m ) ∑ d | D d μ 2 ( d ) μ ( D d ) ( D d ) 2 ∑ i = 1 n D i ∑ j = 1 m D j 令 D = k d = ∑ D = 1 m i n ( n , m ) ∑ d | D μ 2 ( d ) μ ( D d ) D d D ∑ i = 1 n D i ∑ j = 1 m D j = ∑ D = 1 m i n ( n , m ) D ∑ d | D μ 2 ( d ) μ ( D d ) D d ∑ i = 1 n D i ∑ j = 1 m D j = ∑ D = 1 m i n ( n , m ) D ∑ d | D μ 2 ( d ) μ ( D d ) D d s u m ( 1 , n D ) s u m ( 1 , m D )

f(x)=d|xμ2(d)μ(xd)xd f ( x ) = ∑ d | x μ 2 ( d ) μ ( x d ) x d

g(x)=μ2(x) g ( x ) = μ 2 ( x )

h(x)=μ(x)x h ( x ) = μ ( x ) ⋅ x

因此 f(x)=g(x)h(x) f ( x ) = g ( x ) ∗ h ( x ) (卷积)

因为 g(x) g ( x ) h(x) h ( x ) 为积性函数,因此 f(x) f ( x ) 也为积性函数,可以线筛(虽然…我是看不出来的 再见)

在写程序的时候呢,其实可以线筛 Df(x) D ⋅ f ( x ) ,会好做一些

代码

#include 
#include 
using namespace std;
#define ll long long
#define N 4000010
#define U unsigned int
int tot=0,T,notprime[N],prime[N];
U f[N];
inline void sieve(){
    f[1]=1;
    for(int i=2;i<=4000000;i++){
        if(!notprime[i]) prime[++tot]=i,f[i]=1-i;
        for(int j=1;j<=tot && prime[j]*i<=4000000;j++){
            notprime[prime[j]*i]=1;
            if(i%prime[j]==0){
                if(i%(prime[j]*prime[j])==0) f[i*prime[j]]=0;
                else f[i*prime[j]]=-f[i/prime[j]]*prime[j];
                break;
            }f[prime[j]*i]=f[i]*(1-prime[j]);
        }
    }
    for(int i=2;i<=4000000;i++) f[i]=f[i]*i+f[i-1];
}
inline U sum(int x){
    return (U)x*(x+1)/2;
}
int main(){
    scanf("%d",&T);sieve();
    while(T--){
        int n,m;scanf("%d%d",&n,&m);
        if(n>m) swap(n,m);
        U ans=0;
        for(int i=1,last;i<=n;i=last+1){
            last=min(n/(n/i),m/(m/i));
            ans+=(f[last]-f[i-1])*sum(n/i)*sum(m/i);
        }
        printf("%u\n",ans&0x3fffffff);
    }
    return 0;
}

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