莫比乌斯函数及其反演学习笔记
——by sunzz3183
定义
μ ( x ) ≡ { 1 x = 1 ( − 1 ) k x = p 1 ⋅ p 2 ⋅ ⋯ ⋅ p k 0 o t h e r w i s e \mu (x)\equiv \begin{cases} 1&x=1 \\(-1)^k&x=p_1\cdot p_2\cdot \cdots\cdot p_k \\0&otherwise \end{cases} μ(x)≡⎩ ⎨ ⎧1(−1)k0x=1x=p1⋅p2⋅⋯⋅pkotherwise
求法
- 直接求
inline int mu(int x){
if(!x)return 0;
if(x==1)return 1;
int sum=0;
for(int i=2;i*i<=x;i++)
if(!(x%i))
if(!(x/i%i))return 0;
else sum++;
return sum&1?-1:1;
}
- 线性筛
int cnt,prime[M],phi[N],mu[N];
bool is_p[N];
void init(int n){
phi[1]=mu[1]=1;
for(int i=2;i<=n;i++){
if(!is_p[i])prime[++cnt]=i,phi[i]=i-1,mu[i]=-1;
for(int j=1;j<=cnt&&i*prime[j]<=n;j++){
is_p[i*prime[j]]=1;
if(!(i%prime[j])){
phi[i*prime[j]]=phi[i]*prime[j];
mu[i*prime[j]]=0;
break;
}
phi[i*prime[j]]=phi[i]*(prime[j]-1);
mu[i*prime[j]]=-mu[i];
}
}
}
(根据性质,很简单)
反演
式子 1
∑ d ∣ n μ ( d ) = [ n = 1 ] \sum_{d \mid n} \mu(d)=[n=1] d∣n∑μ(d)=[n=1]
式子 2
[ gcd ( i , j ) = 1 ] = ∑ d ∣ gcd ( i , j ) μ ( d ) [\operatorname{gcd}(i, j)=1]=\sum_{d \mid \gcd(i, j)} \mu(d) [gcd(i,j)=1]=d∣gcd(i,j)∑μ(d)
由式子 1 得
式子 3
∑ i = 1 n ∑ j = 1 m [ gcd ( i , j ) = 1 ] = ∑ i = 1 n ∑ j = 1 m ∑ d ∣ g c d ( i , j ) μ ( d ) = ∑ i = 1 n ∑ d = 1 m i n ( i , m ) μ ( d ) ⌊ m i n ( i , m ) d ⌋ = ∑ d = 1 m i n ( n , m ) μ ( d ) ⌊ n d ⌋ ⌊ m d ⌋ \begin{aligned} \\&\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{m}[\gcd(i,j)=1] \\&= \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_{d=1}^{min(i,m)} \mu (d)\left \lfloor \frac{min(i,m)}{d} \right \rfloor \\&=\sum\limits_{d=1}^{min(n,m)} \mu (d)\left \lfloor \frac{n}{d} \right \rfloor \left \lfloor \frac{m}{d} \right \rfloor \end{aligned} i=1∑nj=1∑m[gcd(i,j)=1]=i=1∑nj=1∑md∣gcd(i,j)∑μ(d)=i=1∑nd=1∑min(i,m)μ(d)⌊dmin(i,m)⌋=d=1∑min(n,m)μ(d)⌊dn⌋⌊dm⌋
式子 4
∑ i = 1 n ∑ j = 1 m [ gcd ( i , j ) = k ] \sum\limits_{i=1}^{n} \sum\limits_{j=1}^{m}[\gcd(i,j)=k] i=1∑nj=1∑m[gcd(i,j)=k]
我们可以转化和式子 3 一样的。
∑ i = 1 n ∑ j = 1 m [ gcd ( i , j ) = k ] = ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ [ gcd ( i , j ) = 1 ] = ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ ∑ d ∣ g c d ( i , j ) μ ( d ) = ∑ d = 1 ⌊ m i n ( n , m ) k ⌋ μ ( d ) ⌊ n d × k ⌋ ⌊ m d × k ⌋ \begin{aligned} \\&\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{m}[\gcd(i,j)=k] \\&=\sum\limits_{i=1}^{\left \lfloor \frac{n}{k} \right \rfloor } \sum\limits_{j=1}^{\left \lfloor \frac{m}{k} \right \rfloor }[\gcd(i,j)=1] \\&=\sum\limits_{i=1}^{\left \lfloor \frac{n}{k} \right \rfloor } \sum\limits_{j=1}^{\left \lfloor \frac{m}{k} \right \rfloor }\sum\limits_{d|gcd(i,j)} \mu (d) \\&=\sum\limits_{d=1}^{\left \lfloor \frac{min(n,m)}{k} \right \rfloor } \mu (d)\left \lfloor \frac{n}{d\times k} \right \rfloor \left \lfloor \frac{m}{d\times k} \right \rfloor \end{aligned} i=1∑nj=1∑m[gcd(i,j)=k]=i=1∑⌊kn⌋j=1∑⌊km⌋[gcd(i,j)=1]=i=1∑⌊kn⌋j=1∑⌊km⌋d∣gcd(i,j)∑μ(d)=d=1∑⌊kmin(n,m)⌋μ(d)⌊d×kn⌋⌊d×km⌋
式子 5
∑ i = 1 n ∑ j = 1 m [ gcd ( i , j ) ≤ x ] \sum\limits_{i=1}^{n} \sum\limits_{j=1}^{m}[\gcd(i,j)\leq x] i=1∑nj=1∑m[gcd(i,j)≤x]
转化成式子 4。
∑ i = 1 n ∑ j = 1 m [ gcd ( i , j ) ≤ x ] = ∑ i = 1 n ∑ j = 1 m ∑ k = 1 x [ gcd ( i , j ) = k ] = ∑ k = 1 x ∑ i = 1 n ∑ j = 1 m [ gcd ( i , j ) = k ] = ∑ k = 1 x ∑ d = 1 ⌊ m i n ( n , m ) k ⌋ μ ( d ) ⌊ n d × k ⌋ ⌊ m d × k ⌋ \begin{aligned} \\&\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{m}[\gcd(i,j)\leq x] \\&=\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{m} \sum\limits_{k=1}^{x}[\gcd(i,j)=k] \\&=\sum\limits_{k=1}^{x} \sum\limits_{i=1}^{n} \sum\limits_{j=1}^{m}[\gcd(i,j)=k] \\&=\sum\limits_{k=1}^{x} \sum\limits_{d=1}^{\left \lfloor \frac{min(n,m)}{k} \right \rfloor } \mu (d)\left \lfloor \frac{n}{d\times k} \right \rfloor \left \lfloor \frac{m}{d\times k} \right \rfloor \end{aligned} i=1∑nj=1∑m[gcd(i,j)≤x]=i=1∑nj=1∑mk=1∑x[gcd(i,j)=k]=k=1∑xi=1∑nj=1∑m[gcd(i,j)=k]=k=1∑xd=1∑⌊kmin(n,m)⌋μ(d)⌊d×kn⌋⌊d×km⌋
求法总结
线性筛 O ( n ) O(n) O(n) 求,然后直接求的话也会是 O ( n ) O(n) O(n),可以使用整数分块和前缀和来使时间优化到 O ( n ) O(\sqrt{n}) O(n)。