51Nod 1238 - 最小公倍数之和 V3
求 ∑ i = 1 n ∑ j = 1 n l c m ( i , j ) \displaystyle\sum_{i=1}^{n}\displaystyle\sum_{j=1}^{n}lcm(i, j) i=1∑nj=1∑nlcm(i,j)
原式 = ∑ i = 1 n ∑ j = 1 n i × j ( i , j ) = ∑ d = 1 n d × ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ n d ⌋ i × j × [ ( i , j ) = 1 ] =\displaystyle\sum_{i=1}^{n}\displaystyle\sum_{j=1}^{n}\frac{i\times j}{(i, j)}=\displaystyle\sum_{d=1}^{n}d\times \displaystyle\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor} \displaystyle\sum_{j=1}^{\left \lfloor \frac{n}{d} \right \rfloor} i\times j \times [(i, j)=1] =i=1∑nj=1∑n(i,j)i×j=d=1∑nd×i=1∑⌊dn⌋j=1∑⌊dn⌋i×j×[(i,j)=1]
由 ∑ i = 1 n i × [ ( i , n ) = 1 ] = n × ϕ ( n ) 2 \displaystyle\sum_{i=1}^{n}i\times [(i, n)=1]=\frac{n\times\phi(n)}{2} i=1∑ni×[(i,n)=1]=2n×ϕ(n),原式 = ∑ d = 1 n d × ∑ i = 1 ⌊ n d ⌋ i 2 × ϕ ( i ) =\displaystyle\sum_{d=1}^{n}d\times \displaystyle\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor}i^2\times \phi(i) =d=1∑nd×i=1∑⌊dn⌋i2×ϕ(i)
令 f ( n ) = n 2 × ϕ ( n ) f(n)=n^2\times \phi(n) f(n)=n2×ϕ(n),我们要求 F ( x ) = ∑ i = 1 n f ( i ) F(x)=\displaystyle\sum_{i=1}^{n}f(i) F(x)=i=1∑nf(i)
由 ∑ d ∣ n ϕ ( d ) = n \displaystyle\sum_{d|n}\phi(d)=n d∣n∑ϕ(d)=n,可得 ∑ i = 1 n i 2 × ∑ d ∣ i ϕ ( d ) = ∑ i = 1 n i 3 = n 2 × ( n + 1 ) 2 4 \displaystyle\sum_{i=1}^{n}i^2 \times \displaystyle\sum_{d|i}\phi(d)=\displaystyle\sum_{i=1}^{n}i^3=\frac{n^2\times (n+1)^2}{4} i=1∑ni2×d∣i∑ϕ(d)=i=1∑ni3=4n2×(n+1)2
上式左边可变形为 ∑ i = 1 n ∑ d ∣ i i 2 × ϕ ( d ) = ∑ i = 1 n ∑ d ∣ i d 2 × ϕ ( d ) × ( i d ) 2 = ∑ i = 1 n ∑ d ∣ i f ( d ) × ( i d ) 2 = ∑ i d = 1 n ∑ d = 1 ⌊ n i d ⌋ f ( d ) × ( i d ) 2 = ∑ i = 1 n ∑ d = 1 ⌊ n i ⌋ f ( d ) × i 2 = ∑ i = 1 n i 2 × F ( ⌊ n i ⌋ ) \displaystyle\sum_{i=1}^{n}\displaystyle\sum_{d|i}i^2\times \phi(d)=\displaystyle\sum_{i=1}^{n}\displaystyle\sum_{d|i}d^2\times \phi(d)\times(\frac{i}{d})^2=\displaystyle\sum_{i=1}^{n}\displaystyle\sum_{d|i}f(d)\times(\frac{i}{d})^2=\displaystyle\sum_{\frac{i}{d}=1}^{n}\displaystyle\sum_{d=1}^{\left \lfloor \frac{n}{\frac{i}{d}} \right \rfloor}f(d)\times (\frac{i}{d})^2=\displaystyle\sum_{i=1}^{n}\displaystyle\sum_{d=1}^{\left \lfloor \frac{n}{i} \right \rfloor}f(d)\times i^2=\displaystyle\sum_{i=1}^{n}i^2\times F(\left \lfloor \frac{n}{i} \right \rfloor) i=1∑nd∣i∑i2×ϕ(d)=i=1∑nd∣i∑d2×ϕ(d)×(di)2=i=1∑nd∣i∑f(d)×(di)2=di=1∑nd=1∑⌊din⌋f(d)×(di)2=i=1∑nd=1∑⌊in⌋f(d)×i2=i=1∑ni2×F(⌊in⌋)
故 F ( n ) = n 2 × ( n + 1 ) 2 4 − ∑ i = 2 n i 2 × F ( ⌊ n i ⌋ ) F(n)=\frac{n^2\times (n+1)^2}{4}-\displaystyle\sum_{i=2}^{n}i^2\times F(\left \lfloor \frac{n}{i} \right \rfloor) F(n)=4n2×(n+1)2−i=2∑ni2×F(⌊in⌋)
51Nod 1237 - 最大公约数之和 V3
过程同上,推导出需求 Φ ( n ) = ∑ i = 1 n ϕ ( i ) \Phi(n)=\displaystyle\sum_{i=1}^{n}\phi(i) Φ(n)=i=1∑nϕ(i)。
51Nod 1227 - 平均最小公倍数
令 A ( n ) = 1 n ∑ i = 1 n L C M ( n , i ) A(n)=\frac{1}{n}\displaystyle\sum_{i=1}^{n}LCM(n, i) A(n)=n1i=1∑nLCM(n,i),求 F ( a , b ) = A ( a ) + A ( a + 1 ) + ⋯ + A ( b ) F(a, b)=A(a)+A(a+1)+\dots+A(b) F(a,b)=A(a)+A(a+1)+⋯+A(b)
首先 A ( n ) = 1 n ∑ i = 1 n n × i ( n , i ) = ∑ g ∣ n ∑ i = 1 n g i × [ ( i , n g ) = 1 ] = ∑ g ∣ n n g × ϕ ( n g ) 2 = ∑ g ∣ n g × ϕ ( g ) 2 A(n)=\frac{1}{n} \displaystyle\sum_{i=1}^{n}\frac{n\times i}{(n, i)}=\displaystyle\sum_{g|n}\displaystyle\sum_{i=1}^{\frac{n}{g}}i\times[(i,\frac{n}{g})=1]=\displaystyle\sum_{g|n}\frac{\frac{n}{g}\times\phi(\frac{n}{g})}{2}=\displaystyle\sum_{g|n}\frac{g\times\phi(g)}{2} A(n)=n1i=1∑n(n,i)n×i=g∣n∑i=1∑gni×[(i,gn)=1]=g∣n∑2gn×ϕ(gn)=g∣n∑2g×ϕ(g)
所求即为 F ( n ) = ∑ i = 1 n ∑ g ∣ i g × ϕ ( g ) 2 = 1 2 ∑ d = 1 n ∑ i = 1 ⌊ n d ⌋ i × ϕ ( i ) F(n)=\displaystyle\sum_{i=1}^{n}\displaystyle\sum_{g|i}\frac{g\times\phi(g)}{2}=\frac{1}{2}\displaystyle\sum_{d=1}^{n}\displaystyle\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor}i\times\phi(i) F(n)=i=1∑ng∣i∑2g×ϕ(g)=21d=1∑ni=1∑⌊dn⌋i×ϕ(i)
那么只需求 H ( n ) = ∑ i = 1 n i × ϕ ( i ) H(n)=\displaystyle\sum_{i=1}^{n}i\times\phi(i) H(n)=i=1∑ni×ϕ(i)。
求法见下。
Tsinsen A1231 - Crash的数字表格
需求 ∑ i = 1 n ∑ i = 1 m L C M ( i , j ) \displaystyle\sum_{i=1}^{n}\displaystyle\sum_{i=1}^{m}LCM(i, j) i=1∑ni=1∑mLCM(i,j)
不妨令 n ≤ m n \le m n≤m
原式 = ∑ i = 1 n ∑ i = 1 m i × j ( i , j ) = ∑ g = 1 n g × ∑ i = 1 ⌊ n g ⌋ ∑ i = 1 ⌊ m g ⌋ i × j × [ ( i , j ) = 1 ] =\displaystyle\sum_{i=1}^{n}\displaystyle\sum_{i=1}^{m}\frac{i\times j}{(i, j)}=\displaystyle\sum_{g=1}^{n}g\times\displaystyle\sum_{i=1}^{\lfloor \frac{n}{g} \rfloor}\displaystyle\sum_{i=1}^{\lfloor \frac{m}{g} \rfloor}i\times j\times[(i,j)=1] =i=1∑ni=1∑m(i,j)i×j=g=1∑ng×i=1∑⌊gn⌋i=1∑⌊gm⌋i×j×[(i,j)=1]
令 F ( m , n ) = ∑ i = 1 m ∑ i = 1 n i × j × [ ( i , j ) = 1 ] F(m,n)=\displaystyle\sum_{i=1}^{m}\displaystyle\sum_{i=1}^{n}i\times j\times [(i,j)=1] F(m,n)=i=1∑mi=1∑ni×j×[(i,j)=1], G ( m , n ) = ∑ i = 1 m ∑ i = 1 n i × j = m ( m + 1 ) 2 × n ( n + 1 ) 2 G(m,n)=\displaystyle\sum_{i=1}^{m}\displaystyle\sum_{i=1}^{n}i\times j=\frac{m(m+1)}{2}\times \frac{n(n+1)}{2} G(m,n)=i=1∑mi=1∑ni×j=2m(m+1)×2n(n+1)
由容斥原理可得 F ( m , n ) = ∑ d = 1 n d 2 μ ( d ) G ( ⌊ m d ⌋ , ⌊ n d ⌋ ) F(m,n)=\displaystyle\sum_{d=1}^{n}d^2\mu(d)G(\lfloor \frac{m}{d} \rfloor,\lfloor \frac{n}{d} \rfloor) F(m,n)=d=1∑nd2μ(d)G(⌊dm⌋,⌊dn⌋)
SPOJ DIVCNT2 - Counting Divisors (square)
求 ∑ i = 1 n σ 0 ( i 2 ) , 1 ≤ n ≤ 1 0 12 \displaystyle\sum_{i=1}^{n}\sigma_0(i^2),1\le n\le10^{12} i=1∑nσ0(i2),1≤n≤1012
考虑 n n n 的约数和 n 2 n^2 n2 的约数在指数上的联系,可以得出 σ 0 ( n 2 ) = ∑ d ∣ n 2 w i d t h ( d ) \sigma_0(n^2)=\displaystyle\sum_{d|n}2^{width(d)} σ0(n2)=d∣n∑2width(d),其中 w i d t h ( d ) width(d) width(d) 为 d d d 的不同质因子数,可以构造出 w i d t h ( d ) = ∑ p ∣ d μ 2 ( p ) width(d)=\displaystyle\sum_{p|d}\mu^2(p) width(d)=p∣d∑μ2(p)
所以 σ 0 ( n 2 ) = ∑ d ∣ n ∑ p ∣ d μ 2 ( p ) = ( μ 2 ∗ 1 ) ∗ 1 ) = μ 2 ∗ ( 1 ∗ 1 ) = μ 2 ∗ σ 0 \sigma_0(n^2)=\displaystyle\sum_{d|n}\displaystyle\sum_{p|d}\mu^2(p)=(\mu^2*1)*1)=\mu^2*(1*1)=\mu^2*\sigma_0 σ0(n2)=d∣n∑p∣d∑μ2(p)=(μ2∗1)∗1)=μ2∗(1∗1)=μ2∗σ0
所求 ∑ i = 1 n ∑ d ∣ i μ 2 ( d ) σ 0 ( i d ) = ∑ d = 1 n μ 2 ( d ) ∑ i = 1 ⌊ n d ⌋ σ 0 ( i ) \displaystyle\sum_{i=1}^{n}\displaystyle\sum_{d|i}\mu^2(d)\sigma_0(\frac{i}{d})=\displaystyle\sum_{d=1}^{n}\mu^2(d)\displaystyle\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor}\sigma_0(i) i=1∑nd∣i∑μ2(d)σ0(di)=d=1∑nμ2(d)i=1∑⌊dn⌋σ0(i)
σ 0 ( n ) \sigma_0(n) σ0(n) 可在 O ( n ) O(\sqrt{n}) O(n) 的复杂度求出来,对 μ 2 ( n ) \mu^2(n) μ2(n) 考虑容斥后算出 μ 2 ( n ) = ∑ i = 1 ⌊ n ⌋ μ ( i ) × ⌊ n i 2 ⌋ \mu^2(n)=\displaystyle\sum_{i=1}^{\lfloor \sqrt{n} \rfloor}\mu(i)\times\lfloor \frac{n}{i^2} \rfloor μ2(n)=i=1∑⌊n⌋μ(i)×⌊i2n⌋
51Nod 1222 - 最小公倍数计数
令 f ( n ) = ∑ i = 1 n ∑ j = 1 n [ L C M ( i , j ) = n ] f(n)=\displaystyle\sum_{i=1}^{n}\displaystyle\sum_{j=1}^{n}[LCM(i,j)=n] f(n)=i=1∑nj=1∑n[LCM(i,j)=n],求 ∑ i = a b f ( i ) \displaystyle\sum_{i=a}^{b}f(i) i=a∑bf(i)
BZOJ 4176 - Lucas的数论
求 ∑ i = 1 n ∑ j = 1 n σ 0 ( i j ) \displaystyle\sum_{i=1}^{n}\displaystyle\sum_{j=1}^{n}\sigma_0(i j) i=1∑nj=1∑nσ0(ij)
引理: σ 0 ( m n ) = ∑ i ∣ n ∑ j ∣ m [ ( i , j ) = 1 ] \sigma_0(mn)=\displaystyle\sum_{i|n}\displaystyle\sum_{j|m}[(i,j)=1] σ0(mn)=i∣n∑j∣m∑[(i,j)=1]
证明:考虑提取质因子 p p p 进行归纳,具体过程略。
直接带入引理,所求 ∑ i = 1 n ∑ j = 1 n σ 0 ( i × j ) = ∑ i = 1 n ∑ j = 1 n ∑ t ∣ i ∑ s ∣ j [ ( s , t ) = 1 ] = ∑ i = 1 n ∑ j = 1 n ∑ t ∣ i ∑ s ∣ j ∑ d ∣ ( s , t ) μ ( d ) = ∑ i = 1 n ∑ j = 1 n ∑ d ∣ i , d ∣ j μ ( d ) σ 0 ( i d ) σ 0 ( j d ) = ∑ d = 1 n μ ( d ) ( ∑ i = 1 ⌊ n d ⌋ σ 0 ( i ) ) 2 \displaystyle\sum_{i=1}^{n}\displaystyle\sum_{j=1}^{n}\sigma_0(i\times j)=\displaystyle\sum_{i=1}^{n}\displaystyle\sum_{j=1}^{n}\displaystyle\sum_{t|i}\displaystyle\sum_{s|j}[(s,t)=1]=\displaystyle\sum_{i=1}^{n}\displaystyle\sum_{j=1}^{n}\displaystyle\sum_{t|i}\displaystyle\sum_{s|j}\displaystyle\sum_{d|(s,t)}\mu(d)=\displaystyle\sum_{i=1}^{n}\displaystyle\sum_{j=1}^{n}\displaystyle\sum_{d|i,d|j}\mu(d)\sigma_0(\frac{i}{d})\sigma_0(\frac{j}{d})=\displaystyle\sum_{d=1}^{n}\mu(d)(\displaystyle\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor}\sigma_0(i))^2 i=1∑nj=1∑nσ0(i×j)=i=1∑nj=1∑nt∣i∑s∣j∑[(s,t)=1]=i=1∑nj=1∑nt∣i∑s∣j∑d∣(s,t)∑μ(d)=i=1∑nj=1∑nd∣i,d∣j∑μ(d)σ0(di)σ0(dj)=d=1∑nμ(d)(i=1∑⌊dn⌋σ0(i))2
双倍经验:BZOJ3994: [SDOI2015]约数个数和
51Nod 1220 - 约数之和
求 ∑ i = 1 n ∑ j = 1 n σ 1 ( i j ) \displaystyle\sum_{i=1}^{n}\displaystyle\sum_{j=1}^{n}\sigma_1(ij) i=1∑nj=1∑nσ1(ij)
引理: σ 1 ( m n ) = ∑ s ∣ m ∑ t ∣ n m t s [ ( s , t ) = 1 ] \sigma_1(mn)=\displaystyle\sum_{s|m}\displaystyle\sum_{t|n}\frac{mt}{s}[(s,t)=1] σ1(mn)=s∣m∑t∣n∑smt[(s,t)=1]
证明:同理考虑分离质因子 p p p 后归纳。