从零开始的莫比乌斯反演(函数)[详细推导]

也许更好的阅读体验

文章目录

  • $前置技能$
      • $狄利克雷卷积$
      • $几个定理$
  • $莫比乌斯函数$
  • $如何推导$
      • $如何求逆$
      • $莫比乌斯函数$
  • $莫比乌斯函数的性质$
  • $线性筛$
  • $莫比乌斯反演$
      • $证明$
      • $证明$
  • $莫比乌斯反演的应用$


前 置 技 能 前置技能

  • 学会莫比乌斯函数必须要先知道狄利克雷函数
  • 以及什么是逆元(一本正经胡说八道)

    狄 利 克 雷 卷 积 狄利克雷卷积

    两个函数 f , g f,g f,g
    f ∗ g ( n ) = ∑ d ∣ n n f ( d ) g ( n d ) \begin{aligned}f*g(n)=\sum_{d|n}^nf(d)g(\frac{n}{d})\end{aligned} fg(n)=dnnf(d)g(dn)
    • 性质
      交换律                   f ∗ g = g ∗ f \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ f*g=g*f                  fg=gf
      结合律                   f ∗ g ∗ h = f ∗ ( g ∗ h ) \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ f*g*h=f*(g*h)                  fgh=f(gh)
      加法分配率            f ∗ ( g + h ) = f ∗ g + f ∗ h \ \ \ \ \ \ \ \ \ \ f*(g+h)=f*g+f*h           f(g+h)=fg+fh

    几 个 定 理 几个定理

    • ① 定义单位函数 I ( n ) = [ n = 1 ] I(n)=[n=1] I(n)=[n=1],即只有当n等于1时, I ( 1 ) = 1 I(1)=1 I(1)=1,其余情况都为0
    • ②  I ∗ f = f I * f=f If=f
    • ③ 已知函数 f f f,定义函数 f f f的逆 f − 1 f^{-1} f1,满足 f ∗ f − 1 = I f * f^{-1}=I ff1=I,且积性函数的逆也是积性函数
    • ④ 由③可得 f − 1 ( 1 ) = 1 f ( 1 ) f^{-1}(1)=\frac{1}{f(1)} f1(1)=f(1)1
    • ⑤ 令 ξ ( n ) = 1 \xi(n)=1 ξ(n)=1

