上图摘自Acwing的某位大佬:https://www.acwing.com/solution/content/28507/
https://www.acwing.com/problem/content/876/
#include<cstdio>
#include<iostream>
using namespace std;
const int N=1e6+10;
typedef long long int LL;
LL phi[N];
LL prime[N],cnt;
bool st[N];
LL get_prime(int n)
{
phi[1]=1;
for(int i=2;i<=n;i++)
{
if(!st[i])
{
prime[cnt++]=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[prime[j]*i]=phi[i]*prime[j];
break;
}
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
LL res=0;
for(int i=1;i<=n;i++) res+=phi[i];
return res;
}
int n;
int main(void)
{
cin>>n;
cout<<get_prime(n)<<endl;
return 0;
}