首页 > 代码库 > 欧拉筛

欧拉筛

#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#include<queue>#include<cmath> using namespace std;const int Max=100010;bool IsPrime[Max];int  NumPrime[Max],phi[Max];int N,Top=0;int main(){    scanf("%d",&N);    for(int i=1;i<=N;i++) IsPrime[i] = true ;    IsPrime [1] = false;Top = 0;    for(int i=2;i<=N;i++)    {        if(IsPrime[i]){ NumPrime[++Top] = i;phi[i]=i-1;}        for(int j=1;j<=Top && i*NumPrime[j]<=N;j++)        {            IsPrime[i*NumPrime[j]] = false;            if(i % NumPrime[j]==0) {                phi[i * NumPrime[j] ] = NumPrime[j] * phi [i];                break;            }            else phi[ i * NumPrime[j] ] = phi[NumPrime[j]] * phi[i];        }    }    cout<<phi[100]<<endl;}

 

欧拉筛