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. IntelliJ IDEA 使用总结

    IntelliJ IDEA 使用总结 http://my.oschina.net/xianggao/blog/97539 IntelliJ IDEA 问题解决:1.乱码,主要是快捷键的字样显示乱码 中 ...

  2. 转换primitive主数据类型

    /*转换primitive主数据类型 * 短变长直接转换 * 长变短要强制转换 * 例如:long y = 42; int x = (int)y * String类型转换成primitive类型时可以 ...

  3. jQuery 实现菜单

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. 如何在程序里模拟在cmd里用管理员权限运行一条指令

    转自csdn的yangw150,zhao4zhong1 转自http://www.cnblogs.com/del/archive/2008/02/13/1068229.html http://blog ...

  5. Eclipse for php + Xdebug搭建PHP的调试环境

    Xdebug安装教程 下载Xdebug 首先我们需要下载Xdebug,务必根据安装的PHP版本,选择合适的Xdebug版本,由于我是在Windows环境下安装PHP的,所以选择下载Windows版本的 ...

  6. hibernate 的 CascadeType 属性

    hibernateintegerstringfloatclassmerge @Entity @Table(name="orders")public class Order { // ...

  7. 用VS2010+Qt4.6.4编译QtAV

    http://blog.csdn.net/trustguan/article/details/45623891 如果在链接的过程中,出现以上错误: 1>MSVCRTD.lib(MSVCR100D ...

  8. Fragment开发计划

    Fragment是什么 Fragment正如字面意思所言是碎片,所以这是一个管理碎片时间的应用程序.目前考虑的是先在Android上实现,如果IOS的合作伙伴靠谱可以交给他做,如果不靠谱就等Andro ...

  9. MVC 多级目录菜单

    MVC多级目录菜单  ----- 简单模拟 Model ---- cs { public class Class1 { public int ID{get;set;} public int paren ...

  10. nginx+gunicorn+supervisor+flask @ centos

    /etc/nginx/conf.d/default.conf server { listen 80 default_server; server_name 127.0.0.1; #charset ko ...