• 【BZOJ2154】—Crash的数字表格(莫比乌斯反演+整除分块)


    传送门

    题意:求i=1nj=1mlcm(i,j),n,m1e7sum_{i=1}^{n}sum_{j=1}^{m}lcm(i,j),n,mle1e7


    SolutionSolution

    考虑到lcmlcm无法处理,我们先变成gcdgcd的形式

    ans=i=1nj=1mijgcd(i,j)ans=sum_{i=1}^{n}sum_{j=1}^{m}frac {i*j}{gcd(i,j)}

    考虑枚举gcdgcd

    ans=d=1min(n,m)i=1nj=1m[gcd(i,j)=d]ijdans=sum_{d=1}^{min(n,m)}sum_{i=1}^{n}sum_{j=1}^{m}[gcd(i,j)=d]frac {ij}d=d=1min(n,m)dindjm[gcd(i,j)=d]ijd=sum_{d=1}^{min(n,m)}sum_{d|i}^{n}sum_{d|j}^{m}[gcd(i,j)=d]frac {ij}d

    i=di,j=dji'=frac d i,j'=frac d j(即从枚举dd的倍数变成枚举是dd的几倍)
    则(为了方便仍用i,ji,j表示i,ji',j')

    ans=d=1min(n,m)i=1ndj=1md[gcd(di,dj)=d]idjddans=sum_{d=1}^{min(n,m)}sum_{i=1}^{frac n d}sum_{j=1}^{frac m d}[gcd(di,dj)=d] frac{id*jd}{d}=d=1min(n,m)di=1ndj=1md[gcd(i,j)=1]ij=sum_{d=1}^{min(n,m)}dsum_{i=1}^{frac n d}sum_{j=1}^{frac m d}[gcd(i,j)=1]ij

    考虑有个莫比乌斯函数的简单结论dnμ(d)=[n==1]sum_{d|n}mu(d)=[n==1]

    考虑的式子中有个[gcd(i,j)=1][gcd(i,j)=1]
    则:

    ans=d=1min(n,m)di=1ndj=1mdTgcd(i,j)μ(T)ijans=sum_{d=1}^{min(n,m)}dsum_{i=1}^{frac n d}sum_{j=1}^{frac m d}sum_{T|gcd(i,j)}mu(T)ij

    考虑把TT提到前面来:

    ans=d=1min(n,m)dT=1min(nd,md)μ(T)i=1ndj=1mdij[Tgcd(i,j)]ans=sum_{d=1}^{min(n,m)}dsum_{T=1}^{min(frac n d,frac m d)}mu(T)sum_{i=1}^{frac n d}sum_{j=1}^{frac m d}ij[T|gcd(i,j)]

    考虑令i=iT,j=jTi'=i*T,j'=j*T
    则(同样为了方便直接用i,ji,j表示了)

    ans=d=1min(n,m)dT=1min(nd,md)μ(T)i=1ndTj=1mdTijT2ans=sum_{d=1}^{min(n,m)}dsum_{T=1}^{min(frac n d,frac m d)}mu(T)sum_{i=1}^{frac {n}{dT}}sum_{j=1}^{frac{m}{dT}}ijT^2
    =d=1min(n,m)dT=1min(nd,md)μ(T)T2i=1ndTj=1mdTij=sum_{d=1}^{min(n,m)}dsum_{T=1}^{min(frac n d,frac m d)}mu(T)T^2sum_{i=1}^{frac {n}{dT}}sum_{j=1}^{frac{m}{dT}}ij

    我们发现i=1ndTj=1mdTijsum_{i=1}^{frac {n}{dT}}sum_{j=1}^{frac{m}{dT}}ij这一团是可以O(1)O(1)求的
    i=1nj=1mij=n(n+1)/2m(m+1)/2sum_{i=1}^{n}sum_{j=1}^{m}ij=n*(n+1)/2*m*(m+1)/2

    μ(T)T2mu(T)T^2可以线性筛μmu时求出

    就可以先整除分块一下dd,得到nd,mdfrac n d,frac m d的值后在里面套一个整除分块求

    T=1min(nd,md)μ(T)T2i=1ndTj=1mdTijsum_{T=1}^{min(frac n d,frac m d)}mu(T)T^2sum_{i=1}^{frac {n}{dT}}sum_{j=1}^{frac{m}{dT}}ij这一团

    总复杂度O(nn)=O(n)O(sqrt n *sqrt n)=O(n)

    #include<bits/stdc++.h>
    using namespace std;
    #define int long long
    inline int read(){
    	char ch=getchar();
    	int res=0,f=1;
    	while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
    	while(isdigit(ch))res=(res<<3)+(res<<1)+(ch^48),ch=getchar();
    	return res*f;
    }
    const int N=10000005;
    const int mod=20101009;
    int mu[N],pr[N],vis[N],sum[N],tot,ans;
    inline void init(){
    	mu[1]=1;
    	for(int i=2;i<N;i++){
    		if(!vis[i])pr[++tot]=i,mu[i]=-1;
    		for(int j=1;j<=tot&&i*pr[j]<N;j++){
    			vis[pr[j]*i]=1;
    			if(i%pr[j]==0)break;
    			mu[i*pr[j]]=-mu[i];
    		}
    	}
    	for(int i=1;i<N;i++){
    		sum[i]=(sum[i-1]+(i*i)*mu[i])%mod;
    	}
    	//for(int i=20;i<=30;i++)cout<<sum[i]<<'
    ';
    }
    inline int t(int n,int m){
    	return (((n*(n+1)/2)%mod)*((m*(m+1)/2)%mod)%mod);
    }
    inline int calc(int n,int m){
    	int p=min(n,m),res=0;
    	for(int i=1,nxt;i<=p;i=nxt+1){
    		nxt=min(n/(n/i),m/(m/i));
    		res=(res+((sum[nxt]-sum[i-1]+mod)%mod)*t(n/i,m/i)%mod)%mod;
    	}
    	return res;
    }
    signed main(){
    	init();
    	int n=read(),m=read();
    	int p=min(n,m);
    	for(int i=1,nxt;i<=p;i=nxt+1){
    		nxt=min(n/(n/i),m/(m/i));
    		ans=(ans+(((nxt-i+1)*(nxt+i)/2)%mod)*calc(n/i,m/i)%mod)%mod;
    	}
    	cout<<ans<<'
    ';
    }
    
  • 相关阅读:
    常春藤之路,从幼儿园开始走起
    常青藤零距离
    web-service
    WebService到底是什么?
    How to add libraries to “External Libraries” in WebStorm/PhpStorm/Intellij
    浏览器核心说明
    万维网
    js和jquery获取span里面的值
    TPshop学习
    sphinx文档
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/11145662.html
Copyright © 2020-2023  润新知