线性筛

同时得到欧拉函数和素数表

const int maxn=10000000+5;
bool check[maxn];
int phi[maxn];
int prime[maxn];
int tot;
void phi_ans_prime_table(int n){
    memset(check,false,sizeof(check));
    phi[1]=1;
    tot=0;
    for(int i=2;i<=n;i++){
        if(!check[i]){
            prime[tot++]=i;
            phi[i]=i-1;
        }
        for(int j=0;j<tot;j++){
            if(i*prime[j]>n) break;
            check[i*prime[j]]=true;
            if(i%prime[j]==0) {
                phi[i*prime[j]]=phi[i]*prime[j];
                break;
            }else phi[i*prime[j]]=phi[i]*(prime[j]-1);
        }
    }
}

 

posted on 2018-12-08 18:36  欣崽  阅读(174)  评论(0编辑  收藏  举报

导航