【51nod1584】加权约数和(莫比乌斯反演)(线性筛)

传送门


题解:

一看这种题就是大力上莫比乌斯反演。

首先 m a x max max并不是能够处理的东西。

设:
A ( n ) = ∑ i = 1 n ∑ j = 1 i i σ ( i j ) B ( n ) = ∑ i = 1 n i σ ( i 2 ) A(n)=\sum_{i=1}^n\sum_{j=1}^ii\sigma(ij)\\B(n)=\sum_{i=1}^ni\sigma(i^2) A(n)=i=1nj=1iiσ(ij)B(n)=i=1niσ(i2)

则答案为 A n s = 2 ⋅ A ( n ) − B ( n ) Ans=2\cdot A(n)-B(n) Ans=2A(n)B(n)

化简:
A ( n ) = ∑ i = 1 n i ∑ j = 1 i ∑ p ∣ i ∑ q ∣ j [ g c d ( p , q ) = 1 ] p j q = ∑ d = 1 n μ ( d ) ∑ d ∣ p p ∑ p ∣ i i ∑ d ∣ q , q ≤ i ∑ q ∣ j , j ≤ i j q = ∑ d = 1 n μ ( d ) ∑ p = 1 ⌊ n d ⌋ d p ∑ p ∣ i , i ≤ ⌊ n d ⌋ i d ∑ q = 1 i ∑ q ∣ j , j ≤ i j q = ∑ d = 1 n μ ( d ) d 2 ∑ i = 1 ⌊ n d ⌋ i σ ( i ) S σ ( i ) B ( n ) = ∑ i = 1 n i σ ( i 2 ) = ∑ i = 1 n i ∑ p ∣ i ∑ q ∣ i [ g c d ( p , q ) = 1 ] i p q = ∑ d = 1 n μ ( d ) ∑ i = 1 n ∑ d ∣ p ∣ i ∑ d ∣ q ∣ i i p q = ∑ d = 1 n μ ( d ) ∑ i = 1 ⌊ n d ⌋ i d ∑ p ∣ i ∑ q ∣ i i d p q = ∑ d = 1 n μ ( d ) d 2 ∑ i = 1 ⌊ n d ⌋ i σ 2 ( i ) \begin{aligned} A(n)&=&&\sum_{i=1}^ni\sum_{j=1}^i\sum_{p\mid i}\sum_{q\mid j}[gcd(p,q)=1]\frac{pj}{q}\\ &=&&\sum_{d=1}^n\mu(d)\sum_{d\mid p}p\sum_{p\mid i}i\sum_{d\mid q,q\leq i}\sum_{q\mid j,j\leq i}\frac{j}{q}\\ &=&&\sum_{d=1}^n\mu(d)\sum_{p=1}^{\lfloor\frac{n}{d}\rfloor}dp\sum_{p\mid i,i\leq \lfloor\frac{n}{d}\rfloor}id\sum_{q=1}^i\sum_{q\mid j,j\leq i}\frac{j}{q}\\ &=&&\sum_{d=1}^n\mu(d)d^2\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}i\sigma(i)S_\sigma(i)\\\\\\ B(n)&=&&\sum_{i=1}^ni\sigma(i^2)\\ &=&&\sum_{i=1}^ni\sum_{p\mid i}\sum_{q\mid i}[gcd(p,q)=1]\frac{ip}{q}\\ &=&&\sum_{d=1}^n\mu(d)\sum_{i=1}^n\sum_{d\mid p\mid i}\sum_{d\mid q\mid i}\frac{ip}{q}\\ &=&&\sum_{d=1}^n\mu(d)\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}id\sum_{p\mid i}\sum_{q\mid i}\frac{idp}{q}\\ &=&&\sum_{d=1}^n\mu(d)d^2\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}i\sigma^2(i) \end{aligned} A(n)B(n)=========i=1nij=1ipiqj[gcd(p,q)=1]qpjd=1nμ(d)dpppiidq,qiqj,jiqjd=1nμ(d)p=1dndppi,idnidq=1iqj,jiqjd=1nμ(d)d2i=1dniσ(i)Sσ(i)i=1niσ(i2)i=1nipiqi[gcd(p,q)=1]qipd=1nμ(d)i=1ndpidqiqipd=1nμ(d)i=1dnidpiqiqidpd=1nμ(d)d2i=1dniσ2(i)

