P3327 [SDOI2015]约数个数和

Label

莫比乌斯反演

Description

设 d ( x ) d(x) d(x)为 x x x的约数个数,给定 T T T组 n , m ( 1 ≤ T , n , m ≤ 5 × 1 0 4 ) n,m(1\le T,n,m\le 5\times 10^4) n,m(1≤T,n,m≤5×104),对于每组 n , m n,m n,m,求

​ ∑ i = 1 n ∑ j = 1 m d ( i j ) \sum_{i=1}^{n}\sum_{j=1}^{m}d(ij) i=1∑n​j=1∑m​d(ij)

Solution

为了将 d d d变化为可利用反演结论化简的式子,我们首先引入一个结论:

​ d ( i j ) = ∑ x ∣ i ∑ y ∣ j [ g c d ( i , j ) = 1 ] d(ij)=\sum_{x|i}\sum_{y|j}[gcd(i,j)=1] d(ij)=x∣i∑​y∣j∑​[gcd(i,j)=1] ( 1 ) (1) (1)

该结论中出现了 [ g c d ( i , j ) = 1 ] [gcd(i,j)=1] [gcd(i,j)=1]的形式,我们考虑进一步变换:

d ( i j ) = ∑ x ∣ i ∑ y ∣ j [ g c d ( i , j ) = 1 ] d(ij)=\sum_{x|i}\sum_{y|j}[gcd(i,j)=1] d(ij)=x∣i∑​y∣j∑​[gcd(i,j)=1]

= ∑ x ∣ i ∑ y ∣ j ∑ p ∣ g c d ( x , y ) μ ( p ) =\sum_{x|i}\sum_{y|j}\sum_{p|gcd(x,y)}\mu(p) =x∣i∑​y∣j∑​p∣gcd(x,y)∑​μ(p)

= ∑ p ∣ i ∧ p ∣ j μ ( p ) ∑ x ∣ i ∑ y ∣ j [ p ∣ g c d ( x , y ) ] =\sum_{p|i\wedge p|j}\mu(p)\sum_{x|i}\sum_{y|j}[p|gcd(x,y)] =p∣i∧p∣j∑​μ(p)x∣i∑​y∣j∑​[p∣gcd(x,y)]

= ∑ p ∣ i ∧ p ∣ j μ ( p ) ∑ x p ∣ i p ∑ y p ∣ j p [ 1 ∣ g c d ( x p , y p ) ] =\sum_{p|i\wedge p|j}\mu(p)\sum_{\frac{x}{p}|\frac{i}{p}}\sum_{\frac{y}{p}|\frac{j}{p}}[1|gcd(\frac{x}{p},\frac{y}{p})] =p∣i∧p∣j∑​μ(p)px​∣pi​∑​py​∣pj​∑​[1∣gcd(px​,py​)] ( 2 ) (2) (2)

= ∑ p ∣ i ∧ p ∣ j μ ( p ) ∑ x p ∣ i p ∑ y p ∣ j p 1 =\sum_{p|i\wedge p|j}\mu(p)\sum_{\frac{x}{p}|\frac{i}{p}}\sum_{\frac{y}{p}|\frac{j}{p}}1 =p∣i∧p∣j∑​μ(p)px​∣pi​∑​py​∣pj​∑​1

= ∑ p ∣ i ∧ p ∣ j μ ( p ) ∑ x ∣ i p 1 ∑ y ∣ j p 1 =\sum_{p|i\wedge p|j}\mu(p)\sum_{x|\frac{i}{p}}1\sum_{y|\frac{j}{p}}1 =p∣i∧p∣j∑​μ(p)x∣pi​∑​1y∣pj​∑​1

= ∑ p ∣ i ∧ p ∣ j μ ( p ) d ( i p ) d ( j p ) =\sum_{p|i\wedge p|j}\mu(p)d(\frac{i}{p})d(\frac{j}{p}) =p∣i∧p∣j∑​μ(p)d(pi​)d(pj​)

∴ ∑ i = 1 n ∑ j = 1 m d ( i j ) \therefore \sum_{i=1}^{n}\sum_{j=1}^{m}d(ij) ∴i=1∑n​j=1∑m​d(ij)

= ∑ i = 1 n ∑ j = 1 m ∑ x ∣ i ∑ y ∣ j [ g c d ( i , j ) = 1 ] =\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{x|i}\sum_{y|j}[gcd(i,j)=1] =i=1∑n​j=1∑m​x∣i∑​y∣j∑​[gcd(i,j)=1]

= ∑ i = 1 n ∑ j = 1 m = ∑ p ∣ i ∧ p ∣ j μ ( p ) d ( i p ) d ( j p ) =\sum_{i=1}^{n}\sum_{j=1}^{m}=\sum_{p|i\wedge p|j}\mu(p)d(\frac{i}{p})d(\frac{j}{p}) =i=1∑n​j=1∑m​=p∣i∧p∣j∑​μ(p)d(pi​)d(pj​)

