POJ 2478 Farey Sequence 解题报告(欧拉函数 筛求法)

[poj2478] Farey Sequence 题目描述  Farey序列Fn,要求计算a/b(0 < a < b <= n并且gcd(a,b)=1)的个数,例如:   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}   你的任务是计算Farey序列Fn的个数输入格式包含多组测试数据 阅读详情
Farey Sequence
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11555 Accepted: 4492

Description

The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b) = 1 arranged in increasing order. The first few are 
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} 

You task is to calculate the number of terms in the Farey sequence Fn.

Input

There are several test cases. Each test case has only one line, which contains a positive integer n (2 <= n <= 106). There are no blank lines between cases. A line with a single 0 terminates the input.

Output

For each test case, you should output one line, which contains N(n) ---- the number of terms in the Farey sequence Fn. 

Sample Input

2
3
4
5
0

Sample Output

1
3
5
9

    解题报告: 用类似于素数筛的求法求欧拉函数。代码如下:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn=1000010;
bool h[maxn];
int epi[maxn];
long long sum[maxn];

void calEpi()
{
    for(int i=2;i<maxn;i++)
    {
        if(!h[i]) for(int j=i;j<maxn;j+=i)
        {
            if(epi[j]==0) epi[j]=j;
            epi[j]=epi[j]/i*(i-1);
            h[j]=true;
        }

        sum[i]=sum[i-1]+epi[i];
    }
}

int main()
{
    calEpi();

    int n;
    while(~scanf("%d", &n) && n)
    {
        printf("%lld\n", sum[n]);
    }
}


