AcWing 874. 筛法求欧拉函数
原题链接
简单
作者:
Aaron_wch
,
2021-06-11 19:32:27
,
所有人可见
,
阅读 180
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+10,M=8e4;
int n,prime[M],idx=-1,phi[N];
bool st[N];
ll ans=1;
int main()
{
scanf("%d",&n);
for(int i=2;i<=n;i++)
{
if(!st[i])
{
prime[++idx]=i;
phi[i]=i-1;
}
for(int j=0;prime[j]<=n/i;j++)
{
st[prime[j]*i]=true;
if(i%prime[j]==0)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
phi[prime[j]*i]=phi[i]*(prime[j]-1);
}
}
for(int i=2;i<=n;i++)ans+=phi[i];
printf("%lld",ans);
return 0;
}