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

看几个例子可以发现,后一个总是比前一个多这次的数与之前的数互质的个数,用欧拉就可以,因为是1e6,所以要用筛法的,不能用直接的;

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define pb push_back
#define mm(a,b) memset((a),(b),sizeof(a))
#include<vector>
typedef long long ll;
typedef long double ld;
const ll mod=1e9+7;
using namespace std;
const double pi=acos(-1.0);
ll euler[1000005];
void chuli()
{
euler[1]=1;
for(int i=2;i<1000001;i++)
euler[i]=i;
for(int i=2;i<1000001;i++)
if(euler[i]==i)
for(int j=i;j<1000001;j+=i)
euler[j]=euler[j]/i*(i-1);//先进行除法是为了防止中间数据的溢出
for(int i=3;i<1000001;i++)
euler[i]=euler[i]+euler[i-1];
} int main()
{
//freopen("output1.txt", "r", stdin);
chuli();
int n;
while(1)
{
sf("%d",&n);
if(n==0) return 0;
pf("%lld\n",euler[n]);
}
}

H - Farey Sequence的更多相关文章

  1. Farey Sequence

    Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rationa ...

  2. poj2478 Farey Sequence (欧拉函数)

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

  3. POJ 2478 Farey Sequence

     名字是法雷数列其实是欧拉phi函数              Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  4. POJ 2478 Farey Sequence(欧拉函数前n项和)

    A - Farey Sequence Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  5. The h.264 Sequence Parameter Set

    转债:  http://www.cardinalpeak.com/blog/the-h-264-sequence-parameter-set/ View from the Peak The h.264 ...

  6. POJ2478 - Farey Sequence(法雷级数&&欧拉函数)

    题目大意 直接看原文吧.... The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rat ...

  7. UVA12995 Farey Sequence

    UVA12995 Farey Sequence 欧拉函数 同仪仗队那题几乎相同,本质都是求欧拉函数的和 #include<cstdio> #define N 1000000 ],i,j,t ...

  8. Farey Sequence (素筛欧拉函数/水)题解

    The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/ ...

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

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

随机推荐

  1. open jdk

    http://rednaxelafx.iteye.com/blog/1549577 https://www.jianshu.com/p/f98c3acd8df8 http://download.jav ...

  2. 解决Ubuntu Chrome浏览器很卡不响应的问题

    1. 设定字体,使用Ubuntu Tweak Tool把系统字体设定为默认字体,而不是文泉驿字体: 2. 使用ADBLock Plus把垃圾的广告过滤掉,不然网页上很多Flash就会导致网页非常的卡顿 ...

  3. 用MATLAB生成模糊控制离线查询表

    实时采样得到的数据经过模糊化处理后输入机器,通过查询模糊规则表便可得到应有的输出模糊量,从而避免了近似推理过程.实际应用中,特别是在控制系统较为简单而采用单片机控制时,常常采用这种查表法. 模糊控制表 ...

  4. 使用robot_pose_ekf对传感器信息融合

    robot_pose_ekf是ROS Navigation stack中的一个包,通过扩展卡尔曼滤波器对imu.里程计odom.视觉里程计vo的数据进行融合,来估计平面移动机器人的真实位置姿态,输出o ...

  5. 获取代理电脑的https证书方法

    1.打开fiddler,tools->fiddler options 勾选check for certificate revocation 2.手机打开浏览器 输入fiddler所在电脑ip及端 ...

  6. Swift udp实现根据端口号监听广播数据(利用GCDAsyncUdpSocket实现)

    有个小需求,app需要监听pc广播的数据: 代码实现思路: 使用三方库:CocoaAsyncSocket 1.开启udp监听: udpSocket.beginReceiving() 2.读取udp的数 ...

  7. PHP 扩展开发之Zephir

    最近对代码进行性能分析后,发现两个耗时的地方:自动加载文件数太多:参数验证函数调用超过1000次.这也是许多php语言框架面临的问题,所以发展出来诸如Yaf,Swoole,Phalcon这些C语言扩展 ...

  8. Java代码里利用Fiddler抓包调试设置

    Fiddler启动时已经将自己注册为系统的默认代理服务器,应用程序在访问网络时会去获取系统的默认代理,如果需要捕获java访问网络时的数据,只需要在启动java程序时设置代理服务器为Fiddler即可 ...

  9. Android Launcher分析和修改3——Launcher启动和初始化

    前面两篇文章都是写有关Launcher配置文件的修改,代码方面涉及不多,今天开始进入Launcher代码分析. 我们开机启动Launcher,Launcher是由Activity Manager启动的 ...

  10. 【30集iCore3_ADP出厂源代码(ARM部分)讲解视频】30-13 emWin底层驱动接口介绍

    视频简介:该视频介绍emWin底层驱动接口. 源视频包下载地址:链接:http://pan.baidu.com/s/1nvPpC2d 密码:cbb7 银杏科技优酷视频发布区:http://i.youk ...