mobius inversion 莫比乌斯反演 例题总结

莫比乌斯反演 总结

莫比乌斯反演,实际上主要利用莫比乌斯函数的性质: ϵ = μ ∗ 1 \epsilon = \mu * 1 ϵ=μ1。一般会结合 整除分块线性筛 等算法进行考察。下面总结了几道 Luogu 上较为典型的题目。

每道题若涉及 n n n m m m 两个量,默认 n ≤ m n \le m nm

P3455 [POI2007] ZAP-Queries

T T T 组数据,对于每组数据,给定 n n n m m m d d d,求:

∑ i = 1 n ∑ j = 1 m [ gcd ⁡ ( i , j ) = d ] \sum_{i=1}^n \sum_{j=1}^m [\gcd(i, j) = d] i=1nj=1m[gcd(i,j)=d]

其中: 1 ≤ T ≤ 5 × 1 0 4 1 \le T \le 5 \times 10^4 1T5×104 1 ≤ d ≤ n , m ≤ 5 × 1 0 4 1 \le d \le n, m \le 5 \times 10^4 1dn,m5×104

∑ i = 1 n ∑ j = 1 m [ gcd ⁡ ( i , j ) = d ] = ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ [ gcd ⁡ ( i , j ) = 1 ] = ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ ∑ k ∣ gcd ⁡ ( i , j ) μ ( k ) = ∑ k = 1 ⌊ n d ⌋ μ ( k ) ⌊ n d k ⌋ ⌊ m d k ⌋ \begin{aligned} &\sum_{i=1}^n \sum_{j=1}^m [\gcd(i, j) = d]\\ =&\sum_{i=1}^{\lfloor\frac nd\rfloor}\sum_{j=1}^{\lfloor\frac md\rfloor}[\gcd(i, j) = 1]\\ =&\sum_{i=1}^{\lfloor\frac nd\rfloor}\sum_{j=1}^{\lfloor\frac md\rfloor}\sum_{k \mid \gcd(i, j)} \mu(k)\\ =&\sum_{k=1}^{\lfloor\frac nd\rfloor}\mu(k)\lfloor\frac n{dk} \rfloor \lfloor\frac m{dk} \rfloor \end{aligned} ===i=1nj=1m[gcd(i,j)=d]i=1dnj=1dm[gcd(i,j)=1]i=1dnj=1dmkgcd(i,j)μ(k)k=1dnμ(k)dkndkm

μ ( k ) \mu(k) μ(k) 用前缀和预处理,查询用整除分块算。

P1390 公约数的和

给定 n n n,求:

∑ i = 1 n ∑ j = i + 1 n gcd ⁡ ( i , j ) \sum_{i = 1}^n \sum_{j = i + 1}^n \gcd(i, j) i=1nj=i+1ngcd(i,j)

其中: 2 ≤ n ≤ 2 × 1 0 6 2 \leq n \leq 2 \times 10^6 2n2×106

原式可以看成:

∑ i = 1 n ∑ i = 1 n gcd ⁡ ( i , j ) − n ( n + 1 ) 2 2 \frac{\sum_{i=1}^n \sum_{i=1}^n \gcd(i, j) -\frac{n(n+1)}2}2 2i=1ni=1ngcd(i,j)2n(n+1)

注意到:

