P1829 [国家集训队]Crash的数字表格

Label

经典莫比乌斯反演转化 g c d ( i , j ) gcd(i,j) gcd(i,j)(P3768弱化版)

Description

给定两个正整数 n , m ( n , m ≤ 1 0 7 ) n,m(n,m\leq 10^7) n,m(n,m≤107),求

​ ∑ i = 1 n ∑ j = 1 m l c m ( i , j ) \sum_{i=1}^{n}\sum_{j=1}^{m}lcm(i,j) i=1∑n​j=1∑m​lcm(i,j)

Solution

∵ \because ∵ ∑ i = 1 n ∑ j = 1 m l c m ( i , j ) = ∑ i = 1 n ∑ j = 1 m i j ( i , j ) \sum_{i=1}^{n}\sum_{j=1}^{m}lcm(i,j)=\sum_{i=1}^{n}\sum_{j=1}^{m}\frac{ij}{(i,j)} i=1∑n​j=1∑m​lcm(i,j)=i=1∑n​j=1∑m​(i,j)ij​

若此处再利用 φ ∗ 1 = i d \varphi*1=id φ∗1=id反演, φ \varphi φ的求和式在分母上,不好提取。故考虑传统方法,即外层枚举 g c d ( i , j ) gcd(i,j) gcd(i,j):

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

= ∑ d = 1 m i n ( n , m ) 1 d ∑ i = 1 n ∑ j = 1 m i j [ ( i , j ) = d ] =\sum_{d=1}^{min(n,m)}\frac{1}{d}\sum_{i=1}^{n}\sum_{j=1}^{m}ij[(i,j)=d] =d=1∑min(n,m)​d1​i=1∑n​j=1∑m​ij[(i,j)=d]

= ∑ d = 1 m i n ( n , m ) 1 d ∑ i = 1 n ∑ j = 1 m i j [ g c d ( i d , j d ) = 1 ] =\sum_{d=1}^{min(n,m)}\frac{1}{d}\sum_{i=1}^{n}\sum_{j=1}^{m}ij[gcd(\frac{i}{d},\frac{j}{d})=1] =d=1∑min(n,m)​d1​i=1∑n​j=1∑m​ij[gcd(di​,dj​)=1]

= ∑ d = 1 m i n ( n , m ) 1 d ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ d i d j [ g c d ( i , j ) = 1 ] =\sum_{d=1}^{min(n,m)}\frac{1}{d}\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}didj[gcd(i,j)=1] =d=1∑min(n,m)​d1​i=1∑⌊dn​⌋​j=1∑⌊dm​⌋​didj[gcd(i,j)=1]

= ∑ d = 1 m i n ( n , m ) d ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ i j ∑ p ∣ ( i , j ) μ ( p ) =\sum_{d=1}^{min(n,m)}d\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}ij\sum_{p|(i,j)}\mu(p) =d=1∑min(n,m)​di=1∑⌊dn​⌋​j=1∑⌊dm​⌋​ijp∣(i,j)∑​μ(p)

= ∑ d = 1 m i n ( n , m ) d ∑ p = 1 m i n ( ⌊ n d ⌋ , ⌊ m d ⌋ ) μ ( p ) ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ [ p ∣ i ∧ p ∣ j ] i j =\sum_{d=1}^{min(n,m)}d\sum_{p=1}^{min(\lfloor\frac{n}{d}\rfloor,\lfloor\frac{m}{d}\rfloor)}\mu(p)\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}[p|i\wedge p|j]ij =d=1∑min(n,m)​dp=1∑min(⌊dn​⌋,⌊dm​⌋)​μ(p)i=1∑⌊dn​⌋​j=1∑⌊dm​⌋​[p∣i∧p∣j]ij

= ∑ d = 1 m i n ( n , m ) d ∑ p = 1 m i n ( ⌊ n d ⌋ , ⌊ m d ⌋ ) μ ( p ) ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ [ p ∣ i ∧ p ∣ j ] i j =\sum_{d=1}^{min(n,m)}d\sum_{p=1}^{min(\lfloor\frac{n}{d}\rfloor,\lfloor\frac{m}{d}\rfloor)}\mu(p)\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}[p|i\wedge p|j]ij =d=1∑min(n,m)​dp=1∑min(⌊dn​⌋,⌊dm​⌋)​μ(p)i=1∑⌊dn​⌋​j=1∑⌊dm​⌋​[p∣i∧p∣j]ij

