Mobius Inversion Notes

Mobius inversion

Speaking of which, I was interested in Mobius at the time of the New Year's Eve in 2017...

But now it hurts to see the Mobius inversion QAQ

Mobius inversion , also known as tungsten filament reproduction , is actually two formulas:

Let the functions $f(n)$, $g(n)$ be number-theoretic functions and satisfy:

$f(n)=\sum\limits_{d|n}g(d)$

Möbius inversion

$g(n)=\sum\limits_{d|n}μ(d)f(\frac{n}{d})$

or if:

$f(n)=\sum\limits_{n|d}g(d)$

Then there are:

$g(n)=\sum\limits_{n|d}μ(\frac{d}{n})f(d)(d\le maxd)$

where $μ$ is the Möbius function, set $n=p_{1}^{k_{1}}\cdot p_{2}^{k_{2}}\cdot\cdots\cdot p_{m}^ {k_{m}}$ ($p$ is a prime number)

Then $μ(n)$ is defined as follows:

$μ(n)=
\begin{cases}
1& n=1 \\
(-1)^{m}& \prod\limits_{i=1}^{m}k_{i}=1 \\
0& otherwise(k_{i}>1) 
\end{cases}$

$μ(n)$ has some very wonderful properties, the following examples illustrate:

Property 1: $\mu(n)$ is an integral function, that is, for positive integers $n$, $m$, when $n$ and $m$ are relatively prime, both $f(nm)=f(n )f(m)$;

Proof: omitted, you can consider the proof from the factorization direction

Property two:

$\sum\limits_{d|n}\mu(d)=
\begin{cases}
1& n=1 \\
0& n\not=1
\end{cases}$

Proof: The conclusion is obvious when $n=1$

When $n\not=1$, decompose $n$ into $p_{1}^{k_{1}}\cdot p_{2}^{k_{2}}\cdot\cdots\cdot p_{m }^{k_{m}}$

At this time, only the prime factors with degree 1 need to be considered, and it is easy to obtain that there are $C_{m}^{r}$ factors of which the number of prime factors is $r$, then the original formula can be transformed into:

$\sum\limits_{d|n}\mu(d)=C_{m}^{0}-C_{m}^{1}+C_{m}^{2}-\cdots+(-1)^{m}C_{m}^{m}=\sum\limits_{i=0}^{m}(-1)^{i}C_{m}^{i}$

According to the binomial theorem, we get

$(x+y)^{m}=\sum\limits_{i=0}^{m}C_{m}^{i}x^{i}y^{n-i}$

Substitute $x=-1, y=1$ into $\sum\limits_{i=0}^{m}(-1)^{i}C_{m}^{i}=0$

Certificate completed.

Now let's prove the Mobius inversion. Many of the proofs on the Internet are very simple, and I am confused. Here I will write a little more in detail.

Verification: When $f(n)=\sum\limits_{d|n}g(d)$, $g(n)=\sum\limits_{d|n}\mu(d)f(\frac{ n}{d})$

Proof: Defined by $f$,$g$, $\sum\limits_{d|n}\mu(d)f(\frac{n}{d})=\sum\limits_{d|n}\ mu(d)\sum\limits_{k|\frac{n}{d}}g(k)$

变式得$\sum\limits_{d|n}\mu(d)\sum\limits_{k|\frac{n}{d}}g(k)=\sum\limits_{k|n}g(k)\sum\limits_{d|\frac{n}{k}}\mu(d)$

And because when $n>1$ $\sum\limits_{d|n}\mu(d)=0$

So if and only if $k=n$, the value of the sum formula is $g(n)$, otherwise it is $0$, and the final result is also $g(n)$

Certificate completed.

Another type of proof is similar, only writing the conclusion:

当$f(n)=\sum\limits_{n|d}g(d)$时$g(n)=\sum\limits_{n|d}μ(\frac{d}{n})f(d)$

所以,没有了?

下面我们来看实现和应用。。。这东西看起来就俩式子,但是非常非常有用!

莫比乌斯反演实现与应用

1.线性筛求莫比乌斯函数

我们莫比乌斯函数是积性函数,那么就可以用线性筛来求。

具体实现就是在素数线性筛上加了几句,首先,素数的莫比乌斯函数是$-1$,当一个数的某个质因子指数大于一时就筛成0。注意实现时如果一个数的最小质因子指数大于$1$才会直接被筛为0,否则会用$\mu(x)=-\mu(i)$来完成。每个数被它的最小质因子筛去,所以是线性的。