∑ i = 1 n ∑ j = 1 n gcd ⁡ ( i , j ) = ∑ d = 1 n d ∑ i = 1 n ∑ j = 1 n [ gcd ⁡ ( i , j ) = 1 ] = ∑ d = 1 n d ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ n d ⌋ ∑ k ∣ gcd ⁡ ( i , j ) μ ( k ) = ∑ d = 1 n d ∑ k = 1 ⌊ n d ⌋ μ ( k ) ⌊ n d k ⌋ 2 = ∑ x = 1 n ∑ d ∣ x d μ ( x d ) ⌊ n x ⌋ 2 = ∑ x = 1 n φ ( x ) ⌊ n x ⌋ 2 \begin{aligned} &\sum_{i=1}^n\sum_{j=1}^n\gcd(i, j)\\ =&\sum_{d=1}^n d \sum_{i=1}^n \sum_{j=1}^n [\gcd(i, j) = 1]\\ =&\sum_{d=1}^n d \sum_{i=1}^{\lfloor \frac nd \rfloor}\sum_{j=1}^{\lfloor \frac nd \rfloor} \sum_{k \mid \gcd(i, j)} \mu(k)\\ =&\sum_{d=1}^n d \sum_{k=1}^{\lfloor \frac nd \rfloor} \mu(k) \lfloor \frac n {dk} \rfloor ^2\\ =&\sum_{x=1}^n\sum_{d \mid x} d\mu(\frac xd) \lfloor\frac nx \rfloor^2\\ =&\sum_{x=1}^n\varphi(x)\lfloor\frac nx \rfloor^2 \end{aligned} =====i=1nj=1ngcd(i,j)d=1ndi=1nj=1n[gcd(i,j)=1]d=1ndi=1dnj=1dnkgcd(i,j)μ(k)d=1ndk=1dnμ(k)dkn2x=1ndxdμ(dx)xn2x=1nφ(x)xn2

前缀和+数论分块,绰绰有余。

上面用到了欧拉函数的一个重要结论: i d ∗ μ = φ ∗ 1 ∗ μ = φ ∗ ϵ = φ id * \mu = \varphi * 1 * \mu = \varphi * \epsilon = \varphi idμ=φ1μ=φϵ=φ

双倍经验:P1447 [NOI2010] 能量采集

提示:

Years of struggling in OI, without long long, everything turns out to be a null.
Please remember to use 1ll* to avoid Wrong Answer!

P1829 [国家集训队] Crash的数字表格 / JZPTAB

给定 n n n m m m,求:

∑ i = 1 n ∑ j = 1 m lcm ⁡ ( i , j )   m o d   20101009 \sum_{i=1}^n \sum_{j=1}^m \operatorname{lcm}(i,j) \bmod 20101009 i=1nj=1mlcm(i,j)mod20101009

其中: 1 ≤ n , m ≤ 1 0 7 1 \le n, m \le 10^7 1n,m107

