莫比乌斯函数及其反演学习笔记

莫比乌斯函数及其反演学习笔记

——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=p1p2pkotherwise

求法

  1. 直接求
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;
}
  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] dnμ(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]=dgcd(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=1nj=1m[gcd(i,j)=1]=i=1nj=1mdgcd(i,j)μ(d)=i=1nd=1min(i,m)μ(d)dmin(i,m)=d=1min(n,m)μ(d)dndm

式子 4

例题 [POI2007]ZAP-Queries

∑ 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=1nj=1m[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=1nj=1m[gcd(i,j)=k]=i=1knj=1km[gcd(i,j)=1]=i=1knj=1kmdgcd(i,j)μ(d)=d=1kmin(n,m)μ(d)d×knd×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=1nj=1m[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=1nj=1m[gcd(i,j)x]=i=1nj=1mk=1x[gcd(i,j)=k]=k=1xi=1nj=1m[gcd(i,j)=k]=k=1xd=1kmin(n,m)μ(d)d×knd×km

求法总结

线性筛 O ( n ) O(n) O(n) 求,然后直接求的话也会是 O ( n ) O(n) O(n),可以使用整数分块和前缀和来使时间优化到 O ( n ) O(\sqrt{n}) O(n )

提高

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值