= ∑ p = 1 m i n ( n , m ) μ ( p ) ∑ i = 1 n ∑ j = 1 m [ p ∣ g c d ( i , j ) ] d ( i p ) d ( j p ) =\sum_{p=1}^{min(n,m)}\mu(p)\sum_{i=1}^{n}\sum_{j=1}^{m}[p|gcd(i,j)]d(\frac{i}{p})d(\frac{j}{p}) =p=1∑min(n,m)​μ(p)i=1∑n​j=1∑m​[p∣gcd(i,j)]d(pi​)d(pj​) ( 3 ) (3) (3)

= ∑ p = 1 m i n ( n , m ) μ ( p ) ∑ i = 1 n ∑ j = 1 m [ 1 ∣ g c d ( i p , j p ) ] d ( i p ) d ( j p ) =\sum_{p=1}^{min(n,m)}\mu(p)\sum_{i=1}^{n}\sum_{j=1}^{m}[1|gcd(\frac{i}{p},\frac{j}{p})]d(\frac{i}{p})d(\frac{j}{p}) =p=1∑min(n,m)​μ(p)i=1∑n​j=1∑m​[1∣gcd(pi​,pj​)]d(pi​)d(pj​)

= ∑ p = 1 m i n ( n , m ) μ ( p ) ∑ i = 1 ⌊ n p ⌋ ∑ j = 1 ⌊ m p ⌋ d ( i ) d ( j ) =\sum_{p=1}^{min(n,m)}\mu(p)\sum_{i=1}^{\lfloor\frac{n}{p}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{p}\rfloor}d(i)d(j) =p=1∑min(n,m)​μ(p)i=1∑⌊pn​⌋​j=1∑⌊pm​⌋​d(i)d(j)

= ∑ p = 1 m i n ( n , m ) μ ( p ) S ( ⌊ n p ⌋ ) S ( ⌊ m p ⌋ ) =\sum_{p=1}^{min(n,m)}\mu(p)S(\lfloor\frac{n}{p}\rfloor)S(\lfloor\frac{m}{p}\rfloor) =p=1∑min(n,m)​μ(p)S(⌊pn​⌋)S(⌊pm​⌋)

(其中 S ( x ) = ∑ k = 1 x ( d ( k ) ) S(x)=\sum_{k=1}^{x}(d(k)) S(x)=∑k=1x​(d(k)))

算法时间复杂度 O ( n + T n ) O(n+T\sqrt n) O(n+Tn ​)

总结

注释:

(1)积累结论: d ( i j ) = ∑ x ∣ i ∑ y ∣ j [ g c d ( i , j ) = 1 ] d(ij)=\sum_{x|i}\sum_{y|j}[gcd(i,j)=1] d(ij)=∑x∣i​∑y∣j​[gcd(i,j)=1]。

(2)与将 g c d ( x , y ) = d gcd(x,y)=d gcd(x,y)=d转化为 g c d ( x d , y d ) gcd(\frac{x}{d},\frac{y}{d}) gcd(dx​,dy​)的套路类似,遇到形似 [ x ∣ y ] [x|y] [x∣y]时,可考虑将其转化为 [ 1 ∣ y x ] [1|\frac{y}{x}] [1∣xy​],当 x x x整除 y y y时此真值式恒为1。

(3)求和符号交换时,需考虑合适的将被交换的求和符号对应的范围转化的方式:有时考虑所有合法值的取值范围,有时转化为真值式 [ . . . ] [...] [...]表示更加简洁。

Code

#include<cstdio>
#include<iostream>
#define ri register int
#define ll long long
using namespace std;

const int MAXN=5e4;
int T,N,M,n,cnt;
ll ans,mu[MAXN+20],prime[MAXN+20],sum[MAXN+20],d[MAXN+20];
bool notprime[MAXN+20];

void Mobius() 
{
	mu[1]=1,notprime[1]=true;
	for(ri i=2;i<=MAXN;++i)
	{
		if(!notprime[i]) prime[++cnt]=i,mu[i]=-1;
		for(ri j=1;j<=cnt&&i*prime[j]<=MAXN;++j)
		{
			notprime[i*prime[j]]=true;
			if(i%prime[j]==0) break;
			else mu[i*prime[j]]=-mu[i];
		}
	}
	for(ri i=1;i<=MAXN;++i) sum[i]=sum[i-1]+mu[i];
}

void GetD()
{
	for(ri i=1;i<=MAXN;++i)
		for(ri j=i;j<=MAXN;j+=i) ++d[j];
	for(ri i=1;i<=MAXN;++i) d[i]=d[i-1]+d[i];
}

int main()
{
	std::ios::sync_with_stdio(false);
	cin>>T;
	Mobius(); GetD();
	for(ri op=1;op<=T;++op)
	{
		cin>>N>>M;
		n=min(N,M),ans=0LL;
		for(ri l=1,r;l<=n;l=r+1)
		{
			r=min(N/(N/l),M/(M/l));
			ans+=(sum[r]-sum[l-1])*d[N/l]*d[M/l];
		}
		cout<<ans<<'\n';
	}
	return 0;
}
上一篇:正态分布


下一篇:随机图论的概率基础