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

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 <= 106). 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

Source

POJ Contest,Author:Mathematica@ZSU
 #include <iostream>
#include <cstring>
#include <cstdio> using namespace std; typedef unsigned long long int LL; LL sum[],phi[]; void phi_table()
{
phi[]=;
for(int i=;i<=;i++)if(!phi[i])
{
for(int j=i;j<=;j+=i)
{
if(!phi[j]) phi[j]=j;
phi[j]=phi[j]/i*(i-);
}
}
sum[]=;
for(int i=;i<=;i++)
{
sum[i]=sum[i-]+phi[i];
}
} int main()
{
phi_table();
int n;
while(scanf("%d",&n)!=EOF&&n)
{
printf("%llu\n",sum[n]);
}
return ;
}

POJ 2478 Farey Sequence的更多相关文章

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

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

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

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

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

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

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

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

  5. 欧拉函数 & 【POJ】2478 Farey Sequence & 【HDU】2824 The Euler function

    http://poj.org/problem?id=2478 http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数模板裸题,有两种方法求出所有的欧拉函 ...

  6. Farey Sequence(欧拉函数板子题)

    题目链接:http://poj.org/problem?id=2478 Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  7. Farey Sequence (欧拉函数+前缀和)

    题目链接:http://poj.org/problem?id=2478 Description The Farey Sequence Fn for any integer n with n >= ...

  8. POJ - 2478

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12802   Accepted: 4998 D ...

  9. POJ2478 Farey Sequence

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15023   Accepted: 5962 Description The ...

随机推荐

  1. Java.utils.Collections学习

    阅读类库代码是有意义的,尤其是Java集合类框架以及算法Collections Arrays都是值得阅读的, 一来可以减少新手程序员的编码的工作量,二来,对于常见的需求,程序员应该先找下是否有现成的类 ...

  2. Mysql完全手册(笔记一,底层与内置函数)

    1.MySQL由五个主子系统组成.协同工作,这五个主子系统是: (1)查询引擎 (2)存储管理器 (3)缓冲管理器 (4)事务管理器 (5)恢复管理器 查询引擎: 这个子系统包含三个相互关联的部件: ...

  3. StackExchange.Redis通用封装类分享(转)

    阅读目录 ConnectionMultiplexer 封装 RedisHelper 通用操作类封 String类型的封装 List类型的封装 Hash类型的封装 SortedSet 类型的封装 key ...

  4. 学习web前端学习路程

    学习路程: 1.HTML和CSS基础 2.JavaScript语言 3.jQuery 4.综合网站实践 5.优化及调试

  5. JQuery对id中含有特殊字符的转义处理

    转载--http://www.jb51.net/article/41192.htm <div id="a[]">kkkkkk</div> <scrip ...

  6. bzoj4498: 魔法的碰撞

    首先,如果排列确定,那么就可以组合学解决了,不过排列数很多,显然不能枚举. 我们发现如果区间不能重叠的话,总长度减去所有区间长度就是能用的多余格子数. 然而相邻区间可以重叠较小区间一半的长度,因此这些 ...

  7. Thinking in java学习笔记之final

  8. java高新技术-可变参数与OverLoad相关面试题分析

    可变参数 可变参数的特点: 只能出现在参数列表的最后: ...位于变量类型和变量名之间,前后有无空格都可以: 调用可变参数的方法时,编译器为该可变参数隐含创建一个数组,在方法中以数组的形式访问可变参数 ...

  9. BZOJ 1078: [SCOI2008]斜堆

    1078: [SCOI2008]斜堆 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 770  Solved: 422[Submit][Status][ ...

  10. Java Web的传值汇总(含JavaBean)

    前言: 其实JavaBean就像asp.net MVC上的Model传值一样.