= ∑ d = 1 m i n ( n , m ) d ∑ p = 1 m i n ( ⌊ n d ⌋ , ⌊ m d ⌋ ) μ ( p ) ∑ i = 1 ⌊ n d ⌋ [ p ∣ i ] i ∑ j = 1 ⌊ m d ⌋ [ p ∣ j ] j =\sum_{d=1}^{min(n,m)}d\sum_{p=1}^{min(\lfloor\frac{n}{d}\rfloor,\lfloor\frac{m}{d}\rfloor)}\mu(p)\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}[p|i]i\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}[p|j]j =d=1∑min(n,m)​dp=1∑min(⌊dn​⌋,⌊dm​⌋)​μ(p)i=1∑⌊dn​⌋​[p∣i]ij=1∑⌊dm​⌋​[p∣j]j

= ∑ d = 1 m i n ( n , m ) d ∑ p = 1 m i n ( ⌊ n d ⌋ , ⌊ m d ⌋ ) p 2 μ ( p ) ∑ i = 1 ⌊ n p d ⌋ i ∑ j = 1 ⌊ m p d ⌋ j =\sum_{d=1}^{min(n,m)}d\sum_{p=1}^{min(\lfloor\frac{n}{d}\rfloor,\lfloor\frac{m}{d}\rfloor)}p^2\mu(p)\sum_{i=1}^{\lfloor\frac{n}{pd}\rfloor}i\sum_{j=1}^{\lfloor\frac{m}{pd}\rfloor}j =d=1∑min(n,m)​dp=1∑min(⌊dn​⌋,⌊dm​⌋)​p2μ(p)i=1∑⌊pdn​⌋​ij=1∑⌊pdm​⌋​j

式子化简到这里为止,考虑如何对上式进行分块处理。

这个式子看起来很麻烦,但并不代表不能处理(事实上,如果化简出了正确且可处理的式子而看不出来此式可处理,这才是最GG的)。

我们可以分块改写:

设 S ( ⌊ n d ⌋ , ⌊ m d ⌋ ) = S ( N , M ) = ∑ p = 1 m i n ( N , M ) p 2 μ ( p ) ∑ i = 1 ⌊ N p ⌋ i ∑ j = 1 ⌊ M p ⌋ j S(\lfloor\frac{n}{d}\rfloor,\lfloor\frac{m}{d}\rfloor)=S(N,M)=\sum_{p=1}^{min(N,M)}p^2\mu(p)\sum_{i=1}^{\lfloor\frac{N}{p}\rfloor}i\sum_{j=1}^{\lfloor\frac{M}{p}\rfloor}j S(⌊dn​⌋,⌊dm​⌋)=S(N,M)=∑p=1min(N,M)​p2μ(p)∑i=1⌊pN​⌋​i∑j=1⌊pM​⌋​j,则原式为

​ ∑ i = 1 m i n ( n , m ) d S ( ⌊ n d ⌋ , ⌊ m d ⌋ ) \sum_{i=1}^{min(n,m)}dS(\lfloor\frac{n}{d}\rfloor,\lfloor\frac{m}{d}\rfloor) i=1∑min(n,m)​dS(⌊dn​⌋,⌊dm​⌋)

此式显然可以数论分块求,而每求一次 S S S函数的值,在 O ( n ) O(n) O(n)线性筛(注意此题数据范围,此处不必用杜教筛)预处理出 p 2 μ ( p ) p^2\mu(p) p2μ(p)的前缀和的前提下,又可以数论分块求解。

算法时间复杂度 Θ ( n + m ) \Theta(n+m) Θ(n+m)。

Code

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

const int MAXN=1e7;
const ll MOD=20101009;
int cnt,prime[MAXN+20];
ll N,M,mu[MAXN+20],sum[MAXN+20],Ans;
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]+(((ll)i*(ll)i)%MOD)*mu[i]%MOD+MOD)%MOD;
}

inline ll sum1(ll n)
{
	return (n*(n+1)/2)%MOD;
}

ll S_mu(ll n,ll m)
{
	ll ans=0LL;
	for(ll l=1,r;l<=n;l=r+1)
	{
		r=min(n/(n/l),m/(m/l));
		ans=(ans+(((sum[r]-sum[l-1])%MOD+MOD)%MOD)*(sum1(n/l)*sum1(m/l)%MOD)%MOD)%MOD;
	}
	return ans;
}

int main()
{
	std::ios::sync_with_stdio(false);
	Mobius();
	cin>>N>>M;
	if(N>M) swap(N,M);
	for(ri l=1,r;l<=N;l=r+1)
	{
		r=min(N/(N/l),M/(M/l));
		Ans=(Ans+(S_mu(N/l,M/l)*((sum1(r)-sum1(l-1)+MOD)%MOD))%MOD)%MOD;
	}
	cout<<Ans;
	return 0;
}
上一篇:MySQL的substring_index函数到HGDB的迁移


下一篇:【前端框架学习】第一次实验 跑马灯效果的制作