其中 S σ S_\sigma Sσ表示 σ \sigma σ的前缀和。

则我们有 A n s = 2 ⋅ A ( n ) − B ( n ) = ∑ d = 1 n μ ( d ) d 2 ∑ i = 1 ⌊ n d ⌋ i σ ( i ) ( 2 ⋅ S σ ( i ) − σ ( i ) ) Ans=2\cdot A(n)-B(n)\\=\sum_{d=1}^n\mu(d)d^2\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}i\sigma(i)(2\cdot S_\sigma(i)-\sigma(i)) Ans=2A(n)B(n)=d=1nμ(d)d2i=1dniσ(i)(2Sσ(i)σ(i))

整除分块并不像是可以过的样子。

但是我们可以考虑调和级数预处理。

枚举每一个 i i i d d d,加到 a n s [ i ∗ d ] ans[i*d] ans[id],我们发现,它对所有 n ≥ d ∗ i n\geq d*i ndi的询问都产生影响,求一遍前缀和即可。


代码:

#include<bits/stdc++.h>
#define ll long long
#define re register
#define gc get_char
#define cs const

namespace IO{
	inline char get_char(){
		static cs int Rlen=1<<18|1;
		static char buf[Rlen],*p1,*p2;
		return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,Rlen,stdin),p1==p2)?EOF:*p1++;
	}
	
	template<typename T>
	inline T get(){
		char c;
		while(!isdigit(c=gc()));T num=c^48;
		while(isdigit(c=gc()))num=(num+(num<<2)<<1)+(c^48);
		return num;
	}
	inline int getint(){return get<int>();}
}
using namespace IO;

using std::cerr;
using std::cout;

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){return (a-=b)<0?a+mod:a;}
inline int mul(int a,int b){ll r=(ll)a*b;return r>=mod?r%mod:r;}
inline void Inc(int &a,int b){(a+=b)>=mod&&(a-=mod);}
inline void Dec(int &a,int b){(a-=b)<0&&(a+=mod);}

cs int P=1e6+7;

bool mark[P];
int pr[P],pcnt;
int sigma[P],sum_sigma[P],mu[P];
int dm[P],dmt[P];

int f[P],g[P],ans[P];

inline void linear_sieves(int n=1e6){
	sigma[1]=mu[1]=1;
	for(int re i=2;i<=n;++i){
		if(!mark[i]){
			pr[++pcnt]=i;
			mu[i]=mod-1;sigma[i]=i+1;
			dm[i]=1;dmt[i]=i+1;
		}
		for(int re j=1;i*pr[j]<=n;++j){
			int k=i*pr[j];
			mark[k]=true;
			if(i%pr[j]){
				dm[k]=i;dmt[k]=pr[j]+1;
				mu[k]=dec(0,mu[i]);
				sigma[k]=mul(sigma[i],pr[j]+1);
			}
			else {
				dm[k]=dm[i];mu[k]=0;
				dmt[k]=add(mul(dmt[i],pr[j]),1);
				sigma[k]=mul(dmt[k],sigma[dm[i]]);
				break;
			}
		}
	}
	for(int re i=1;i<=n;++i){
		f[i]=mul(mu[i],mul(i,i));
		sum_sigma[i]=add(sum_sigma[i-1],sigma[i]);
		g[i]=mul(mul(i,sigma[i]),dec(add(sum_sigma[i],sum_sigma[i]),sigma[i]));
	}
	for(int re i=1;i<=n;++i){
		for(int re j=1,lj=n/i;j<=lj;++j)
		Inc(ans[i*j],mul(f[i],g[j]));
	}
	for(int re i=1;i<=n;++i)Inc(ans[i],ans[i-1]);
}

signed main(){
#ifdef zxyoi
	freopen("sigma.in","r",stdin);
#endif
	linear_sieves();int T=getint();
	for(int re i=1;i<=T;++i)cout<<"Case #"<<i<<": "<<ans[getint()]<<"\n";
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值