∑ i = 1 n ∑ j = 1 m lcm ⁡ ( i , j ) = ∑ d = 1 n ∑ i = 1 n ∑ j = 1 m i j d [ gcd ⁡ ( i , j ) = d ] = ∑ d = 1 n ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ i j d [ gcd ⁡ ( i , j ) = 1 ] = ∑ d = 1 n d ∑ i = 1 ⌊ n d ⌋ i ∑ j = 1 ⌊ m d ⌋ j ∑ k ∣ gcd ⁡ ( i , j ) μ ( k ) = ∑ d = 1 n d ∑ k = 1 ⌊ n d ⌋ μ ( k ) ∑ i = 1 ⌊ n k d ⌋ i k ∑ j = 1 ⌊ m k d ⌋ j k = ∑ d = 1 n d ∑ k = 1 ⌊ n d ⌋ k 2 μ ( k ) ∑ i = 1 ⌊ n k d ⌋ i ∑ j = 1 ⌊ m k d ⌋ j = ∑ x = 1 n ∑ k ∣ x x k ⋅ k 2 μ ( k ) ∑ i = 1 ⌊ n x ⌋ i ∑ j = 1 ⌊ m x ⌋ j = ∑ x = 1 n x ∑ k ∣ x k μ ( k ) ∑ i = 1 ⌊ n x ⌋ i ∑ j = 1 ⌊ m x ⌋ j \begin{aligned} &\sum_{i=1}^n \sum_{j=1}^m \operatorname{lcm}(i, j)\\ =&\sum_{d=1}^n \sum_{i=1}^n \sum_{j=1}^m \frac{ij}d [\gcd(i, j) = d]\\ =&\sum_{d=1}^n \sum_{i=1}^{\lfloor \frac nd \rfloor} \sum_{j=1}^{\lfloor \frac md \rfloor} ijd [\gcd(i, j) = 1]\\ =&\sum_{d=1}^n d\sum_{i=1}^{\lfloor \frac nd \rfloor} i\sum_{j=1}^{\lfloor \frac md \rfloor} j \sum_{k\mid \gcd(i, j)} \mu(k)\\ =&\sum_{d=1}^n d \sum_{k=1}^{\lfloor\frac nd\rfloor} \mu(k) \sum_{i=1}^{\lfloor\frac n{kd}\rfloor} ik \sum_{j=1}^{\lfloor\frac m{kd}\rfloor} jk\\ =&\sum_{d=1}^n d \sum_{k=1}^{\lfloor\frac nd\rfloor} k^2\mu(k) \sum_{i=1}^{\lfloor\frac n{kd}\rfloor} i \sum_{j=1}^{\lfloor\frac m{kd}\rfloor} j\\ =&\sum_{x=1}^n\sum_{k\mid x}\frac xk \cdot k^2\mu(k) \sum_{i=1}^{\lfloor \frac nx \rfloor} i \sum_{j=1}^{\lfloor \frac mx \rfloor} j\\ =&\sum_{x=1}^nx\sum_{k \mid x} k\mu(k) \sum_{i=1}^{\lfloor \frac nx \rfloor} i \sum_{j=1}^{\lfloor \frac mx \rfloor} j \end{aligned} =======i=1nj=1mlcm(i,j)d=1ni=1nj=1mdij[gcd(i,j)=d]d=1ni=1dnj=1dmijd[gcd(i,j)=1]d=1ndi=1dnij=1dmjkgcd(i,j)μ(k)d=1ndk=1dnμ(k)i=1kdnikj=1kdmjkd=1ndk=1dnk2μ(k)i=1kdnij=1kdmjx=1nkxkxk2μ(k)i=1xnij=1xmjx=1nxkxkμ(k)i=1xnij=1xmj

x = k d x = kd x=kd 也是典型的套路啦。

∑ i = 1 ⌊ n x ⌋ i \sum_{i=1}^{\lfloor \frac nx \rfloor} i i=1xni 是等差数列求和,确定 ⌊ n x ⌋ \lfloor\frac nx\rfloor xn 之后, O ( 1 ) O(1) O(1) 可求。

f ( n ) = ∑ d ∣ n d μ ( d ) f(n) = \sum _{d \mid n}d\mu(d) f(n)=dndμ(d) 是个积性函数,线性筛 O ( n ) O(n) O(n) 可求。(只要是积性函数,都能够线性筛。)

可参考线性筛 ∑ d ∣ n d φ ( d ) \sum_{d \mid n} d\varphi(d) dndφ(d) 的文章。

先证明 f ( n ) f(n) f(n) 是积性函数:

g ( n ) = i d ( n ) μ ( n ) g(n) = id(n)\mu(n) g(n)=id(n)μ(n),注意到 i d ( n ) id(n) id(n) μ ( n ) \mu(n) μ(n) 都是积性函数,那么 g ( n ) g(n) g(n) 是积性函数。
f = 1 ∗ g f = 1 * g f=1g,根据狄利克雷卷积, f ( n ) f(n) f(n) 也是积性函数。

再考虑线性筛的方法:

f ( n ) = ∑ d ∣ n d μ ( d ) f(n) = \sum _{d \mid n}d\mu(d) f(n)=dndμ(d)

p p p 为质数。

显然, f ( 1 ) = 1 f(1) = 1 f(1)=1 f ( p ) = 1 − p f(p) = 1 - p f(p)=1p

p ∤ i p \nmid i pi,显然 f ( i p ) = f ( i ) f ( p ) f(ip) = f(i)f(p) f(ip)=f(i)f(p)

