線性篩莫比烏斯函式(C++版)
阿新 • • 發佈:2019-01-01
#include <bits/stdc++.h>
using namespace std;
const int N=1e7+50;
//同時篩出素數和莫比烏斯函式
int p[N],miu[N];
bool check[N];
int pre[N];
void init(){
int t;
miu[1]=1;
check[1]=true;
for(int i=2;i<=N;i++){
if(!check[i]){
p[++p[0]]=i;
miu[i]=-1;
}
for(int j=1;j<=p[0];j++){
t=i*p[j];
if(t>N){
break;
}
check[t]=true;
//有平方因子,函式值為0
if(i%p[j]==0){
miu[t]=0;
}else{
//質因子數量改變,符號改變
miu[t]=-miu[i] ;
}
}
}
}
int main(void){
init();
return 0;
}