AcWing 874. 筛法求欧拉函数
原题链接
简单
作者:
最后五分钟
,
2024-04-11 12:41:51
,
所有人可见
,
阅读 6
#include<bits/stdc++.h>
#define int long long
#define x first
#define y second
using namespace std;
const int N=1e6+10;
typedef pair<int,int> PII;
vector<int> p;
int phi[N];
int st[N];
void getp(int n)
{
for(int i=2;i<=n;i++)
{
if(!st[i])
{
p.push_back(i);
phi[i]=i-1;
}
for(int j=0;i<=n/p[j];j++)
{
st[i*p[j]]=1;
if(i%p[j]==0)
{
phi[i*p[j]]=phi[i]*p[j];
break;
}
else phi[i*p[j]]=phi[i]*(p[j]-1);
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
int res=0;
phi[1]=1;
getp(n);
for(int i=1;i<=n;i++)res+=phi[i];
cout<<res<<endl;
return 0;
}