UVA12995 Farey Sequence
欧拉函数
同仪仗队那题几乎相同,本质都是求欧拉函数的和
#include<cstdio> #define N 1000000 int n,cnt,pri[N+2],i,j,t; long long phi[N+2],s[N+2]; bool v[N+2]; int main(){ for(i=2;i<=N;++i){ if(!v[i]) phi[i]=i-1,pri[++cnt]=i; for(j=1;j<=cnt;++j){ t=i*pri[j]; if(t>N) break; v[t]=1; if(i%pri[j]) phi[t]=phi[i]*(pri[j]-1); else{phi[t]=phi[i]*pri[j];break;} } }//线性筛欧拉函数板子 for(i=1;i<=N;++i) s[i]=s[i-1]+phi[i]; while(scanf("%d",&n)){ if(!n) break; printf("%lld\n",s[n]); }return 0; }