题目:
题目链接:Farey Sequence
题解:
欧拉函数的线性筛,题目就是让求前n项的和,累加即可。
(主要是想存一下线性筛的板子)
代码:
#include<stdio.h>
#define LL long long
using namespace std;
const int sea=1000001;
int a[sea],b[sea],f[sea],k,x;
LL s[sea];
void xxs()
{
int n=sea,i,j;for(i=2;i<=n;i++) a[i]=0;for(i=2;i<=n;i++){
if(a[i]==0) a[i]=i,b[++k]=i;for(int j=1;j<=k&&i*b[j]<=n;j++) {
a[i*b[j]]=b[j];if(i%b[j]==0) break;}} f[1]=1;for(i=2;i<=n;i++)if(a[i/a[i]]==a[i]) f[i]=f[i/a[i]]*a[i];else f[i]=f[i/a[i]]*(a[i]-1);for(i=2;i<=n;i++) s[i]=s[i-1]+f[i];
}
int main()
{
// freopen("a.txt","r",stdin);xxs();while(scanf("%d",&x)!=EOF) {
if(x==0) break; printf("%lld\n",s[x]); } return 0;
}