一、题目

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 <= 10 6). 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

二、题意分析

题意就是给你一个范围内的正整数N,让你去用1~N的数字去组合成Farey序列。关于Farey序列,依题意可知,就是1~N的数字中互素的a,b,其中a<b,就可以凑成一个a/b。然后问有多少个不同的a/b。

我们先看2,就一个,看3,发现2有的3肯定有,然后其余的就是与3互质的数与3凑成的a/b。再看4,3有的还是有,然后再加上与4互质的数与4凑成的a/b。依次递推下去。就是欧拉函数的前N项和。用一个数组累加保存下来,就是所有结果了,再用线性筛法去求欧拉函数(可以看我之前的欧拉函数学习笔记),速度绝对够。需要注意的是,结果增长的很快,需要用long long。

三、AC代码

  

#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 1e6+5;
int Phi[MAXN], Prime[MAXN], nPrime;
long long Ans[MAXN]; void Euler()
{
memset(Phi, 0, sizeof(Phi));
Phi[1] = 1;
nPrime = 0;
for(int i = 2; i < MAXN; i++)
{
if(!Phi[i]) //i为素数
{
Phi[i] = i - 1;
Prime[nPrime++] = i;
}
for(int j = 0; j < nPrime && (long long)i*Prime[j] < MAXN; j++)
{
if(i%Prime[j])
{ Phi[ i*Prime[j] ] = Phi[i]*(Prime[j]-1);
}
else
{
Phi[ i*Prime[j] ] = Phi[i]*Prime[j];
break;
}
}
}
return;
} void solve()
{
Euler();
Ans[2] = Phi[2];
for(int i = 3; i < MAXN; i++)
{
Ans[i] = Ans[i-1] + Phi[i];
}
return;
} int main()
{
int N;
solve();
while(cin>>N && N)
{
cout << Ans[N] << endl;
}
return 0;
}

  

POJ_2478 Farey Sequence 【欧拉函数+简单递推】的更多相关文章

  1. poj2478 Farey Sequence (欧拉函数)

    Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...

  2. POJ2478 Farey Sequence —— 欧拉函数

    题目链接:https://vjudge.net/problem/POJ-2478 Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K To ...

  3. hdu1787 GCD Again poj 2478 Farey Sequence 欧拉函数

    hdu1787,直接求欧拉函数 #include <iostream> #include <cstdio> using namespace std; int n; int ph ...

  4. poj 2478 Farey Sequence(欧拉函数是基于寻求筛法素数)

    http://poj.org/problem?id=2478 求欧拉函数的模板. 初涉欧拉函数,先学一学它主要的性质. 1.欧拉函数是求小于n且和n互质(包含1)的正整数的个数. 记为φ(n). 2. ...

  5. poj2478 Farey Sequence 欧拉函数的应用

    仔细看看题目,按照题目要求 其实就是 求 小于等于n的 每一个数的 欧拉函数值  的总和,为什么呢,因为要构成 a/b 然后不能约分  所以 gcd(a,b)==1,所以  分母 b的 欧拉函数值   ...

  6. UVA12995 Farey Sequence [欧拉函数,欧拉筛]

    洛谷传送门 Farey Sequence (格式太难调,题面就不放了) 分析: 实际上求分数个数就是个幌子,观察可以得到,所求的就是$\sum^n_{i=2}\phi (i)$,所以直接欧拉筛+前缀和 ...

  7. poj 2478 Farey Sequence 欧拉函数前缀和

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K       Description The Farey Sequence Fn for ...

  8. uva 11426 线性欧拉函数筛选+递推

    Problem J GCD Extreme (II) Input: Standard Input Output: Standard Output Given the value of N, you w ...

  9. Poj 2478-Farey Sequence 欧拉函数,素数,线性筛

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14291   Accepted: 5647 D ...

随机推荐

  1. Extend volumn in ubuntu 14.04

    运行环境: ubuntu 14.04, VMware12.5.7 1. VMware上点击 虚拟机->设置->硬盘(SCSI)->扩展选项,设置自己希望的ubuntu磁盘运行空间大小 ...

  2. Python pandas.DataFrame调整列顺序及修改index名

    1. 从字典创建DataFrame >>> import pandas >>> dict_a = {'],'mark_date':['2017-03-07','20 ...

  3. win10手动开启wifi

    win+R键,输入cmd,以管理员身份运行,输入netsh wlan set hostednetwork mode=allow ssid=wifi key=wifimima123回车 解释一下: ss ...

  4. win32多线程 (六)信号量 Synchronization

    比如:多个人来取仓库物品,我们为了做到多线程的互斥,一种方法是,对每个货品加个mutex互斥锁.另种方法,对窗口(只一个)加个mutex锁,但这样的话效率都比较低.而另钟解决办法是用信号量.信号量其实 ...

  5. grid search

    sklearn.metrics.make_scorer(score_func, greater_is_better=True, needs_proba=False, needs_threshold=F ...

  6. pip指定安装包版本

    pip是目前最好用的python包关系工具,没有之一. 1.版本范围符号:==.>=.<=.>.<. 2.单个安装示例: 1 2 3 pip install cchardet  ...

  7. django: django rest framework 分页

    django: django rest framework 分页 2018年06月22日 13:41:43 linux_player_c 阅读数:665更多 所属专栏: django 实战   版权声 ...

  8. 专题2-通过按键玩中断\第1课-中断处理流程深度剖析-lesson1

    中断概念 1.中断生命周期 串口先产生一个事件,该事件传送到中断控制器里面,中断控制器会进行相应过滤,能通过过滤,那么就交给CPU去处理. 2.中断源 2440芯片手册 6410芯片手册 3.中断过滤 ...

  9. vitamio MediaController总是显示在底部的问题

    前面一直用腾讯的x5 tas来播放视频,但是体验效果不好,不能设置播放页,无法获取用户对视频的学习情况,百度了下,发现好多人在使用vitamio,最新版本是5.0的,下载可能要花费点时间,官网上竟然没 ...

  10. SQL Server Profiler (SQl跟踪器)的简单使用

    一.工具介绍 在实际开发中,我们的数据库应用系统因为不可避免会存在有大量表,视图,索引,触发器,函数,存储过程,sql语句等,所以会出现一系列问题,有问题不可怕,找对工具很重要.接下来我就介绍一款性能 ...