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. Redis集群规范

    Redis集群规范 什么是 Redis 集群 分布式(distributed) 容错(fault-tolerant) 是普通单机 Redis 所能使用的功能的一个子集(subset). 集群的容错功能 ...

  2. 使用nvm进行node多版本管理

    nvm与Python的virtualenv和Ruby的rvm类似.NVM (Node Version Manager,Node多版本管理器)是一个通用的叫法,它目前有许多不同的实现.通常我们说的 nv ...

  3. mipmap和drawable文件夹的区别

    在Android上创建工程,会默认创建mipmap文件夹.之前在Eclipse上创建的是drawable的文件夹.那么这两个有什么区别呢? 问题: I'm working with android s ...

  4. ios mac 对照片进行JPEG压缩

    ios mac 对照片进行JPEG压缩 1. 在iOS上可以使用 API UIImageJPEGRepresentation 对照片数据进行JPEG压缩: 我们知道iOS其实是MAC OS 的移植,那 ...

  5. java 除法运算只保留整数位的3种方式

      1.情景展示 根据提供的毫秒数进行除法运算,如果将毫秒数转换成小时,小时数不为0,则只取整数位,依此类推... 2.情况分析 可以使用3个函数实现 Math.floor(num)  只保留整数位 ...

  6. Linux编译步骤概述

    Linux,一切皆文件! linux环境下,编译源码文件步骤总结 01.下载解压 一遍在开源网站有download/下载页面 02.安装基本编译环境 yum install -y gcc gcc-c+ ...

  7. Effective Java 第三版——61. 基本类型优于装箱的基本类型

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  8. SNF开发平台WinForm之十五-时间轴控件使用-SNF快速开发平台3.3-Spring.Net.Framework

    一.显示效果如下: 二.在控件库里选择UCTimeAxis 拖拽到窗体里. 三.加入以下代码,在load事件里进行调用就可以运行了. #region 给时间轴控件加载数据 private void U ...

  9. 如何禁止VS显示“You have mixed tabs and spaces. Fix this?”

    如何禁止VS显示“You have mixed tabs and spaces. Fix this?” VS2013 版本的解决方案: Vs2013  IDE下,编辑C++的工程源码,在打开文件的时候 ...

  10. Communications link failure due to underlying exception异常处理(转)

    最近的一个项目在Hibernate使用C3P0的连接池,数据库为Mysql.开发测试没有问题,在运行中每个一段长的空闲时间就出现异常: java 代码 org.hibernate.exception. ...