前置知识
狄利克雷卷积
形如
f
(
n
)
=
∑
d
∣
n
g
(
d
)
∗
h
(
n
d
)
f(n) = \sum\limits_{d|n} g(d)*h(\dfrac{n}{d})
f(n)=d∣n∑g(d)∗h(dn),记作
f
f
f是
g
g
g 和
h
h
h的狄利克雷卷积,记作
f
=
g
∗
h
f = g*h
f=g∗h。
e
g
.
eg.
eg.
f
(
n
)
=
∑
d
∣
n
g
(
d
)
⇒
f
=
g
∗
1
f(n)=\sum\limits_{d|n}g(d) \Rightarrow f = g*1
f(n)=d∣n∑g(d)⇒f=g∗1
f
=
f
∗
e
f = f * e
f=f∗e,
e
e
e 为单位函数
狄利克雷卷积的性质
狄利克雷卷积满足交换律和结合律,即
f
=
g
∗
h
∗
k
=
g
∗
k
∗
h
=
g
∗
(
h
∗
k
)
f = g*h*k=g*k*h=g*(h*k)
f=g∗h∗k=g∗k∗h=g∗(h∗k)
进入正题
莫比乌斯函数
定义
定义可以参考其他 b l o g blog blog (TAT,分段函数实在太难打了)
性质
∑
d
∣
n
μ
(
d
)
=
[
n
=
1
]
\sum\limits_{d|n} \mu(d)=[n=1]
d∣n∑μ(d)=[n=1],即是
μ
∗
1
=
e
(
n
)
\mu * 1 = e(n)
μ∗1=e(n),
e
e
e 为单位函数。
这是莫比乌斯函数很重要的性质,反演基本基于此。
证明如下:
考虑到每个
u
(
d
)
u(d)
u(d) 对
a
n
s
ans
ans 贡献只是
1
1
1 或
−
1
-1
−1(
0
0
0 不用管)
设
n
=
∏
i
=
1
m
p
i
a
i
n = \prod\limits_{i=1}^{m}p_i^{a_i}
n=i=1∏mpiai
那么
n
>
1
n > 1
n>1 时
a
n
s
=
∑
k
=
0
m
(
−
1
)
k
C
m
k
=
(
1
−
1
)
m
=
0
ans = \sum\limits_{k=0}^{m}(-1)^kC_m^k=(1-1)^m=0
ans=k=0∑m(−1)kCmk=(1−1)m=0
n
=
1
n = 1
n=1时,
a
n
s
=
1
ans = 1
ans=1。
证毕。
莫比乌斯反演定理
若有 f ( n ) = ∑ d ∣ n g ( d ) f(n) = \sum\limits_{d|n}g(d) f(n)=d∣n∑g(d),则有 g ( n ) = ∑ d ∣ n f ( d ) ∗ μ ( n d ) g(n) = \sum\limits_{d|n}f(d)*\mu(\dfrac{n}{d}) g(n)=d∣n∑f(d)∗μ(dn)
证明如下:
现有
f
=
g
∗
1
f = g*1
f=g∗1,则有
f
∗
μ
=
g
∗
(
μ
∗
1
)
=
g
∗
e
=
g
f*\mu=g*(\mu*1)=g*e=g
f∗μ=g∗(μ∗1)=g∗e=g(这是狄利克雷卷积的证法,当然还有其他证法,可参考其他
b
l
o
g
blog
blog)
莫比乌斯函数的应用
①:让我们先来考虑下面一道最最最经典的题:
求
a
n
s
=
∑
i
=
1
n
∑
j
=
1
m
[
g
c
d
(
i
,
j
)
=
=
1
]
(
n
<
m
)
ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}[gcd(i,j)==1](n < m)
ans=i=1∑nj=1∑m[gcd(i,j)==1](n<m)
解:考虑到
[
g
c
d
(
i
,
j
)
=
=
1
]
[gcd(i,j) == 1]
[gcd(i,j)==1] 是单位函数, 而
e
=
μ
∗
1
e = \mu*1
e=μ∗1,于是
a
n
s
=
∑
i
=
1
n
∑
j
=
1
m
∑
d
∣
g
c
d
(
i
,
j
)
μ
(
d
)
=
∑
i
=
1
n
∑
j
=
1
m
∑
d
∣
i
,
d
∣
j
μ
(
d
)
ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\sum\limits_{d|gcd(i,j)}\mu(d)=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\sum\limits_{d|i,d|j}\mu(d)
ans=i=1∑nj=1∑md∣gcd(i,j)∑μ(d)=i=1∑nj=1∑md∣i,d∣j∑μ(d)
考虑每个
μ
(
d
)
\mu(d)
μ(d) 出现了多少次,则有
a
n
s
=
∑
d
=
1
n
μ
(
d
)
∗
⌊
n
d
⌋
∗
⌊
m
d
⌋
ans = \sum\limits_{d=1}^{n}\mu(d)*\left\lfloor\dfrac{n}{d}\right\rfloor*\left\lfloor\dfrac{m}{d}\right\rfloor
ans=d=1∑nμ(d)∗⌊dn⌋∗⌊dm⌋,然后用除法分块,可以使复杂度达到
O
(
n
)
O(\sqrt{n})
O(n)
②:进阶版
求
a
n
s
=
∑
i
=
1
n
∑
j
=
1
m
g
c
d
(
i
,
j
)
(
n
<
m
)
ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}gcd(i,j)(n < m)
ans=i=1∑nj=1∑mgcd(i,j)(n<m)
枚举
d
=
g
c
d
(
i
,
j
)
d = gcd(i,j)
d=gcd(i,j),有
a
n
s
=
∑
d
=
1
n
d
∑
i
=
1
n
∑
j
=
1
m
[
g
c
d
(
i
/
d
,
j
/
d
)
=
=
1
]
=
∑
d
=
1
n
d
∑
i
=
1
⌊
n
d
⌋
∑
j
=
1
⌊
m
d
⌋
[
g
c
d
(
i
,
j
)
=
=
1
]
=
∑
d
=
1
n
d
∑
i
=
1
⌊
n
d
⌋
∑
j
=
1
⌊
m
d
⌋
∑
k
∣
i
,
k
∣
j
μ
(
k
)
=
∑
d
=
1
n
d
∑
k
=
1
⌊
n
d
⌋
μ
(
k
)
⌊
n
k
d
⌋
⌊
m
k
d
⌋
ans = \sum\limits_{d=1}^{n}d\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}[gcd(i/d,j/d)==1] = \sum\limits_{d=1}^{n}d\sum\limits_{i=1}^{{\tiny\left\lfloor\dfrac{n}{d}\right\rfloor}}\sum\limits_{j=1}^{{\tiny\left\lfloor\dfrac{m}{d}\right\rfloor}}[gcd(i,j)==1]=\sum\limits_{d=1}^{n}d\sum\limits_{i=1}^{{\tiny\left\lfloor\dfrac{n}{d}\right\rfloor}}\sum\limits_{j=1}^{{\tiny\left\lfloor\dfrac{m}{d}\right\rfloor}}\sum\limits_{k|i,k|j}\mu(k)=\sum\limits_{d=1}^{n}d\sum\limits_{k=1}^{{\tiny\left\lfloor\dfrac{n}{d}\right\rfloor}}\mu(k)\left\lfloor\dfrac{n}{kd}\right\rfloor\left\lfloor\dfrac{m}{kd}\right\rfloor
ans=d=1∑ndi=1∑nj=1∑m[gcd(i/d,j/d)==1]=d=1∑ndi=1∑⌊dn⌋j=1∑⌊dm⌋[gcd(i,j)==1]=d=1∑ndi=1∑⌊dn⌋j=1∑⌊dm⌋k∣i,k∣j∑μ(k)=d=1∑ndk=1∑⌊dn⌋μ(k)⌊kdn⌋⌊kdm⌋,然后
⌊
n
d
⌋
\left\lfloor\dfrac{n}{d}\right\rfloor
⌊dn⌋分块,
⌊
n
k
d
⌋
\left\lfloor\dfrac{n}{kd}\right\rfloor
⌊kdn⌋分块。
复杂度是
O
(
n
∗
n
=
n
)
O(\sqrt{n}*\sqrt{n}=n)
O(n∗n=n)
③:再进阶一点点
这题是
b
z
o
j
2154
bzoj~2154
bzoj 2154 Crash的数字表格 / JZPTAB
求
a
n
s
=
∑
i
=
1
n
∑
j
=
1
m
l
c
m
(
i
,
j
)
ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}lcm(i,j)
ans=i=1∑nj=1∑mlcm(i,j)
众所周知,
l
c
m
(
i
,
j
)
=
i
∗
j
g
c
d
(
i
,
j
)
lcm(i,j) = \dfrac{i*j}{gcd(i,j)}
lcm(i,j)=gcd(i,j)i∗j,于是
a
n
s
=
∑
i
=
1
n
∑
j
=
1
m
i
∗
j
g
c
d
(
i
,
j
)
=
∑
d
=
1
n
∑
i
=
1
n
∑
j
=
1
m
i
∗
j
d
∗
[
g
c
d
(
i
,
j
)
=
=
d
]
=
∑
d
=
1
n
∑
i
=
1
⌊
n
d
⌋
∑
j
=
1
⌊
m
d
⌋
i
j
d
[
g
c
d
(
i
,
j
)
=
=
1
]
=
∑
d
=
1
n
d
∑
i
=
1
⌊
n
d
⌋
∑
j
=
1
⌊
m
d
⌋
i
j
∑
k
∣
i
,
k
∣
j
μ
(
k
)
ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\dfrac{i*j}{gcd(i,j)}=\sum\limits_{d=1}^{n}\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\dfrac{i*j}{d}*[gcd(i,j)==d]=\sum\limits_{d=1}^{n}\sum\limits_{i=1}^{{\tiny \left\lfloor\dfrac{n}{d}\right\rfloor}}\sum\limits_{j=1}^{{\tiny \left\lfloor\dfrac{m}{d}\right\rfloor}}ijd[gcd(i,j)==1]=\sum\limits_{d=1}^{n}d\sum\limits_{i=1}^{{\tiny \left\lfloor\dfrac{n}{d}\right\rfloor}}\sum\limits_{j=1}^{{\tiny \left\lfloor\dfrac{m}{d}\right\rfloor}}ij\sum\limits_{k|i,k|j}\mu(k)
ans=i=1∑nj=1∑mgcd(i,j)i∗j=d=1∑ni=1∑nj=1∑mdi∗j∗[gcd(i,j)==d]=d=1∑ni=1∑⌊dn⌋j=1∑⌊dm⌋ijd[gcd(i,j)==1]=d=1∑ndi=1∑⌊dn⌋j=1∑⌊dm⌋ijk∣i,k∣j∑μ(k),然后考虑枚举
k
k
k,有
a
n
s
=
∑
d
=
1
n
d
∑
k
=
1
⌊
n
d
⌋
μ
(
k
)
∗
k
2
∑
i
=
1
⌊
n
k
d
⌋
i
∑
j
=
1
⌊
m
k
d
⌋
j
ans = \sum\limits_{d=1}^{n}d\sum\limits_{k=1}^{{\tiny \left\lfloor\dfrac{n}{d}\right\rfloor}}\mu(k)*k^2\sum\limits_{i=1}^{{\tiny \left\lfloor\dfrac{n}{kd}\right\rfloor}}i\sum\limits_{j=1}^{{\tiny \left\lfloor\dfrac{m}{kd}\right\rfloor}}j
ans=d=1∑ndk=1∑⌊dn⌋μ(k)∗k2i=1∑⌊kdn⌋ij=1∑⌊kdm⌋j,然后两个整除分块即可,复杂度
O
(
n
)
O(n)
O(n)
代码如下
#include <bits/stdc++.h>
#define N 10000005
#define LL long long
#define mod 20101009
using namespace std;
int p[N], cnt, mu[N], s[N], g[N];
bool x[N];
LL z = 1, t;
int main(){
int i, j, n, m, l1, l2, r1, r2, sum, ans, a, b;
scanf("%d%d", &n, &m);
if(n > m) swap(n, m);
mu[1] = 1;
for(i = 2; i <= m; i++){
if(!x[i]) x[i] = 1, p[++cnt] = i, mu[i] = -1;
for(j = 1; j <= cnt; j++){
t = z * i * p[j];
if(t > m) break;
x[t] = 1;
if(i % p[j] == 0) break;
mu[t] = -mu[i];
}
}
for(i = 1; i <= m; i++) g[i] = (g[i - 1] + i) % mod, s[i] = (s[i - 1] + z * mu[i] * i * i % mod + mod) % mod;
ans = sum = 0;
for(l1 = 1; l1 <= n; l1 = r1 + 1){
j = n / l1; r1 = min(n / (n / l1), m / (m / l1));
a = n / l1, b = m / l1;
sum = 0;
for(l2 = 1; l2 <= j; l2 = r2 + 1){
r2 = min(a / (a / l2), b / (b / l2));
sum = (sum + z * (s[r2] - s[l2 - 1]) * g[a / l2] % mod * g[b / l2] % mod) % mod;
}
ans = (ans + z * sum * (g[r1] - g[l1 - 1]) % mod) % mod;
}
printf("%d", (ans + mod) % mod);
return 0;
}
④ 再进阶一点点
求
a
n
s
=
∑
i
=
1
n
∑
j
=
1
m
[
g
c
d
(
i
,
j
)
∈
p
r
i
m
e
]
(
n
<
m
)
ans = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}[gcd(i,j)\in prime](n < m)
ans=i=1∑nj=1∑m[gcd(i,j)∈prime](n<m)
考虑枚举
n
n
n 以内的素数,于是有
a
n
s
=
∑
p
∑
i
=
1
n
∑
j
=
1
m
[
g
c
d
(
i
,
j
)
=
=
p
]
=
∑
p
∑
i
=
1
⌊
n
p
⌋
∑
j
=
1
⌊
m
p
⌋
[
g
c
d
(
i
,
j
)
=
=
1
]
=
∑
p
∑
i
=
1
⌊
n
p
⌋
∑
j
=
1
⌊
m
p
⌋
∑
k
∣
i
,
k
∣
j
μ
(
k
)
=
∑
p
∑
k
=
1
⌊
n
p
⌋
μ
(
k
)
⌊
n
k
p
⌋
⌊
m
k
p
⌋
ans = \sum\limits_{p}\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}[gcd(i,j)== p]=\sum\limits_{p}\sum\limits_{i=1}^{{\tiny \left\lfloor\dfrac{n}{p}\right\rfloor}}\sum\limits_{j=1}^{{\tiny \left\lfloor\dfrac{m}{p}\right\rfloor}}[gcd(i,j)==1]=\sum\limits_{p}\sum\limits_{i=1}^{{\tiny \left\lfloor\dfrac{n}{p}\right\rfloor}}\sum\limits_{j=1}^{{\tiny \left\lfloor\dfrac{m}{p}\right\rfloor}}\sum\limits_{k|i,k|j}\mu(k)=\sum\limits_{p}\sum\limits_{k=1}^{{\tiny \left\lfloor\dfrac{n}{p}\right\rfloor}}\mu(k){\left\lfloor\dfrac{n}{kp}\right\rfloor}{\left\lfloor\dfrac{m}{kp}\right\rfloor}
ans=p∑i=1∑nj=1∑m[gcd(i,j)==p]=p∑i=1∑⌊pn⌋j=1∑⌊pm⌋[gcd(i,j)==1]=p∑i=1∑⌊pn⌋j=1∑⌊pm⌋k∣i,k∣j∑μ(k)=p∑k=1∑⌊pn⌋μ(k)⌊kpn⌋⌊kpm⌋
操作来了!!
我们考虑枚举
T
=
k
p
T = kp
T=kp,于是有
a
n
s
=
∑
T
=
1
n
⌊
n
T
⌋
⌊
m
T
⌋
∑
p
∣
T
μ
(
T
p
)
ans = \sum\limits_{T=1}^{n}{\left\lfloor\dfrac{n}{T}\right\rfloor}{ \left\lfloor\dfrac{m}{T}\right\rfloor}\sum\limits_{p|T}\mu({\dfrac{T}{p}})
ans=T=1∑n⌊Tn⌋⌊Tm⌋p∣T∑μ(pT)
对于后面那部分,我们可以预处理,将每个素数的倍数
i
i
i 加上
μ
(
i
)
\mu(i)
μ(i),复杂度是
O
(
w
l
n
(
w
)
)
O(wln(w))
O(wln(w)),
w
w
w 为素数个数,而
w
w
w 大约是
n
l
n
(
n
)
\dfrac{n}{ln(n)}
ln(n)n,所以总的复杂度是
O
(
n
)
O(n)
O(n),单次询问的复杂度为
O
(
n
)
O(\sqrt{n})
O(n)