p ∣ i p \mid i pi f ( i p ) = f ( i ) f(ip) = f(i) f(ip)=f(i)。因为 i p ip ip 的因数中,比 i i i 多的那一部分 d d d,一定含有平方因子。而根据莫比乌斯函数的定义,含有平方因子会导致 μ ( d ) = 0 \mu(d) = 0 μ(d)=0,从而这一部分对结果没有影响。

以上算法 预处理 O ( n ) O(n) O(n),单次询问 O ( n ) O(\sqrt n) O(n ),可以处理多测的情况。

P3327 [SDOI2015] 约数个数和

T T T 组数据,对于每组数据,给定 n , m n,m n,m,求:

∑ i = 1 n ∑ j = 1 m τ ( i j ) \sum_{i=1}^n\sum_{j=1}^m\tau(ij) i=1nj=1mτ(ij)

其中 1 ≤ T , n , m ≤ 5 × 1 0 4 1\le T,n,m \le 5 \times 10^4 1T,n,m5×104 τ ( n ) \tau(n) τ(n) 表示 n n n 的约数个数。

遇到陌生函数,不要惊慌,把它转化成熟悉的函数。

引理 τ ( i j ) = ∑ x ∣ i ∑ y ∣ j [ gcd ⁡ ( x , y ) = 1 ] \tau(ij) = \sum_{x \mid i} \sum_{y \mid j} [\gcd(x, y) = 1] τ(ij)=xiyj[gcd(x,y)=1]

证明

先复习一个小学生都会的知识:

n = ∏ p i c i n = \prod p_i^{c_i} n=pici,则 τ ( n ) = ∏ ( c i + 1 ) \tau(n) = \prod (c_i + 1) τ(n)=(ci+1)

不难发现,上述结论意味着 τ ( n ) \tau(n) τ(n) 是积性函数。所以我们可以分别考虑 i i i j j j 的每一个质因子 p p p

假设 i i i j j j 含有的质因子 p p p 的个数分别为 α \alpha α β \beta β

( α + β + 1 ) (\alpha + \beta + 1) (α+β+1) 可以看成前 α \alpha α 个从 i i i 中取,后 β \beta β 个从 j j j 中取,还有一个就是两边都不取。

那么就不会出现两边都取到质因子 p p p 的情况,从而得到 τ ( i j ) = ∑ x ∣ i ∑ y ∣ j [ gcd ⁡ ( x , y ) = 1 ] \tau(ij) = \sum_{x \mid i} \sum_{y \mid j} [\gcd(x, y) = 1] τ(ij)=xiyj[gcd(x,y)=1]

现在可以直接推式子了。

∑ i = 1 n ∑ j = 1 m τ ( i j ) = ∑ i = 1 n ∑ j = 1 m ∑ x ∣ i ∑ y ∣ j [ gcd ⁡ ( x , y ) = 1 ] = ∑ x = 1 n ⌊ n x ⌋ ∑ y = 1 m ⌊ m y ⌋ [ gcd ⁡ ( x , y ) = 1 ] = ∑ x = 1 n ⌊ n x ⌋ ∑ y = 1 m ⌊ m y ⌋ ∑ d ∣ gcd ⁡ ( x , y ) μ ( d ) = ∑ d = 1 n μ ( d ) ∑ i = 1 ⌊ n d ⌋ ⌊ n i d ⌋ ∑ j = 1 ⌊ m d ⌋ ⌊ m j d ⌋ \begin{aligned} &\sum_{i=1}^n \sum_{j=1}^m \tau(ij)\\ =&\sum_{i=1}^n \sum_{j=1}^m \sum_{x \mid i} \sum_{y \mid j} [\gcd(x, y) = 1]\\ =&\sum_{x=1}^n \lfloor\frac nx\rfloor \sum_{y=1}^m \lfloor \frac my \rfloor[\gcd(x, y) = 1]\\ =&\sum_{x=1}^n \lfloor\frac nx\rfloor \sum_{y=1}^m \lfloor \frac my \rfloor \sum_{d\mid\gcd(x, y)} \mu(d)\\ =&\sum_{d=1}^n\mu(d) \sum_{i=1}^{\lfloor \frac nd \rfloor}\lfloor \frac{n}{id}\rfloor \sum_{j=1}^{\lfloor \frac md \rfloor}\lfloor \frac{m}{jd}\rfloor \end{aligned} ====i=1nj=1mτ(ij)i=1nj=1mxiyj[gcd(x,y)=1]x=1nxny=1mym[gcd(x,y)=1]x=1nxny=1mymdgcd(x,y)μ(d)d=1nμ(d)i=1dnidnj=1dmjdm

