#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #define ll long long using namespace std; ll n,jdg[1000001],prime[1000001],phi[1000001],cnt,ans; int main() { scanf("%lld",&n); phi[1]=1;jdg[1]=1; for(ll i=2;i<=n;i++) { if(!jdg[i]) { prime[++cnt]=i; phi[i]=i-1; } for(ll j=1;j<=cnt;j++) { if(i*prime[j]>n) break; jdg[i*prime[j]]=1; if(i%prime[j]==0) { phi[i*prime[j]]=phi[i]*prime[j]; break; } phi[i*prime[j]]=phi[i]*(prime[j]-1); } } }
原文:https://www.cnblogs.com/water-radish/p/9280649.html