閱讀643 返回首頁    go 阿裏雲 go 技術社區[雲棲]


POJ 2478 法雷級數

題意:給出法雷級數定義

F2 = {1/2} 

F3 = {1/3, 1/2, 2/3} 
F4 = {1/4, 1/3, 1/2, 2/3, 3/4} 

F5 = {1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5}  求f[n]組法雷級數分數的個數。

不難發現就是歐拉函數前n項的和。運用遞推求歐拉函數即可。再發個快的,留著比賽做模板。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 1000008
long long phi[maxn];
void getphi()
{
    for(int i=1; i<maxn; i++) phi[i]=i;
    for(int i=2; i<maxn; i+=2) phi[i]>>=1;
    for(int i=3; i<maxn; i+=2)
        if(phi[i]==i)
        {
            for(int j=i; j<maxn; j+=i)
                phi[j]=phi[j]/i*(i-1);
        }
    for(int i=3; i<maxn; i++)
        phi[i]+=phi[i-1];
}
int main()
{
    getphi();
    int n;
    while(~scanf("%d",&n),n)
        printf("%lld\n",phi[n]);
    return 0;
}
網上找的快的79ms
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>

#ifdef unix
#define lld "%lld"
#else
#define lld "%I64d"
#endif

using namespace std;

const int maxn=1000005;

int n;
int prime[maxn/10],prime_size;
bool prime_flag[maxn]= {1,1};
int phi[maxn]= {0,1};
long long ans[maxn];

void mk_prime()
{
    for(int i=2; i<=n; i++)
    {
        if(prime_flag[i]==0)
        {
            prime[prime_size++]=i;
            phi[i]=i-1;
        }
        for(int j=0; j<prime_size && i*prime[j]<=n; j++)
        {
            prime_flag[i*prime[j]]=true;
            if(i%prime[j]==0)
            {
                phi[i*prime[j]]=phi[i]*prime[j];
                break;
            }
            else
            {
                phi[i*prime[j]]=phi[i]*(prime[j]-1);
            }
        }
    }
}

int main()
{
    n=maxn-2;
    mk_prime();
    for(int i=1; i<maxn; i++)
        ans[i]=ans[i-1]+phi[i];
    while(true)
    {
        scanf("%d",&n);
        if(n==0)
            return 0;
        printf(lld"\n",ans[n]-1ll);
    }
    return 0;
}


最後更新:2017-04-03 16:48:45

  上一篇:go pci、pci-x、pcie的區別
  下一篇:go 中國也許不需要穀歌