poj2478 Farey Sequence Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 F2 = {1/2}  F3 = {1/3, 1/2, 2/3}  F4 = {1/4, 1/3, 1/2, 2/ 阅读详情

相关推荐

Farey Sequence 法里序列(基本数论/欧拉函数/欧拉)

【代码】Farey Sequence 法里序列(基本数论/欧拉函数/欧拉)思路:欧拉函数的前缀和

qq_73635134的博客 282

Farey Sequence法里序列

Description Farey数列Fn对于每个n( n>=2 ),如果0<a<b<=n 且 ab互质即a/b为不可约有理数,那么就在Fn集合中且以递增序列排序。 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,

onlyxinbaby的博客 1416

[转][数论]Farey Sequence深入学习

Farey Sequence深入学习 本文所要解决的问题: 1、如何生成N阶Farey序列 2、如何用Stern Brocot tree查找有理数或逼近无理数 3、如何求N阶Farey序列中第K个分数 一、基础知识: 1、Stern Brocot tree 如图是一棵Stern Brocot tree Stern Brocot tree是一棵二叉树,初始为(0/1,1/0),...

weixin_34167043的博客 278

POJ 2478 Farey Sequence

POJ 2478 Farey Sequence 题目链接 Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b) = 1 arranged in increasing order. The first few are F2 = {1/2} F3 = {1/

旺崽的博客 2542

poj2478题解-欧拉函数(Farey Sequence)

poj2478题解-欧拉函数 前言 先点关注,不迷路 大家好,我是于斯为盛 这一段刚好在学欧拉函数,说实话刚看到这道题的时候我还是有点懵的 通过这道题,我也意识到了define的重要性 哎,两个地方少打了零 废话不多说 原题传送门: 点这里. 讲得不好勿喷 暴力方法 暴力方法我想很多人都能想明白 无非就是三层循环 while 输入n for 循环分母 for 循环分子 最多加个前缀和,打个表的优化 for 循环分母 for 循环分子//打表 while 输入,从表中读取 但是… O(

maxChang_algha的博客 1247

poj 2478 Farey Sequence 题解(欧拉函数

原题链接 poj 题意简述 多组数据。每次给定n(&lt;=1e6)n(&lt;=1e6)n(<=1e6),求分子分母均是&lt;=n&lt;=n<=n的正整数的真分数有多少个。(注释:真分数:满足1. 分子<分母 2. 分子和分母互质) 思路 这个怎么做呢?观察样例我们珂以有这样一个想法:枚举分母iii,然后求[1,i−1][1,i-1][1,i...

LightningUZ的博客 348

poj 2478 Farey Sequence欧拉函数

Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13204   Accepted: 5181 Description The Farey Sequence Fn for any integer n with n >= 2 is the

hahahaha 1198

POJ2478 Farey Sequence —— 欧拉函数

题目链接:https://vjudge.net/problem/POJ-2478 Farey Sequence Time Limit:1000MS Memory Limit:65536K Total Submissions:17753 Accepted:7112 Description The Farey S...

alince20008的博客 176

POJ2478 Farey Sequence欧拉函数+前缀和】

POJ2478 Farey Sequence 题目 函数F(n)定义为最简分数 a/b 的个数,其中n>1,0<a<b<=n,求F(n)的值 输入 正整数n,n=0时结束 输出 F(n) 样例输入 2 3 4 5 0 样例输出 1 3 5 9 分析 当a和b互质时a/b为最简分数,设f(n)表示为小于n且与n互质的数的个数,那么易得F(n...

逐梦者 400

POJ 2478-Farey Sequence(选法求欧拉函数)

Farey Sequence Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2478 Appoint description:  System Crawler  (2015-04-01) Descript

Rocky0429 1746

POJ2478 Farey Sequence

Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15250   Accepted: 6058 Description The Farey Sequence Fn for any integer n with n >= 2 is the

Guang_Novak的博客 417

Farey Sequence POJ - 2478(欧拉函数)

题目地址:http://poj.org/problem?id=2478 FnFn-1相比多了与小于n并且n互质的组合,增加的个数为phi(i),Fn实则是欧拉函数的前缀和。打表,求前缀和即可。 #include<stdio.h> #include<math.h> #include<algorithm> #include<iostream> #inc...

呼吸 195

POJ - Farey Sequence(欧拉函数&前缀和)

题目链接:http://poj.org/problem?id=2478Time Limit:1000MSMemory Limit:65536K Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 &l...

ityanger的技术栈 2694

POJ 2478 Farey Sequence(欧拉函数)

/*对于每一个输入的n,结果输出为 从2开始的欧拉函数值一直加到n的欧拉函数值, 为了对于不同的n,这个累加的过程不再重复, 我们将累加的结果存储在数组a之中*/ /*注意,由于是累加,因此累加后的结果很大,最终 要求的答案也可能很大,超出int的范围,因此要用long long*/ #include #include using namespace std; const int

Hot_Summer_Days的专栏 763

poj2478-Farey Sequence】递推求欧拉函数-欧拉函数的几个性质和推论

poj2478-Farey Sequence】递推求欧拉函数-欧拉函数的几个性质和推论 http://poj.org/problem?id=2478 题意:给定一个数x,求&lt;=x的数的欧拉函数值的和。(x&lt;=10^6) 题解:数据范围比较大,像poj1248一样的做法是不可行的了。 首先我们要了解欧拉函数的几个性质和推论:(今天...

qq_25768269的博客 261

POJ2478 Farey Sequence欧拉函数

题目链接 Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b) = 1 arranged in increasing order. The first few are F2 = {1/2} F3 = {1/3, 1/2, 2/3} F4 = {1/4,

kitalekita的博客 321

poj2478 Farey Sequence(法雷级数+欧拉函数式素数

http://poj.org/problem?id=2478 题意:求第n项的法雷级数是多少。 思路:法雷级数,百度百科就可以知道后一项和前一项的差值就是与该数互质的数的个数,因为如果不互质就约分变成其他数了。求小于n与n互质的数的个数正是欧拉函数的作用,但是这题数据量大,需要打个表再判断。打表也不是普通的打表,不是那种先打个素数表,然后再来一次打欧拉表,最后打法雷级表,时间应该超

Iguodala的博客 605
上一篇: POJ 2407 Relatives 解题报告(欧拉函数水题)
下一篇: POJ 3358 Period of an Infinite Binary Expansion 解题报告(欧拉函数+因式分解)
SF-_-
博客等级 码龄14年 28粉丝 227原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值