代码:

 1 int miu[100001],pri[100001],tot=0;
 2 bool ntp[100001];
 3 void getmiu(){
 4     memset(ntp,0,sizeof(ntp));
 5     memset(miu,0,sizeof(miu));
 6     miu[1]=1;
 7     for(int i=2;i<=100000;i++){
 8         if(!ntp[i]){
 9             pri[++tot]=i;
10             miu[i]=-1;
11         }
12         for(int j=1;j<=tot&&pri[j]*i<=100000;j++){
13             ntp[pri[j]*i]=true;
14             if(i%pri[j]==0){
15                 miu[i*pri[j]]=0;
16                 break;
17             }
18             miu[i*pri[j]]=-miu[i];
19         }
20     }
21 } 

2.求解一系列有关gcd(是最小公因数不是……)的问题

例题一:BZOJ2301&&HDU1695(这个要求x,y和y,x不重复的)

题意:$n$次询问,每次求有多少对$(x,y)$满足$a\le x\le b$且$c\le y\le d$且$gcd(x,y)=k$。

$n,a,b,c,d$都是$50000$级别。

容斥一下(不会怎么容斥的请Alt+F4百度一下)每次拆成四个询问,都是形如有多少对$(x,y)$满足$1\le x\le n$且$1\le y\le m$且$gcd(x,y)=k$(默认$n\le m$否则交换)。

这样的询问等价于询问有多少对$(x,y)$满足$1\le x\le \lfloor\frac{n}{k}\rfloor$且$1\le y\le \lfloor\frac{m}{k}\rfloor$且$x,y$互质

直接做?4*50000^3=???

考虑莫比乌斯反演,令$g(i)$等于满足$1\le x\le n$且$1\le y\le m$且$gcd(x,y)=i$的数对$(x,y)$个数,$f(i)$等于满足$1\le x\le n$且$1\le y\le m$且$i|gcd(x,y)$的数对$(x,y)$个数。

易得$f(i)=\lfloor\frac{n}{i}\rfloor\lfloor\frac{m}{i}\rfloor$

反演一下得$g(i)=\sum\limits_{i|d}\mu(\frac{d}{i})f(d)=\sum\limits_{i|d}\mu(\frac{d}{i})\lfloor\frac{n}{d}\rfloor\lfloor\frac{m}{d}\rfloor$

枚举每个$k$的倍数,就可以$O(n)$处理每个询问了。

然而50000^2=???

继续考虑优化

观察式子$\lfloor\frac{n}{d}\rfloor$,可(da)以(dan)证(cai)明(ce)发现它最多只有$O(\sqrt{n})$种取值。

具体证明如下:

考虑$1\le d\le \sqrt{n}$的情况,此时$\lfloor\frac{n}{d}\rfloor$最多只有$\sqrt{n}$种取值;

剩余当$\sqrt{n}\le d\le n$时,有$\lfloor\frac{n}{d}\rfloor<\sqrt{n}$,因此取值也不超过$\sqrt{n}$种;

综上,$\lfloor\frac{n}{d}\rfloor$最多只有$O(\sqrt{n})$种取值。

即$\lfloor\frac{n}{d}\rfloor$取值连续的段最多只有$\sqrt{n}+\sqrt{m}$段。

所以就可以$O(\sqrt{n}+\sqrt{m})$时间内枚举,然后就……做完了?

具体实现就是分块维护一个$\mu$的前缀和。

并且非常好写!

完整代码:

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cmath>
 5 using namespace std;
 6 typedef long long ll;
 7 int t,a,b,c,d,k,pre[100001],miu[100001],pri[100001],tot=0;
 8 bool ntp[100001];
 9 void getmiu(){
10     memset(ntp,0,sizeof(ntp));
11     memset(miu,0,sizeof(miu));
12     miu[1]=1;
13     for(int i=2;i<=100000;i++){
14         if(!ntp[i]){
15             pri[++tot]=i;
16             miu[i]=-1;
17         }
18         for(int j=1;j<=tot&&pri[j]*i<=100000;j++){
19             ntp[pri[j]*i]=true;
20             if(i%pri[j]==0){
21                 miu[i*pri[j]]=0;
22                 break;
23             }
24             miu[i*pri[j]]=-miu[i];
25         }
26     }
27 }
28 ll work(int n,int m){
29     ll ans=0;
30     int last=0;
31     if(n>m)swap(n,m);
32     for(int i=1;i<=n;i=last+1){
33         last=min(n/(n/i),m/(m/i));
34         ans+=(ll)(pre[last]-pre[i-1])*(n/i)*(m/i);
35     }
36     return ans;
37 }
38 int main(){
39     getmiu();
40     pre[0]=0;
41     for(int i=1;i<=100000;i++)pre[i]=pre[i-1]+miu[i];
42     scanf("%d",&t);
43     for(int i=1;i<=t;i++){
44         scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
45         a--,c--;
46         if(!k)printf("0\n");
47         else printf("%lld\n",work(b/k,d/k)-work(a/k,d/k)-work(b/k,c/k)+work(a/k,c/k));
48     }
49     return 0;
50 }

 例题二:BZOJ2820

