線性篩尤拉函式與莫比烏斯函式 (模板)
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+1;
int prime[MAXN]; //素數
int phi[MAXN]; //尤拉函式
int miu[MAXN]; //莫比烏斯函式
int cnt;
bool vis[MAXN];
int main(){
miu[1]=1;phi[1]=1;
for(register int i=2;i<=MAXN;i++){
if(!vis[i]) {miu[i]=-1;prime[++cnt]=i;phi[i]=i-1 ;vis[i]=1;}
for(register int j=1;j<=cnt && prime[j]*i<=MAXN;j++){
vis[i*prime[j]]=1;
if(i%prime[j]==0){
miu[i*prime[j]]=0;
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else {
phi[i*prime[j]]=phi[i]*(prime[j]-1 );
miu[i*prime[j]]=-miu[i];
}
}
}
for(register int i=1;i<=MAXN;i++){
cout<<i<<" "<<"phi"<<":"<<phi[i]<<"miu"<<":"<<miu[i]<<endl;
}
return 0;
}