最近刷莫反题目,刷着刷着,发现problem b这道题目可以用三种方法推出来,觉得记下来以后可以方便复习,也算巩固一下。
problem b
这类问题是:
∑
i
=
1
n
∑
j
=
1
m
[
g
c
d
(
i
,
j
)
=
d
]
\sum_{i = 1} ^ n \sum_{j = 1}^m[gcd(i , j) = d]
i=1∑nj=1∑m[gcd(i,j)=d]
关于这个问题,我们再熟悉不过,答案是:
∑
d
′
=
1
m
i
n
(
n
d
,
n
d
)
μ
(
d
′
)
[
n
d
∗
d
′
]
∗
[
m
d
∗
d
′
]
\sum_{d' = 1}^{min(\frac{n}{d} , \frac{n}{d})}\mu(d')[\frac{n}{d*d'}] * [\frac{m}{d*d'}]
d′=1∑min(dn,dn)μ(d′)[d∗d′n]∗[d∗d′m]
推法一(容斥原理):
考虑经典转换:
∑
i
=
1
n
∑
j
=
1
m
[
g
c
d
(
i
,
j
)
=
d
]
=
∑
i
=
1
n
∑
j
=
1
m
[
g
c
d
(
i
/
d
,
j
/
d
)
=
1
]
(1)
\sum_{i = 1} ^ n \sum_{j = 1}^m[gcd(i , j) = d] = \sum_{i = 1} ^ n \sum_{j = 1}^m[gcd(i / d , j / d) = 1]\tag{1}
i=1∑nj=1∑m[gcd(i,j)=d]=i=1∑nj=1∑m[gcd(i/d,j/d)=1](1)
上式我们还可以套用一个经典变换。
∑
i
=
1
n
∑
j
=
1
m
[
g
c
d
(
i
/
d
,
j
/
d
)
=
1
]
=
∑
i
=
1
n
d
∑
j
=
1
m
d
[
g
c
d
(
i
,
j
)
=
1
]
(2)
\sum_{i = 1} ^ n \sum_{j = 1}^m[gcd(i / d , j / d) = 1]= \sum_{i = 1} ^ {\frac{n}{d}} \sum_{j = 1}^{\frac{m}{d}}[gcd(i , j ) = 1] \tag{2}
i=1∑nj=1∑m[gcd(i/d,j/d)=1]=i=1∑dnj=1∑dm[gcd(i,j)=1](2)
即:考虑
1
1
1到
n
/
d
n/d
n/d,
1
1
1到
m
/
d
m / d
m/d中,有多少个数对满足互质的个数。
很明显,互质的个数 = 总数 - 不互质的个数.
不互质的个数是几个呢?
我们考虑枚举倍数,2的倍数的个数,3的倍数的个数,4的倍数的个数…考虑符号的时候,发现和莫比乌斯函数的符号是一致的,这个以前的博客证明过,在这不啰嗦。与
(
n
/
d
)
∗
(
m
/
d
)
(n/d) * (m/d)
(n/d)∗(m/d) 合并后得答案:
∑
i
=
1
m
i
n
(
n
,
m
)
[
n
d
∗
i
]
∗
[
m
d
∗
i
]
∗
μ
(
i
)
(3)
\sum_{i = 1}^{min(n , m)}[\frac{n}{d*i}] * [\frac{m}{d*i}] * \mu(i)\tag{3}
i=1∑min(n,m)[d∗in]∗[d∗im]∗μ(i)(3)
推法二(根据莫比乌斯函数性质):
由莫比乌斯函数性质可得:
∑
i
=
1
n
d
∑
j
=
1
m
d
[
g
c
d
(
i
,
j
)
=
1
]
=
∑
i
=
1
n
d
∑
j
=
1
m
d
∑
d
′
∣
g
c
d
(
i
,
j
)
μ
(
d
′
)
(4)
\sum_{i = 1} ^ {\frac{n}{d}} \sum_{j = 1}^{\frac{m}{d}}[gcd(i , j ) = 1] =\sum_{i = 1} ^ {\frac{n}{d}} \sum_{j = 1}^{\frac{m}{d}}\sum_{d' | gcd(i , j)}\mu(d')\tag{4}
i=1∑dnj=1∑dm[gcd(i,j)=1]=i=1∑dnj=1∑dmd′∣gcd(i,j)∑μ(d′)(4)
d
′
d'
d′是
g
c
d
(
i
,
j
)
gcd(i , j)
gcd(i,j)的因子,我们观察到前面两个求和只是在修饰
μ
\mu
μ,给
μ
\mu
μ提供一个范围,我们还发现:
d
′
d'
d′实际上可以取到
1
∼
m
i
n
(
n
d
,
m
d
)
1 \sim min(\frac{n}{d},\frac{m}{d})
1∼min(dn,dm),这个很容易想明白:如果
i
,
j
i , j
i,j一样,那么最大共因子是他的本身,所以
1
∼
m
i
n
(
n
d
,
m
d
)
1 \sim min(\frac{n}{d},\frac{m}{d})
1∼min(dn,dm)的范围内,
d
′
d'
d′都可以取到。
现在我们要思考的是:每个
μ
(
d
′
)
\mu(d')
μ(d′)被枚举了多少次?我们什么时候能取到这个
d
′
d'
d′呢,答案是当
i
,
j
i , j
i,j是
d
′
d'
d′的倍数的时候,
g
c
d
(
i
,
j
)
gcd(i , j)
gcd(i,j)是
d
′
d'
d′的倍数。答案呼之欲出:
[
n
d
∗
d
′
]
∗
[
m
d
∗
d
′
]
[\frac{n}{d*d'}] * [\frac{m}{d*d'}]
[d∗d′n]∗[d∗d′m]
∑
d
′
=
1
m
i
n
(
n
d
,
n
d
)
μ
(
d
′
)
[
n
d
∗
d
′
]
∗
[
m
d
∗
d
′
]
(5)
\sum_{d' = 1}^{min(\frac{n}{d} , \frac{n}{d})}\mu(d')[\frac{n}{d*d'}] * [\frac{m}{d*d'}]\tag{5}
d′=1∑min(dn,dn)μ(d′)[d∗d′n]∗[d∗d′m](5)
根据我们严谨的分析,
(
4
)
与
(
5
)
(4)与(5)
(4)与(5)等价。
推法三(莫比乌斯反演):
莫比乌斯反演:
若
F
(
n
)
=
∑
n
∣
d
f
(
d
)
(6)
F(n) = \sum_{n | d}f(d)\tag{6}
F(n)=n∣d∑f(d)(6)
则
f
(
n
)
=
∑
n
∣
d
μ
(
d
n
)
F
(
d
)
(7)
f(n) = \sum_{n | d}\mu(\frac{d}{n})F(d)\tag{7}
f(n)=n∣d∑μ(nd)F(d)(7)
对于本题:
我们设:
F
(
d
)
=
∑
i
=
1
n
∑
j
=
1
m
[
d
∣
g
c
d
(
i
,
j
)
]
(8)
F(d) = \sum_{i = 1}^{n}\sum_{j = 1} ^ {m}[d | gcd(i , j )]\tag{8}
F(d)=i=1∑nj=1∑m[d∣gcd(i,j)](8)
f
(
d
)
=
∑
i
=
1
n
∑
j
=
1
m
[
g
c
d
(
i
,
j
)
=
d
]
(9)
f(d) = \sum_{i = 1}^{n}\sum_{j = 1} ^ {m}[gcd(i , j ) = d]\tag{9}
f(d)=i=1∑nj=1∑m[gcd(i,j)=d](9)
很明显
(
8
)
(
9
)
(8)(9)
(8)(9)满足
(
6
)
(6)
(6),那么我们可以根据
(
7
)
(7)
(7)式子直接列出:
f
(
n
)
=
∑
n
∣
d
μ
(
d
n
)
∑
i
=
1
n
∑
j
=
1
m
[
d
∣
g
c
d
(
i
,
j
)
]
(10)
f(n) = \sum_{n | d}\mu(\frac{d}{n})\sum_{i = 1}^{n}\sum_{j = 1} ^ {m}[d | gcd(i , j )]\tag{10}
f(n)=n∣d∑μ(nd)i=1∑nj=1∑m[d∣gcd(i,j)](10)
为了不引起歧义,原来的n , m改为a , b
我们发现:
d
/
n
d/n
d/n是枚举的从
1
∼
n
1\sim n
1∼n的自然数,我们将
d
/
n
d/n
d/n设为
d
′
d'
d′则:
f
(
n
)
=
∑
d
′
μ
(
d
′
)
∑
i
=
1
a
∑
j
=
1
b
[
d
′
n
∣
g
c
d
(
i
,
j
)
]
(11)
f(n) = \sum_{d'}\mu(d')\sum_{i = 1}^{a}\sum_{j = 1} ^ {b}[d'n | gcd(i , j )]\tag{11}
f(n)=d′∑μ(d′)i=1∑aj=1∑b[d′n∣gcd(i,j)](11)
根据推
(
5
)
(5)
(5)式的方法,我们推出:
f
(
n
)
=
∑
d
′
=
1
μ
(
d
′
)
∑
i
=
1
a
∑
j
=
1
b
[
d
′
n
∣
g
c
d
(
i
,
j
)
]
(12)
f(n) = \sum_{d' = 1}\mu(d')\sum_{i = 1}^{a}\sum_{j = 1} ^ {b}[d'n | gcd(i , j )]\tag{12}
f(n)=d′=1∑μ(d′)i=1∑aj=1∑b[d′n∣gcd(i,j)](12)
f
(
n
)
=
∑
d
′
=
1
μ
(
d
′
)
[
a
d
′
∗
n
]
[
b
d
′
∗
n
]
(13)
f(n) = \sum_{d' = 1}\mu(d')[\frac{a}{d'*n}][\frac{b}{d'*n}] \tag{13}
f(n)=d′=1∑μ(d′)[d′∗na][d′∗nb](13)
答案显而易见:
f
(
d
)
=
∑
d
′
=
1
μ
(
d
′
)
[
a
d
′
∗
d
]
[
b
d
′
∗
d
]
(14)
f(d) = \sum_{d' = 1}\mu(d')[\frac{a}{d'*d}][\frac{b}{d'*d}]\tag{14}
f(d)=d′=1∑μ(d′)[d′∗da][d′∗db](14)
通过三种方法:我们发现推出的式子完全一样。殊途同归说的便是这吧。我觉得这也是数论的魅力。代码略。