f ( n ) = ∑ i = 1 n ⌊ n i ⌋ f(n) = \sum_{i=1}^n \lfloor \frac ni \rfloor f(n)=i=1nin,则原式等于:

∑ d = 1 n μ ( d ) f ( ⌊ n d ⌋ ) f ( ⌊ m d ⌋ ) \sum_{d=1}^n \mu(d) f(\lfloor \frac nd \rfloor) f(\lfloor \frac md \rfloor) d=1nμ(d)f(⌊dn⌋)f(⌊dm⌋)

预处理 μ ( d ) \mu(d) μ(d) 的前缀和,对上述式子进行 O ( n ) O(\sqrt n) O(n ) 的整除分块,计算 f ( n ) f(n) f(n) 的时候注意进行记忆化(不记忆化会TLE)。总时间复杂度 O ( n n + T n ) O(n\sqrt n + T\sqrt n) O(nn +Tn )

P6810 「MCOI-02」Convex Hull 凸包

本题标题有误导性,怀疑比赛计算几何题被毙掉后,换了莫比乌斯反演题,但标题没有换?

给定 n n n m m m p p p,求:

∑ i = 1 n ∑ j = 1 m τ ( i ) τ ( j ) τ ( gcd ⁡ ( i , j ) )   m o d   p \sum_{i=1}^n\sum_{j=1}^m \tau(i)\tau(j)\tau(\gcd(i,j)) \bmod p i=1nj=1mτ(i)τ(j)τ(gcd(i,j))modp

其中: 1 ≤ n , m ≤ 2 × 1 0 6 1 \le n, m \le 2 \times 10^6 1n,m2×106 1 ≤ p ≤ 1 0 9 1 \le p \le 10^9 1p109

