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;
}


目录
相关文章
|
12月前
【AcWing】曼哈顿距离
【AcWing】曼哈顿距离
41 0
AcWing 658. 一元二次方程公式
AcWing 658. 一元二次方程公式
52 0
AcWing 658. 一元二次方程公式
|
Java 机器学习/深度学习
UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 65817    Accepted Submission(s): 28794 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。
1124 0
|
人工智能
Uva 11300 Spreading the Wealth(递推,中位数)
Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone around a circular table.
892 0
|
Java
HDU 2080 夹角有多大II
夹角有多大II Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10901    Accepted Submission(s): 5642 Problem Description 这次xhd面临的问题是这样的:在一个平面内有两个点,求两个点分别和原点的连线的夹角的大小。
930 0
数论 + 公式 - HDU 4335 What is N?
What is N?  Problem's Link:  http://acm.hdu.edu.cn/showproblem.php?pid=4335   Mean:  给你三个数b、P、M,让你求有多少个n满足下式。
833 0
|
容器
hdu3388 Coprime【容斥原理】
Problem Description Please write a program to calculate the k-th positive integer that is coprime with m and n simultaneously.
1106 0
|
机器学习/深度学习 算法
数论 - 欧拉函数模板题 --- poj 2407 : Relatives
Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11372   Accepted: 5544 Description Given n, a positive in...
977 0