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 <= 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


#include<stdio.h>
#include<string.h>
__int64 e[1000005],ans[1000005];
void fun() ///筛法求
{
int i,j;
for(i=2;i<1000005;i++)
if(!e[i])
for(j=i;j<1000005;j+=i)
{
if(!e[j])
e[j]=j;
e[j]=e[j]-e[j]/i;
}
for(i=1;i<1000005;i++)
ans[i]=ans[i-1]+e[i];
}
int main()
{
int n;
fun();
while(~scanf("%d",&n)&&n)
printf("%I64d\n",ans[n]);
}

Farey Sequence的更多相关文章

  1. poj2478 Farey Sequence (欧拉函数)

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

  2. POJ 2478 Farey Sequence

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

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

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

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

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

  5. H - Farey Sequence

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

  6. UVA12995 Farey Sequence

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

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

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

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

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

  9. poj2478——Farey Sequence(欧拉函数)

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18507   Accepted: 7429 D ...

随机推荐

  1. Android的学习——ubuntu下android5.1源码的make编译

    在repo sync下载源码后,经历了漫长的时间,终于可以进行下一步了. 在进行make之前还需要三个步骤. 1> source build/envsetup.sh:加载命令          ...

  2. matplotlib画图保存

    import numpy as np import matplotlib.pyplot as plt xData = np.arange(0, 10, 1) yData1 = xData.__pow_ ...

  3. java SE 常用的排序算法

    java程序员会用到的经典排序算法实现 常用的排序算法(以下代码包含的)有以下五类: A.插入排序(直接插入排序.希尔排序) B.交换排序(冒泡排序.快速排序) C.选择排序(直接选择排序.堆排序) ...

  4. JS中 obj.style.left 与 obj.offsetLeft 的区别

    1.obj.style.left 取得是字符串:28px; 而 obj.offsetLeft取得的值为数字 28: 2.obj.style.left 需要事先将left值写在HTML中,不能致谢在CS ...

  5. 不能将 Null 值赋给类型为 (不可为 null 的值类型)的成员。解决方法

    一般代码没有错,是对应的数据库里有的字段是NULL,不是主键,主键肯定不会是NULL的.是其他字段. 把这些列的NULL赋值.

  6. 关于char类型的连续输入

    这个忘了好久了 先回想吧 一 单字符 ①  char m; scanf("%d",&m); ② char m; m=getchar(); putchar(m); 二 字符数 ...

  7. 《C与指针》第二章练习

    本章问题 1.Comments in C do not nest(嵌套).What would be the result of "commenting out" the code ...

  8. JSP知识点汇总

    有几种方法可以实现服务器内部跳转? 使用request对象提供的方法:request.getRequestDispatcher(String URI).forward(ServletRequest r ...

  9. windows多线程相关

    1.多线程同步的方法 a)entercirticalsection leaveciriticalsection b)Mutex互斥对象 waitforsingleobject releasemutex ...

  10. unbuntu server (linux系统)下面安装 lamp

    1.sudo apt-get update 2.sudo apt-get install apache2 3.检查是否安装成功: apache2 -v 4.sudo apt-get update ph ...