莫比乌斯反演专题题解
「SDOI2015」约数个数和
这个题关键就在于知不知道这个
d
i
j
=
∑
x
∣
i
∑
y
∣
j
(
g
c
d
(
x
,
y
)
=
=
1
)
d_{ij}=\sum_{x|i}\sum_{y|j}(gcd(x,y)==1)
dij=∑x∣i∑y∣j(gcd(x,y)==1) 结论 以及
g
c
d
=
=
1
gcd==1
gcd==1 怎么用莫比乌斯函数表示的 然后推式子就很愉快了
a
n
s
=
∑
i
=
1
n
∑
j
=
1
m
d
i
j
=
∑
i
=
1
n
∑
j
=
1
m
∑
x
∣
i
∑
y
∣
j
(
g
c
d
(
x
,
y
)
=
=
1
)
=
∑
i
=
1
n
∑
j
=
1
m
∑
x
∣
i
∑
y
∣
j
∑
d
∣
g
c
d
(
x
,
y
)
u
d
=
∑
d
=
1
m
i
n
(
n
,
m
)
u
d
∑
d
∣
x
∑
d
∣
y
∑
i
∣
x
∑
j
∣
y
=
∑
d
=
1
m
i
n
(
n
,
m
)
u
d
∑
d
∣
x
∑
d
∣
y
⌊
n
x
⌋
⌊
m
y
⌋
g
(
k
)
=
∑
i
=
1
k
⌊
k
i
⌋
a
n
s
=
∑
d
=
1
m
i
n
(
n
,
m
)
u
d
×
g
(
⌊
n
d
⌋
)
×
g
(
⌊
m
d
⌋
)
\begin{aligned} ans &=\sum_{i=1}^{n}\sum_{j=1}^{m}d_{ij}\\ &=\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{x|i}\sum_{y|j}(gcd(x,y)==1)\\ &=\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{x|i}\sum_{y|j}\sum_{d|gcd(x,y)}u_d\\ &=\sum_{d=1}^{min(n,m)}u_d\sum_{d|x}\sum_{d|y}\sum_{i|x}\sum_{j|y}\\ &=\sum_{d=1}^{min(n,m)}u_d\sum_{d|x}\sum_{d|y} \lfloor \cfrac{n}{x} \rfloor\lfloor \cfrac{m}{y } \rfloor \\ \end{aligned} \\ g(k)=\sum_{i=1}^{k}\lfloor\cfrac{k}{i}\rfloor \\ ans=\sum_{d=1}^{min(n,m)}u_d\times g(\lfloor\cfrac{n}{d}\rfloor )\times g(\lfloor\cfrac{m}{d}\rfloor )
ans=i=1∑nj=1∑mdij=i=1∑nj=1∑mx∣i∑y∣j∑(gcd(x,y)==1)=i=1∑nj=1∑mx∣i∑y∣j∑d∣gcd(x,y)∑ud=d=1∑min(n,m)udd∣x∑d∣y∑i∣x∑j∣y∑=d=1∑min(n,m)udd∣x∑d∣y∑⌊xn⌋⌊ym⌋g(k)=i=1∑k⌊ik⌋ans=d=1∑min(n,m)ud×g(⌊dn⌋)×g(⌊dm⌋)
g
(
x
)
g(x)
g(x) 函数用整出分块预处理 然后就是套路了
「SDOI2017」数字表格
a n s = ∏ i = 1 n ∏ j = 1 m f i b [ g c d ( i , j ) ] = ∏ t = 1 n f i b [ t ] ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = = 1 ] = ∏ t = 1 n f i b [ t ] ∑ t ∣ d n u ( d t ) ⌊ n d ⌋ ⌊ m d ⌋ = ∏ t = 1 n ∏ t ∣ d f i b [ t ] u ( d t ) ⌊ n d ⌋ ⌊ m d ⌋ = ∏ d = 1 n ∏ t ∣ d f i b [ t ] u ( d t ) ⌊ n d ⌋ ⌊ m d ⌋ = ∏ d = 1 n [ ∏ t ∣ d f i b [ t ] u ( d t ) ] ⌊ n d ⌋ ⌊ m d ⌋ \begin{aligned} ans &=\prod_{i=1}^{n} \prod_{j=1}^{m}fib[gcd(i,j)] \\ &=\prod_{t=1}^{n}fib[t]^{\sum_{i=1}^{n} \sum_{j=1}^{m}[gcd(i,j)==1]} \\ &=\prod_{t=1}^{n} fib[t]^{\sum_{t|d}^{n} u(\frac{d}{t}) \lfloor\frac{n}{d}\rfloor \lfloor\frac{m}{d}\rfloor } \\ &=\prod_{t=1}^{n} \prod_{t|d} fib[t]^{u(\frac{d}{t}) \lfloor\frac{n}{d}\rfloor \lfloor\frac{m}{d}\rfloor } \\ &=\prod_{d=1}^{n} \prod_{t|d} fib[t]^{u(\frac{d}{t})\lfloor\frac{n}{d}\rfloor \lfloor\frac{m}{d}\rfloor } \\ &=\prod_{d=1}^{n} {[\prod_{t|d} fib[t]^{u(\frac{d}{t})}]}^{\lfloor\frac{n}{d}\rfloor \lfloor\frac{m}{d}\rfloor } \end{aligned} \\ ans=i=1∏nj=1∏mfib[gcd(i,j)]=t=1∏nfib[t]∑i=1n∑j=1m[gcd(i,j)==1]=t=1∏nfib[t]∑t∣dnu(td)⌊dn⌋⌊dm⌋=t=1∏nt∣d∏fib[t]u(td)⌊dn⌋⌊dm⌋=d=1∏nt∣d∏fib[t]u(td)⌊dn⌋⌊dm⌋=d=1∏n[t∣d∏fib[t]u(td)]⌊dn⌋⌊dm⌋
这道题的关键是是第三步到第四步 推式子的过程中交换枚举顺序很重要
然后反演里面整除分块的式子常用但不限于四则运算
时间 O ( T n + n log n ) O(T\sqrt{n}+n\log n) O(Tn+nlogn)
嗷嗷对了 还有对函数 g ( d ) = ∏ t ∣ d f i b [ t ] u ( d t ) g(d)=\prod_{t|d} fib[t]^{u(\frac{d}{t})} g(d)=∏t∣dfib[t]u(td) 的预处理可以用埃式筛来做
「BZOJ2693」jzptab
a n s = ∑ i = 1 n ∑ j = 1 m l c m ( i , j ) = ∑ i = 1 n ∑ j = 1 m i × j g c d ( i , j ) = ∑ t = 1 n t ∑ i = 1 i t ≤ n ∑ j = 1 j t ≤ m i × j × ( g c d ( i , j ) = = 1 ) = ∑ t = 1 n t ∑ i = 1 i t ≤ n ∑ j = 1 j t ≤ m i × j × ∑ d ∣ i d ∣ j u d = ∑ d = 1 n u d × d 2 ∑ t d ≤ n t ∑ i d t ≤ n ∑ j d t ≤ m i × j \begin{aligned} ans&=\sum_{i=1}^n \sum_{j=1}^m lcm(i,j) \\ &=\sum_{i=1}^n \sum_{j=1}^m\cfrac{i\times j}{gcd(i,j)} \\ &=\sum_{t=1}^n t\sum_{i=1}^{it\leq n}\sum_{j=1}^{jt\leq m}i\times j \times (gcd(i,j)==1) \\ &=\sum_{t=1}^n t\sum_{i=1}^{it\leq n}\sum_{j=1}^{jt\leq m}i\times j \times \sum_{d|i\ d|j}u_d \\ &=\sum_{d=1}^{n}u_d \times d^2 \sum_{td \le n}t\sum_{idt\le n}^{}\sum_{jdt\le m}^{}i\times j \\ \end{aligned} ans=i=1∑nj=1∑mlcm(i,j)=i=1∑nj=1∑mgcd(i,j)i×j=t=1∑nti=1∑it≤nj=1∑jt≤mi×j×(gcd(i,j)==1)=t=1∑nti=1∑it≤nj=1∑jt≤mi×j×d∣i d∣j∑ud=d=1∑nud×d2td≤n∑tidt≤n∑jdt≤m∑i×j
令
x
=
⌊
n
d
⌋
,
y
=
⌊
m
d
⌋
,
s
u
m
(
a
,
b
)
=
∑
i
=
1
a
∑
j
=
1
b
i
×
j
x=\lfloor \cfrac{n}{d} \rfloor,y=\lfloor \cfrac{m}{d} \rfloor,sum(a,b)=\sum_{i=1}^a\sum_{j=1}^bi\times j
x=⌊dn⌋,y=⌊dm⌋,sum(a,b)=∑i=1a∑j=1bi×j
a
n
s
=
∑
d
=
1
n
u
d
×
d
2
∑
t
=
1
x
t
∑
i
t
≤
x
∑
j
t
≤
y
i
×
j
=
∑
d
=
1
n
u
d
×
d
2
∑
t
=
1
x
t
×
s
u
m
(
⌊
x
t
⌋
,
⌊
y
t
⌋
)
=
∑
d
=
1
n
∑
t
=
1
m
i
n
(
x
,
y
)
u
d
×
d
2
×
t
×
s
u
m
(
⌊
n
d
t
⌋
,
⌊
m
d
t
⌋
)
①
=
∑
t
=
1
n
∑
d
t
≤
n
u
d
×
d
2
×
t
×
s
u
m
(
⌊
n
d
t
⌋
,
⌊
m
d
t
⌋
)
=
∑
d
t
≤
n
s
u
m
(
⌊
n
d
t
⌋
,
⌊
m
d
t
⌋
)
∑
t
∣
d
t
u
d
×
d
2
×
t
②
\begin{aligned} ans&=\sum_{d=1}^{n}u_d \times d^2 \sum_{t=1}^xt\sum_{it\le x}^{}\sum_{jt\le y}^{}i\times j \\ &=\sum_{d=1}^{n}u_d \times d^2 \sum_{t=1}^xt\times sum(\lfloor\cfrac{x}{t}\rfloor,\lfloor\cfrac{y}{t}\rfloor) \\ &=\sum_{d=1}^{n}\sum_{t=1}^{min(x,y)} u_d \times d^2\times t\times sum(\lfloor\cfrac{n} {dt}\rfloor,\lfloor\cfrac{m}{dt}\rfloor)\ \ \ \ \ \ \ \ ① \\ &=\sum_{t=1}^{n}\sum_{dt\le n}^{} u_d \times d^2\times t\times sum(\lfloor\cfrac{n} {dt}\rfloor,\lfloor\cfrac{m}{dt}\rfloor) \\ &=\sum_{dt\le n}sum(\lfloor\cfrac{n} {dt}\rfloor,\lfloor\cfrac{m}{dt}\rfloor)\sum_{t|dt}^{} u_d \times d^2\times t\ \ \ \ \ \ \ \ ② \end{aligned}
ans=d=1∑nud×d2t=1∑xtit≤x∑jt≤y∑i×j=d=1∑nud×d2t=1∑xt×sum(⌊tx⌋,⌊ty⌋)=d=1∑nt=1∑min(x,y)ud×d2×t×sum(⌊dtn⌋,⌊dtm⌋) ①=t=1∑ndt≤n∑ud×d2×t×sum(⌊dtn⌋,⌊dtm⌋)=dt≤n∑sum(⌊dtn⌋,⌊dtm⌋)t∣dt∑ud×d2×t ②
最后化成可以整除分块的式子的时候 如果有函数不能直接求或者预处理 要把该函数提到最前面 如这道题 ① -> ②这个过程
数字表格那道题同样也用了这个技巧
「SDOI2014」数表
a n s = ∑ i = 1 n ∑ j = 1 m g ( g c d ( i , j ) ) = ∑ t = 1 n g ( t ) ∑ i = 1 i t ≤ n ∑ j = 1 j t ≤ m ( g c d ( i , j ) = = 1 ) = ∑ t = 1 n g ( t ) ∑ i = 1 ⌊ n t ⌋ u i ⌊ n i t ⌋ ⌊ m i t ⌋ = ∑ t = 1 n ∑ i = 1 ⌊ n t ⌋ g ( t ) u i ⌊ n i t ⌋ ⌊ m i t ⌋ = ∑ i t = 1 n ∑ t ∣ i t g ( t ) u i ⌊ n i t ⌋ ⌊ m i t ⌋ = ∑ i t = 1 n ⌊ n i t ⌋ ⌊ m i t ⌋ ∑ t ∣ i t g ( t ) u i t t \begin{aligned} ans&=\sum_{i=1}^{n} \sum_{j=1}^{m} g(gcd(i,j)) \\ &=\sum_{t=1}^{n}g(t) \sum_{i=1}^{it \le n} \sum_{j=1}^{jt\le m}(gcd(i,j)==1) \\ &=\sum_{t=1}^{n}g(t) \sum_{i=1}^{ \lfloor \frac{n}{t} \rfloor }u_i \lfloor \frac{n}{it} \rfloor \lfloor \frac{m}{it} \rfloor \\ &=\sum_{t=1}^{n} \sum_{i=1}^{ \lfloor \frac{n}{t} \rfloor }g(t)u_i \lfloor \frac{n}{it} \rfloor \lfloor \frac{m}{it} \rfloor \\ &=\sum_{it=1}^{n} \sum_{t|it}^{} g(t)u_i \lfloor \frac{n}{it} \rfloor \lfloor \frac{m}{it} \rfloor \\ &=\sum_{it=1}^{n} \lfloor \frac{n}{it} \rfloor \lfloor \frac{m}{it} \rfloor \sum_{t|it}^{} g(t)u_{\frac{it}{t}} \end{aligned} ans=i=1∑nj=1∑mg(gcd(i,j))=t=1∑ng(t)i=1∑it≤nj=1∑jt≤m(gcd(i,j)==1)=t=1∑ng(t)i=1∑⌊tn⌋ui⌊itn⌋⌊itm⌋=t=1∑ni=1∑⌊tn⌋g(t)ui⌊itn⌋⌊itm⌋=it=1∑nt∣it∑g(t)ui⌊itn⌋⌊itm⌋=it=1∑n⌊itn⌋⌊itm⌋t∣it∑g(t)utit
离线有点麻烦 常数有点卡(指卡我的常数