POJ 2478 Farey Sequence

来源:互联网 发布:python 爬虫 定时任务 编辑:程序博客网 时间:2024/06/07 14:51
分母为x的既约真分数用欧拉函数算,因为欧拉函数的值为小于n且与n互素的数,然后遍历一遍,打表即可。
////  main.cpp//  Richard////  Created by 邵金杰 on 16/8/2.//  Copyright © 2016年 邵金杰. All rights reserved.//#include<iostream>using namespace std;typedef long long LL;const int maxn=1000001;LL phi[maxn],f[maxn];void phi_table(){    for(int i=1;i<=maxn;i++) phi[i]=0;    for(int i=2;i<=maxn;i++)    {        if(!phi[i])        {            for(int j=i;j<=maxn;j+=i)            {                if(!phi[j]) phi[j]=j;                phi[j]=phi[j]/i*(i-1);            }        }    }    for(int i=2;i<=maxn;i++)    {        f[i]=f[i-1]+phi[i];    }}int main(){    phi_table();    int n;    while(cin>>n)    {        if(n==0) break;        cout<<f[n]<<endl;    }    return 0;}

0 0