题目大意:求有多少对$(x,y)$满足$1\le x\le n$且$1\le y\le m$且$gcd(x,y)=$质数

定义$f(i)$,$g(i)$同上,再枚举质数,有

$ans=\sum\limits_{p}^{n}(\sum\limits_{d=1}^{\frac{n}{p}}\mu(d)\lfloor\frac{n}{pd}\rfloor\lfloor\frac{m}{pd}\rfloor)$

直接做?复杂度大概是$O(\frac{n\sqrt{n}}{logn})$,不是很行

变一下式,设$t=pd$,有

$ans=\sum\limits_{t=1}^{n}\lfloor\frac{n}{t}\rfloor\lfloor\frac{m}{t}\rfloor(\sum\limits_{p|t}\mu(\frac{t}{p}))$

预处理一下$\sum\limits_{p|t}\mu(\frac{t}{p})$,剩下的分块前缀和就和之前一模一样了。

如何预处理?

考虑线性筛,$f[i*prime[j]]$

$prime[j]|i$时显然$f[i]=\mu(i)$

否则考虑$\mu(i*prime[j]/p_{1})$,当$prime[j]=p_{1}$时为$\mu(i)$,否则所有和为$-f[i]$,因此总和即为$\mu(i)-f[i]$。

代码:

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cmath>
 5 using namespace std;
 6 typedef long long ll;
 7 int t,a,b,pre[10000001],miu[10000001],pri[10000001],f[10000001],tot=0;
 8 bool ntp[10000001];
 9 void getmiu(){
10     memset(ntp,0,sizeof(ntp));
11     memset(miu,0,sizeof(miu));
12     miu[1]=1;
13     for(int i=2;i<=10000000;i++){
14         if(!ntp[i]){
15             pri[++tot]=i;
16             miu[i]=-1;
17             f[i]=1;
18         }
19         for(int j=1;j<=tot&&pri[j]*i<=10000000;j++){
20             ntp[pri[j]*i]=true;
21             if(i%pri[j]==0){
22                 miu[i*pri[j]]=0;
23                 f[i*pri[j]]=miu[i];
24                 break;
25             }
26             miu[i*pri[j]]=-miu[i];
27             f[i*pri[j]]=miu[i]-f[i];
28         }
29     }
30 }
31 ll work(int n,int m){
32     ll ans=0;
33     int last=0;
34     if(n>m)swap(n,m);
35     for(int i=1;i<=n;i=last+1){
36         last=min(n/(n/i),m/(m/i));
37         ans+=(ll)(pre[last]-pre[i-1])*(n/i)*(m/i);
38     }
39     return ans;
40 }
41 int main(){
42     getmiu();
43     pre[0]=0;
44     for(int i=1;i<=10000000;i++)pre[i]=pre[i-1]+f[i];
45     scanf("%d",&t);
46     for(int i=1;i<=t;i++){
47         scanf("%d%d",&a,&b);
48         printf("%lld\n",work(a,b));
49     }
50     return 0;
51 }

 

 

 

例题三:GDOI2018Day2T1谈笑风生

题目过于暴力不予显示(顾名思义)

题目大意:n个点m条边的无向图,边权由点权计算,其中可以花费p能量把每条边权减少p,求最少要花费多少能量才能使1~n的最短路小于等于t;

其中边权计算式为$w=\sum\limits_{i=1}^{num[u]}\sum\limits_{j=1}^{num[v]}(i+j)[gcd(i,j)==1]$

待填坑……

3.还有吗?那我就不会了

总结

不存在的

就是背好两个式子吧……

同时推荐一些很好的课件(同时也是我学习的):Orz PoPoQQQSengxian's Blog

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325265247&siteId=291194637