[国家集训队] Crash的数字表格/JZPTAB
Statement
计算: A n s = ∑ i = 1 N ∑ j = 1 M lcm ( i , j ) Ans=\sum_{i=1}^N\sum_{j=1}^M\text{lcm}(i,j) Ans=∑i=1N∑j=1Mlcm(i,j).
Solution
∑ i = 1 N ∑ j = 1 M lcm ( i , j ) = ∑ i = 1 N ∑ j = 1 M i ⋅ j gcd ( i , j ) \sum_{i=1}^N\sum_{j=1}^M\text{lcm}(i,j)=\sum_{i=1}^N\sum_{j=1}^M\frac{i\cdot j}{\gcd(i,j)} ∑i=1N∑j=1Mlcm(i,j)=∑i=1N∑j=1Mgcd(i,j)i⋅j.
- A n s = ∑ d = 1 m i n ( N , M ) 1 d ∑ i = 1 N ∑ j = 1 M i ⋅ j [ gcd ( i , j ) = d ] Ans=\sum_{d=1}^{min(N,M)}\frac{1}{d}\sum_{i=1}^N\sum_{j=1}^Mi\cdot j[\gcd(i,j)=d] Ans=∑d=1min(N,M)d1∑i=1N∑j=1Mi⋅j[gcd(i,j)=d]
设 f ( d ) = ∑ i = 1 N ∑ j = 1 M i ⋅ j [ gcd ( i , j ) = d ] f(d)=\sum_{i=1}^N\sum_{j=1}^Mi\cdot j[\gcd(i,j)=d] f(d)=∑i=1N∑j=1Mi⋅j[gcd(i,j)=d].
设 g ( d ) = ∑ d ∣ d ′ f ( d ′ ) = ∑ i = 1 N ∑ j = 1 M i ⋅ j [ d ∣ gcd ( i , j ) ] = ∑ i = 1 N ∑ j = 1 M i ⋅ j [ d ∣ i ] [ d ∣ j ] g(d)=\sum_{d|d'}f(d')=\sum_{i=1}^N\sum_{j=1}^Mi\cdot j[d|\gcd(i,j)]=\sum_{i=1}^N\sum_{j=1}^Mi\cdot j[d|i][d|j] g(d)=∑d∣d′f(d′)=∑i=1N∑j=1Mi⋅j[d∣gcd(i,j)]=∑i=1N∑j=1Mi⋅j[d∣i][d∣j].
则 g ( d ) = ∑ i = 1 N i [ d ∣ i ] ∑ j = 1 M j [ d ∣ j ] = [ d ( 1 + ⌊ N d ⌋ ) ⌊ N d ⌋ 2 ] × [ d ( 1 + ⌊ M d ⌋ ) ( ⌊ M d ⌋ ) 2 ] g(d)=\sum_{i=1}^Ni[d|i]\sum_{j=1}^Mj[d|j]=[d\frac{(1+\lfloor\frac{N}{d}\rfloor)\lfloor\frac{N}{d}\rfloor}{2}]\times[d\frac{(1+\lfloor\frac{M}{d}\rfloor)(\lfloor\frac{M}{d}\rfloor)}{2}] g(d)=∑i=1Ni[d∣i]∑j=1Mj[d∣j]=[d2(1+⌊dN⌋)⌊dN⌋]×[d2(1+⌊dM⌋)(⌊dM⌋)].
则 f ( d ) = ∑ d ∣ d ′ μ ( d ′ d ) g ( d ′ ) = ∑ d ∣ d ′ μ ( d ′ d ) ⋅ d ′ 2 ⋅ [ ( 1 + ⌊ N d ⌋ ) ⌊ N d ⌋ 2 ] × [ ( 1 + ⌊ M d ⌋ ) ( ⌊ M d ⌋ ) 2 ] f(d)=\sum_{d|d'}\mu(\frac{d'}{d})g(d')=\sum_{d|d'}\mu(\frac{d'}{d})\cdot d'^2\cdot[\frac{(1+\lfloor\frac{N}{d}\rfloor)\lfloor\frac{N}{d}\rfloor}{2}]\times[\frac{(1+\lfloor\frac{M}{d}\rfloor)(\lfloor\frac{M}{d}\rfloor)}{2}] f(d)=∑d∣d′μ(dd′)g(d′)=∑d∣d′μ(dd′)⋅d′2⋅[2(1+⌊dN⌋)⌊dN⌋]×[2(1+⌊dM⌋)(⌊dM⌋)].
不妨设 g ( n , m ) = ( 1 + n ) n 2 × ( 1 + m ) m 2 g(n,m)=\frac{(1+n)n}{2}\times\frac{(1+m)m}{2} g(n,m)=2(1+n)n×2(1+m)m.
整理可得:
A
n
s
=
∑
d
=
1
min
(
N
,
M
)
1
d
f
(
d
)
=
∑
d
=
1
min
(
N
,
M
)
1
d
∑
d
∣
d
′
μ
(
d
′
d
)
⋅
d
′
2
⋅
g
(
⌊
N
d
′
⌋
,
⌊
M
d
′
⌋
)
=
∑
d
=
1
min
(
N
,
M
)
d
∑
t
=
1
min
(
N
d
,
M
d
)
μ
(
t
)
⋅
t
2
⋅
g
(
⌊
N
t
d
⌋
,
⌊
M
t
d
⌋
)
\begin{aligned} Ans&=\sum_{d=1}^{\min(N,M)}\frac{1}{d}f(d)\\ &=\sum_{d=1}^{\min(N,M)}\frac{1}{d}\sum_{d|d'}\mu(\frac{d'}{d})\cdot d'^2\cdot g(\lfloor\frac{N}{d'}\rfloor,\lfloor\frac{M}{d'}\rfloor)\\ &=\sum_{d=1}^{\min(N,M)}d\sum_{t=1}^{\min(\frac{N}{d},\frac{M}{d})}\mu(t)\cdot t^2\cdot g(\lfloor\frac{N}{td}\rfloor,\lfloor\frac{M}{td}\rfloor) \end{aligned}
Ans=d=1∑min(N,M)d1f(d)=d=1∑min(N,M)d1d∣d′∑μ(dd′)⋅d′2⋅g(⌊d′N⌋,⌊d′M⌋)=d=1∑min(N,M)dt=1∑min(dN,dM)μ(t)⋅t2⋅g(⌊tdN⌋,⌊tdM⌋)
我们不妨设:
S
u
m
(
n
,
m
)
=
∑
i
=
1
min
(
n
,
m
)
μ
(
i
)
⋅
i
2
⋅
g
(
⌊
n
i
⌋
,
⌊
m
i
⌋
)
Sum(n,m)=\sum_{i=1}^{\min(n,m)}\mu(i)\cdot i^2\cdot g(\lfloor\frac{n}{i}\rfloor,\lfloor\frac{m}{i}\rfloor)
Sum(n,m)=∑i=1min(n,m)μ(i)⋅i2⋅g(⌊in⌋,⌊im⌋).
可得 A n s = ∑ d = 1 min ( N , M ) d ⋅ sum ( ⌊ N d ⌋ , ⌊ M d ⌋ ) Ans=\sum_{d=1}^{\min(N,M)}d\cdot\text{ sum }(\lfloor\frac{N}{d}\rfloor,\lfloor\frac{M}{d}\rfloor) Ans=∑d=1min(N,M)d⋅ sum (⌊dN⌋,⌊dM⌋).
时间复杂度-二次数论分块: O ( N × N ) = O ( N ) O(\sqrt{N}\times\sqrt{N})=O(N) O(N×N)=O(N).