莫比乌斯反演
对于定义在\(\mathbb{N}\)上的函数\(F(n)\)和\(f(n)\),若满足:
\(F(n) = \sum\limits_{d\mid n}f(d)\)
则有:
\(f(n) = \sum\limits_{d\mid n}\mu(d)F(\frac{n}{d})\)
一个更常用的形式是:
若有:
\(F(n) = \sum\limits_{n\mid d}f(d)\)
则有:
\(f(n) = \sum\limits_{n\mid d}\mu(\frac{d}{n})F(d)\)
其中\(\mu(d)\)为莫比乌斯函数:
令 \(x = p_{1}^{a_{1}}p_{2}^{a_{2}}p_{3}^{a_{3}}\cdot\cdot\cdot p_{k-1}^{a_{k-1}}p_{k}^{a_{k}},p_{1},p_{2},p_{3} \cdot \cdot \cdot p_{k-1},p_{k}\in \mathbb{P}\)
则满足:
\(\mu(x) = \left\{\begin{matrix}&1 &,x = 1\\ &(-1)^{k} &,\forall i\in [1,k],a_{i}=1\\ &0 &,\exists i\in [1,k],a_{i}>1\end{matrix}\right.\)
证明如下:
\(\sum\limits_{d\mid n}\mu(d)F(\frac{n}{d}) = \sum\limits_{d\mid n}\mu(d)\sum\limits_{d^{'}\mid\frac{n}{d}}f(d^{'}) = \sum\limits_{d\mid n}\sum\limits_{d^{'}\mid\frac{n}{d}}\mu(d)f(d^{'}) = \sum\limits_{d^{'}\mid n}\sum\limits_{d\mid\frac{n}{d^{'}}}\mu(d)f(d^{'}) = \sum\limits_{d^{'}\mid n}f(d^{'})\sum\limits_{d\mid\frac{n}{d^{'}}}\mu(d) = f(n)\)
莫比乌斯函数还有两个很神奇的性质:
1.\(\sum_{d|n}\mu (d)=[n==1]\)
2.\(\sum_{d|n}\frac{\mu (d)}{d}=\frac {\phi (d)}{d}\)
以下为线性筛莫比乌斯函数的代码
int cnt, prime[N], mu[N], vis[N];
void get_mu(int n) {
mu[1] = 1;
for(int i = 2; i <= n; i++) {
if(!vis[i]) pime[++cnt] = i, mu[i] = -1;
for(int j = 1; j <= cnt && i*prime[j] <= n; j++) {
vis[i*prime[j]] = 1;
if(i%prime[j] == 0) {
mu[i*prime[j]] = 0;
break;
}
mu[i*prime[j]] = -mu[i];
}
}
}