∑ i = 1 n ∑ j = 1 m τ ( i ) τ ( j ) τ ( gcd ⁡ ( i , j ) ) = ∑ d = 1 n τ ( d ) ∑ i = 1 n τ ( i ) ∑ j = 1 m τ ( j ) [ gcd ⁡ ( i , j ) = d ] = ∑ d = 1 n τ ( d ) ∑ i = 1 ⌊ n d ⌋ τ ( i d ) ∑ j = 1 ⌊ m d ⌋ τ ( j d ) [ gcd ⁡ ( i , j ) = 1 ] = ∑ d = 1 n τ ( d ) ∑ i = 1 ⌊ n d ⌋ τ ( i d ) ∑ j = 1 ⌊ m d ⌋ τ ( j d ) ∑ k ∣ gcd ⁡ ( i , j ) μ ( k ) = ∑ d = 1 n τ ( d ) ∑ k = 1 ⌊ n d ⌋ μ ( k ) ∑ i = 1 ⌊ n d k ⌋ τ ( i d k ) ∑ j = 1 ⌊ m d k ⌋ τ ( j d k ) = ∑ x = 1 n ∑ d ∣ x τ ( d ) μ ( x d ) ∑ i = 1 ⌊ n x ⌋ τ ( i x ) ∑ j = 1 ⌊ m x ⌋ τ ( j x ) = ∑ x = 1 n ∑ i = 1 ⌊ n x ⌋ τ ( i x ) ∑ j = 1 ⌊ m x ⌋ τ ( j x ) \begin{aligned} &\sum_{i=1}^n\sum_{j=1}^m \tau(i)\tau(j)\tau(\gcd(i,j))\\ =&\sum_{d=1}^n \tau(d) \sum_{i=1}^n\tau(i) \sum_{j=1}^m\tau(j)[\gcd(i, j) = d]\\ =&\sum_{d=1}^n \tau(d) \sum_{i=1}^{\lfloor\frac nd\rfloor}\tau(id) \sum_{j=1}^{\lfloor \frac md \rfloor} \tau(jd)[\gcd(i, j) = 1]\\ =&\sum_{d=1}^n \tau(d) \sum_{i=1}^{\lfloor\frac nd\rfloor}\tau(id) \sum_{j=1}^{\lfloor \frac md \rfloor} \tau(jd)\sum_{k \mid \gcd(i, j)} \mu(k)\\ =&\sum_{d=1}^n \tau(d) \sum_{k=1}^{\lfloor\frac nd\rfloor} \mu(k) \sum_{i=1}^{\lfloor\frac n{dk}\rfloor}\tau(idk) \sum_{j=1}^{\lfloor \frac m{dk} \rfloor} \tau(jdk)\\ =&\sum_{x=1}^n\sum_{d \mid x} \tau(d) \mu(\frac xd)\sum_{i=1}^{\lfloor\frac nx\rfloor}\tau(ix) \sum_{j=1}^{\lfloor \frac mx \rfloor} \tau(jx)\\ =&\sum_{x=1}^n\sum_{i=1}^{\lfloor\frac nx\rfloor}\tau(ix) \sum_{j=1}^{\lfloor \frac mx \rfloor} \tau(jx) \end{aligned} ======i=1nj=1mτ(i)τ(j)τ(gcd(i,j))d=1nτ(d)i=1nτ(i)j=1mτ(j)[gcd(i,j)=d]d=1nτ(d)i=1dnτ(id)j=1dmτ(jd)[gcd(i,j)=1]d=1nτ(d)i=1dnτ(id)j=1dmτ(jd)kgcd(i,j)μ(k)d=1nτ(d)k=1dnμ(k)i=1dknτ(idk)j=1dkmτ(jdk)x=1ndxτ(d)μ(dx)i=1xnτ(ix)j=1xmτ(jx)x=1ni=1xnτ(ix)j=1xmτ(jx)

最后一步解释:因为 τ = 1 ∗ 1 \tau = 1 * 1 τ=11 μ ∗ 1 = ϵ \mu * 1 = \epsilon μ1=ϵ,所以 τ ∗ μ = 1 ∗ 1 ∗ μ = 1 ∗ ϵ = 1 \tau * \mu = 1 * 1 * \mu = 1 * \epsilon = 1 τμ=11μ=1ϵ=1。其他都是典型的套路。

这里可以线性筛预处理 τ \tau τ 函数的值,外层数论分块后内层暴力处理。

τ ( 1 ) = 1 \tau(1) = 1 τ(1)=1 τ ( p ) = 2 \tau(p) = 2 τ(p)=2。对于质数 p ∣ i p \mid i pi τ ( p i ) = 2 τ ( i ) − τ ( i / p ) \tau(pi) = 2\tau(i) - \tau(i/p) τ(pi)=2τ(i)τ(i/p)

总结

归纳总结做莫比乌斯反演的题目的方法:

  1. 把陌生的式子化成熟悉的式子,直到可以拿起式子计算。
  2. 牢记、抓住 μ ∗ 1 = ϵ \mu * 1 = \epsilon μ1=ϵ,将 [ gcd ⁡ ( i , j ) = 1 ] [\gcd(i, j) = 1] [gcd(i,j)=1] 化为 ∑ k ∣ gcd ⁡ ( i , j ) μ ( k ) \sum_{k \mid \gcd(i, j)} \mu(k) kgcd(i,j)μ(k)
  3. P1390 P1829 P6810 三题,都用了 x = k d x = kd x=kd 的套路,套了之后可以出现卷积。
  4. 掌握线性筛的方法,学会推出陌生积性函数的线性筛公式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值