莫 比 乌 斯 函 数 莫比乌斯函数

  • 莫比乌斯函数 μ = ξ − 1 \mu=\xi^{-1} μ=ξ1
    n = p 1 a 1 ⋅ p 2 a 2 ⋅ ⋯ p k a k n=p_1^{a_1}·p_2^{a_2}·\cdots p_k^{a_k} n=p1a1p2a2pkak
    μ ( n ) = ξ − 1 ( n ) = { 1 n = 1 ( − 1 ) k a 1 = a 2 = ⋯ = a k = 1 0 o t h e r w i s e \mu(n)=\xi^{-1}(n)= \left\{\begin{matrix}1 &n=1 \\(-1)^k &a1=a2=\cdots =ak=1&\\ 0&otherwise \end{matrix}\right. μ(n)=ξ1(n)=1(1)k0n=1a1=a2==ak=1otherwise

如 何 推 导 如何推导

  • 如 何 求 逆 如何求逆

    ③等价于

    ∀ n : ∑ d ∣ n f ( d ) ⋅ f − 1 ( n d ) = [ n = 1 ] \begin{aligned}\forall n: \sum_{d|n} f(d)·f^{-1}(\frac nd)=[n=1]\end{aligned} n:dnf(d)f1(dn)=[n=1]

    ∑ d ∣ n f ( d ) ⋅ f − 1 ( n d ) = I ( n ) ( n = ̸ 1 ) = 0 \begin{aligned}\sum_{d|n}f(d)·f^{-1}(\frac nd)=I(n) (n=\not 1)=0\end{aligned} dnf(d)f1(dn)=I(n)(n≠1)=0

    f − 1 ( n ) ⋅ f ( 1 ) = − ∑ d ∣ n , d = ̸ 1 f ( d ) ⋅ f − 1 ( n d ) f^{-1}(n)·f(1)=-\sum_{d|n,d=\not1}f(d)·f^{-1}(\frac nd) f1(n)f(1)=dn,d≠1f(d)f1(dn)

    f − 1 ( n ) = − ∑ d ∣ n , d = ̸ 1 f ( d ) ⋅ f − 1 ( n d ) f ( 1 ) \begin{aligned}f^{-1}(n)=\frac{-\sum_{d|n,d=\not1}f(d)·f^{-1}(\frac nd)}{f(1)}\end{aligned} f1(n)=f(1)dn,d≠1f(d)f1(dn)

  • 莫 比 乌 斯 函 数 莫比乌斯函数

    f = ξ f=\xi f=ξ, p p p为质数

    f − 1 ( 1 ) = 1 f^{-1}(1)=1 f1(1)=1

    f − 1 ( p ) = − f ( p ) ⋅ f − 1 ( 1 ) f ( 1 ) = − 1 f^{-1}(p)=\frac{-f(p)·f^{-1}(1)}{f(1)}=-1 f1(p)=f(1)f(p)f1(1)=1

    f − 1 ( p 2 ) = − ( f ( p 2 ) ⋅ f − 1 ( 1 ) + f ( p ) ⋅ f − 1 ( p ) ) = 0 f^{-1}(p^2)=-(f(p^2)·f^{-1}(1)+f(p)·f^{-1}(p))=0 f1(p2)=(f(p2)f1(1)+f(p)f1(p))=0

    f − 1 ( p 3 ) = − ( f ( p 3 ) ⋅ f − 1 ( 1 ) + f ( p 2 ) ⋅ f − 1 ( p ) + f ( p ) ⋅ f − 1 ( p 2 ) ) = 0 f^{-1}(p^3)=-(f(p^3)·f^{-1}(1)+f(p^2)·f^{-1}(p)+f(p)·f^{-1}(p^2))=0 f1(p3)=(f(p3)f1(1)+f(p2)f1(p)+f(p)f1(p2))=0

    f − 1 ( p k ) = − ∑ i = 1 k f ( p i ) ⋅ f − 1 ( p k − i ) \begin{aligned}f^{-1}(p^k)=-\sum_{i=1}^k f(p^i)·f^{-1}(p^{k-i})\end{aligned} f1(pk)=i=1kf(pi)f1(pki)

    其中只有 f ( p k ) ⋅ f − 1 ( 1 ) = 1 , f ( p k − 1 ) ⋅ f − 1 ( p ) = − 1 f(p^k)·f^{-1}(1)=1,f(p^{k-1})·f^{-1}(p)=-1 f(pk)f1(1)=1,f(pk1)f1(p)=1,其余项都是0

    f − 1 ( p k ) = { − 1 k = 1 0 k > 1 f^{-1}(p^k)=\left\{\begin{matrix}-1 & k=1\\0 & k>1 \end{matrix}\right. f1(pk)={10k=1k>1

    n = p 1 a 1 ⋅ p 2 a 2 ⋅ ⋯ p k a k n=p_1^{a_1}·p_2^{a_2}·\cdots p_k^{a_k} n=p1a1p2a2pkak

    所以莫比乌斯函数可以推出来了

    μ ( n ) = f − 1 ( n ) = { 1 n = 1 ( − 1 ) k a 1 = a 2 = ⋯ = a k = 1 ⇐ 积 性 函 数 推 出 0 o t h e r w i s e \mu(n)=f^{-1}(n)= \left\{\begin{matrix}1 &n=1 \\(-1)^k &a1=a2=\cdots =ak=1&\Leftarrow 积性函数推出 \\ 0&otherwise \end{matrix}\right. μ(n)=f1(n)=1(1)k0n=1a1=a2==ak=1otherwise


莫 比 乌 斯 函 数 的 性 质 莫比乌斯函数的性质

  • μ ∗ ξ = I \mu*\xi=I μξ=I
    因为 μ = ξ − 1 \mu=\xi^{-1} μ=ξ1
    所以有 ∑ d ∣ n n μ ( d ) = ∑ d ∣ n n μ ⋅ 1 = μ ∗ ξ ( n ) = I ( n ) = [ n = 1 ] \begin{aligned}\sum_{d|n}^n\mu(d)=\sum_{d|n}^n\mu·1=\mu*\xi(n)=I(n)=[n=1]\end{aligned} dnnμ(d)=dnnμ1=μξ(n)=I(n)=[n=1]
    记住这句,下面的证明要用到
  • I ∗ f = f I*f=f If=f
  • f ∗ μ ∗ ξ = f f*\mu*\xi=f fμξ=f
  • f ∗ ξ = g , f = g ∗ μ f*\xi=g,f=g*\mu fξ=g,f=gμ

线 性 筛 线性筛 线

利用积性函数的性质以及 μ \mu μ的表达式

int cnt;
int prime[maxn],mu[maxn];
bool vis[maxn];
void mu_sieve (int n)
{
	mu[1]=1;
	for (int i=2;i<=n;++i){
		if (!vis[i]){	prime[++cnt]=i,mu[i]=-1;}
		for (int j=1;j<=cnt&&i*prime[j]<=n;++j){
			vis[i*prime[j]]=true;
			if (i%prime[j]==0){
				mu[i*prime[j]]=0;
				break;
			}
			mu[i*prime[j]]=-mu[i];
		}
	}
}


莫 比 乌 斯 反 演 莫比乌斯反演

  • 若有 g ( n ) = ∑ d ∣ n f ( d ) \begin{aligned}g(n)=\sum_{d|n}f(d)\end{aligned} g(n)=dnf(d)
    则有 f ( n ) = ∑ d ∣ n μ ( d ) g ( n d ) = ∑ d ∣ n μ ( n d ) g ( d ) \begin{aligned}f(n)=\sum_{d|n}\mu(d)g(\frac{n}{d})=\sum_{d|n}\mu(\frac{n}{d})g(d)\end{aligned} f(n)=dnμ(d)g(dn)=dnμ(dn)g(d)

    • 证 明 证明

      法一 ∑ d ∣ n μ ( d ) g ( n d ) = ∑ d ∣ n μ ( d ) ∑ x ∣ n d f ( x ) = ∑ x ∣ n f ( x ) ∑ d ∣ n x μ ( d ) = ∑ x ∣ n f ( x ) [ n x = 1 ] = f ( n ) \begin{aligned} \sum_{d|n}{\mu(d)g(\frac{n}{d})}=\sum_{d|n}{\mu(d)\sum_{x|\frac{n}{d}}{f(x)}}=\sum_{x|n}{f(x)}\sum_{d|\frac{n}{x}}{\mu(d)}=\sum_{x|n}{f(x)[\frac{n}{x}=1]}=f(n) \end{aligned} dnμ(d)g(dn)=dnμ(d)xdnf(x)=xnf(x)dxnμ(d)=xnf(x)[xn=1]=f(n)
      法二 g = f ∗ ξ , f = g ∗ μ g=f*\xi,f=g*\mu g=fξ,f=gμ
  • 若有 g ( n ) = ∑ n ∣ d f ( d ) \begin{aligned}g(n)=\sum_{n|d}f(d)\end{aligned} g(n)=ndf(d)
    则有 f ( n ) = ∑ n ∣ d μ ( d n ) g ( d ) \begin{aligned}f(n)=\sum_{n|d}\mu(\frac{d}{n})g(d)\end{aligned} f(n)=ndμ(nd)g(d)

    • 证 明 证明

      ∑ n ∣ d μ ( d n ) g ( d ) = ∑ k = 1 + ∞ μ ( k ) g ( k n ) = ∑ k = 1 + ∞ μ ( k ) ∑ k n ∣ d f ( d ) = ∑ n ∣ d f ( d ) ∑ k ∣ d n μ ( k ) = ∑ n ∣ d f ( d ) [ d n = 1 ] = f ( n ) \begin{aligned}\sum_{n|d}\mu(\frac{d}{n})g(d)=\sum_{k=1}^{+\infty}\mu(k)g(kn)=\sum_{k=1}^{+\infty}\mu(k)\sum_{kn|d}f(d)=\sum_{n|d}f(d)\sum_{k|\frac{d}{n}}\mu(k)=\sum_{n|d}f(d)[\frac{d}{n}=1]=f(n) \end{aligned} ndμ(nd)g(d)=k=1+μ(k)g(kn)=k=1+μ(k)kndf(d)=ndf(d)kndμ(k)=ndf(d)[nd=1]=f(n)

    这一种一般用的比较多


莫 比 乌 斯 反 演 的 应 用 莫比乌斯反演的应用

莫比乌斯函数一般用于 g c d gcd gcd
∑ i = 1 n ∑ j = 1 m gcd ⁡ ( i , j ) = ∑ d m i n ( n , m ) d ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = d ] = ∑ d m i n ( n , m ) d ∑ i = 1 n d ∑ j = 1 m d [ g c d ( i , j ) = 1 ] = ∑ d m i n ( n , m ) d ∑ i = 1 n d ∑ j = 1 m d ∑ k ∣ g c d ( i , j ) μ ( k ) = ∑ d m i n ( n , m ) d ∑ k m i n ( n d , m d ) μ ( k ) ∑ k ∣ i n d ∑ k ∣ j m d = ∑ d m i n ( n , m ) d ∑ k m i n ( n d , m d ) μ ( k ) ⌊ n k d ⌋ ⌊ m k d ⌋ \begin{aligned} \sum_{i=1}^n\sum_{j=1}^m\gcd(i,j)&=\sum_d^{min(n,m)} d\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)=d] \\ &=\sum_{d}^{min(n,m)}d\sum_{i=1}^{\frac nd}\sum_{j=1}^{\frac md}[gcd(i,j)=1] \\ &=\sum_{d}^{min(n,m)}d\sum_{i=1}^{\frac nd}\sum_{j=1}^{\frac md}\sum_{k\mid gcd(i,j)}\mu(k) \\ &=\sum_d^{min(n,m)} d\sum_k^{min(\frac{n}{d},\frac{m}{d})}\mu(k)\sum_{k\mid i}^{\frac nd}\sum_{k\mid j}^{\frac md} \\ &=\sum_{d}^{min(n,m)}d\sum_k^{min(\frac{n}{d},\frac{m}{d})}\mu(k)\lfloor \frac n{kd}\rfloor \lfloor\frac m{kd}\rfloor \end{aligned} i=1nj=1mgcd(i,j)=dmin(n,m)di=1nj=1m[gcd(i,j)=d]=dmin(n,m)di=1dnj=1dm[gcd(i,j)=1]=dmin(n,m)di=1dnj=1dmkgcd(i,j)μ(k)=dmin(n,m)dkmin(dn,dm)μ(k)kidnkjdm=dmin(n,m)dkmin(dn,dm)μ(k)kdnkdm
T = k d T=kd T=kd
                             = ∑ T m i n ( n , m ) ⌊ n T ⌋ ⌊ m T ⌋ ∑ k ∣ T T k μ ( k ) \begin{aligned} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ =\sum_{T}^{min(n,m)}\lfloor\frac nT\rfloor\lfloor\frac mT\rfloor\sum_{k\mid T}\frac Tk\mu(k)\end{aligned}                             =Tmin(n,m)TnTmkTkTμ(k)
以上是莫比乌斯反演推出来的式子

                             = ∑ T m i n ( n , m ) ⌊ n T ⌋ ⌊ m T ⌋ φ ( T ) \begin{aligned}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ =\sum_{T}^{min(n,m)}\lfloor\frac nT\rfloor\lfloor\frac mT\rfloor\varphi(T)\end{aligned}                             =Tmin(n,m)TnTmφ(T)
这一步是欧拉反演,证明请移步欧拉函数|(扩展)欧拉定理|欧拉反演
当然,这里这个例子不如直接用欧拉反演
∑ i = 1 n ∑ j = 1 m gcd ⁡ ( i , j ) = ∑ i = 1 n ∑ j = 1 m ∑ d ∣ g c d ( i , j ) φ ( d ) = ∑ d = 1 m i n ( n , m ) ⌊ n d ⌋ ⌊ m d ⌋ φ ( d ) \begin{aligned}\sum_{i=1}^n\sum_{j=1}^m\gcd(i,j)&=\sum_{i=1}^n\sum_{j=1}^m\sum_{d|gcd(i,j)}\varphi(d)=\sum_{d=1}^{min(n,m)}\lfloor\frac{n}{d}\rfloor\lfloor\frac{m}{d}\rfloor\varphi(d)\end{aligned} i=1nj=1mgcd(i,j)=i=1nj=1mdgcd(i,j)φ(d)=d=1min(n,m)dndmφ(d)


如有哪里讲得不是很明白或是有错误,欢迎指正
如您喜欢的话不妨点个赞收藏一下吧

你可能感兴趣的:(理解,数论,#,莫比乌斯反演,竞赛算法)