luogu P1829 [国家集训队]Crash的数字表格 / JZPTAB 题解

∑ i = 1 n ∑ j = 1 m l c m ( i , j ) \sum_{i=1}^n\sum_{j=1}^mlcm(i,j) i=1nj=1mlcm(i,j)
(以下简称为原式)
= ∑ i = 1 n ∑ j = 1 m i j g c d ( i , j ) =\sum_{i=1}^n\sum_{j=1}^m\frac{ij}{gcd(i,j)} =i=1nj=1mgcd(i,j)ij
枚举d=gcd(i,j),令
x = i d x=\frac{i}{d} x=di
y = j d y=\frac{j}{d} y=dj

原式
= ∑ d = 1 n ∑ x = 1 [ n d ] ∑ y = 1 [ m d ] d x y [ g c d ( x , y ) = = 1 ] =\sum_{d=1}^n\sum_{x=1}^{[\frac{n}{d}]}\sum_{y=1}^{[\frac{m}{d}]}dxy[gcd(x,y)==1] =d=1nx=1[dn]y=1[dm]dxy[gcd(x,y)==1]
= ∑ d = 1 n d ∑ x = 1 [ n d ] ∑ y = 1 [ m d ] x y [ g c d ( x , y ) = = 1 ] =\sum_{d=1}^nd\sum_{x=1}^{[\frac{n}{d}]}\sum_{y=1}^{[\frac{m}{d}]}xy[gcd(x,y)==1] =d=1ndx=1[dn]y=1[dm]xy[gcd(x,y)==1]
为了方便起见,我们令
N = [ n d ] N=[\frac{n}{d}] N=[dn]
M = [ m d ] M=[\frac{m}{d}] M=[dm]
f ( p ) = ∑ x = 1 N ∑ y = 1 M x y [ g c d ( x , y ) = = p ] f(p)=\sum_{x=1}^{N}\sum_{y=1}^{M}xy[gcd(x,y)==p] f(p)=x=1Ny=1Mxy[gcd(x,y)==p]
然后套路地莫比乌斯反演:


F ( p ) = ∑ p ∣ d f ( d ) F(p)=\sum_{p|d}f(d) F(p)=pdf(d)
= ∑ p ∣ d ∑ x = 1 N ∑ y = 1 M x y [ g c d ( x , y ) = = d ] =\sum_{p|d}\sum_{x=1}^{N}\sum_{y=1}^{M}xy[gcd(x,y)==d] =pdx=1Ny=1Mxy[gcd(x,y)==d]
= ∑ x = 1 N ∑ y = 1 M x y [ d ∣ x ] [ d ∣ y ] =\sum_{x=1}^{N}\sum_{y=1}^{M}xy[d|x][d|y] =x=1Ny=1Mxy[dx][dy]
= ∑ x = 1 N x [ d ∣ x ] ∑ y = 1 M y [ d ∣ y ] =\sum_{x=1}^{N}x[d|x]\sum_{y=1}^{M}y[d|y] =x=1Nx[dx]y=1My[dy]
= d 2 ∑ i = 1 [ N d ] i ∑ j = 1 [ M d ] j =d^2\sum_{i=1}^{[\frac{N}{d}]}i\sum_{j=1}^{[\frac{M}{d}]}j =d2i=1[dN]ij=1[dM]j
= d 2 [ N d ] ( [ N d ] + 1 ) [ M d ] ( [ M d ] + 1 ) 4 =\frac{d^2[\frac{N}{d}]([\frac{N}{d}]+1)[\frac{M}{d}]([\frac{M}{d}]+1)}{4} =4d2[dN]([dN]+1)[dM]([dM]+1)

f ( 1 ) = ∑ 1 ∣ d μ ( d 1 ) F ( d ) f(1)=\sum_{1|d}\mu(\frac{d}{1})F(d) f(1)=1dμ(1d)F(d)
= ∑ d = 1 N μ ( d ) F ( d ) =\sum_{d=1}^{N}\mu(d)F(d) =d=1Nμ(d)F(d)
= ∑ d = 1 N d 2 μ ( d ) [ N d ] ( [ N d ] + 1 ) [ M d ] ( [ M d ] + 1 ) 4 =\sum_{d=1}^N\frac{d^2\mu(d)[\frac{N}{d}]([\frac{N}{d}]+1)[\frac{M}{d}]([\frac{M}{d}]+1)}{4} =d=1N4d2μ(d)[dN]([dN]+1)[dM]([dM]+1)
其中知道了
[ N d ] [\frac{N}{d}] [dN]

[ M d ] [\frac{M}{d}] [dM]
就可以O(1)算出
[ N d ] ( [ N d ] + 1 ) [ M d ] ( [ M d ] + 1 ) [\frac{N}{d}]([\frac{N}{d}]+1)[\frac{M}{d}]([\frac{M}{d}]+1) [dN]([dN]+1)[dM]([dM]+1)

d 2 μ ( d ) d^2\mu(d) d2μ(d)
的前缀和在线性筛时可以预处理出来
故整除分块可以O(√n)求f(1)
用整除分块枚举本文开头的d也是O(√n)的
综上,时间复杂度O(n),可过1e7

你可能感兴趣的:(luogu P1829 [国家集训队]Crash的数字表格 / JZPTAB 题解)