Farey Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15242   Accepted: 6054

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

  1. 2
  2. 3
  3. 4
  4. 5
  5. 0

Sample Output

  1. 1
  2. 3
  3. 5
  4. 9
    思路:欧拉函数打表。
  1. #include <cstdio>
  2. using namespace std;
  3. const int MAXN=;
  4. long long euler[MAXN];
  5. void sieve()
  6. {
  7. for(int i=;i<MAXN;i++) euler[i]=i;
  8. for(int i=;i<MAXN;i+=) euler[i]/=;
  9. for(int i=;i<MAXN;i+=)
  10. {
  11. if(euler[i]==i)
  12. {
  13. for(int j=i;j<MAXN;j+=i)
  14. {
  15. euler[j]=euler[j]*(i-)/i;
  16. }
  17. }
  18. }
  19. for(int i=;i<MAXN;i++)
  20. {
  21. euler[i]+=euler[i-];
  22. }
  23. }
  24. int main()
  25. {
  26. sieve();
  27. int n;
  28. while(scanf("%d",&n)!=EOF&&n!=)
  29. {
  30. printf("%lld\n",euler[n]);
  31. }
  32. return ;
  33. }

POJ2478(欧拉函数)的更多相关文章

  1. poj2478欧拉函数

    打表欧拉函数,求2到n的欧拉函数和 #include<map> #include<set> #include<cmath> #include<queue> ...

  2. POJ2478 Farey Sequence —— 欧拉函数

    题目链接:https://vjudge.net/problem/POJ-2478 Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K To ...

  3. poj2478(欧拉函数)

    题目链接:https://vjudge.net/problem/POJ-2478 题意:给定n,输出集合中元素的数量,集合中的元素为最简小于1的分数,分子分母均属于[1,n-1]. 思路:理清题意后就 ...

  4. poj2478 Farey Sequence (欧拉函数)

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

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

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

  6. poj2478 Farey Sequence 欧拉函数的应用

    仔细看看题目,按照题目要求 其实就是 求 小于等于n的 每一个数的 欧拉函数值  的总和,为什么呢,因为要构成 a/b 然后不能约分  所以 gcd(a,b)==1,所以  分母 b的 欧拉函数值   ...

  7. POJ2478(SummerTrainingDay04-E 欧拉函数)

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16927   Accepted: 6764 D ...

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

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

  9. poj-2478 Farey Sequence(dp,欧拉函数)

    题目链接: Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14230   Accepted:  ...

随机推荐

  1. ubuntu 14.04 建立wifi热点

    昨天突然想起来我可以用笔记本搞一个热点这样我的手机就不用上流量了,但是手机死活搜不到建好的信号,目前的解决方案如下: 直接用ubuntu自带的创建wifi网络功能是不好使的,因为android系统不支 ...

  2. Codeforces Round #447 (Div. 2) C 构造

    现在有一个长度为n的数列 n不超过4000 求出它的gcd生成set 生成方式是对<i,j> insert进去(a[i] ^ a[i+1] ... ^a[j]) i<=j 然而现在给 ...

  3. 通过案例说明struts2的工作流程

    本文主要是通过一个例子来说明Struts2的一个工作流程. 首先定义一个登录页面login.jsp: [java] view plaincopy <%@ page language=" ...

  4. 智课雅思词汇---二十三、名词性后缀mony

    智课雅思词汇---二十三.名词性后缀mony 一.总结 一句话总结:Latin: action, result of an action or condition; a suffix that for ...

  5. XamlParseException异常

    一般出现System.Windows.Markup.XamlParseException的错误是由1.dll库加载错误,查询一下你程序中引用的dll你是否加载并引用到程序内.2.程序中引用的文件(tx ...

  6. cassandra框架模型之二——存储机制 CommitLog MemTable SSTable

    四.副本存储 Cassandra不像HBase是基于HDFS的分布式存储,它的数据是存在每个节点的本地文件系统中. Cassandra有三种副本配置策略: 1) SimpleStrategy (Rac ...

  7. QT延时方法

    (转自:http://blog.sina.com.cn/s/blog_613cfe940100kacm.html) 1. void sleep(unsigned int msec){    QTime ...

  8. C# List 排序

    (转自:http://www.cnblogs.com/bradwarden/archive/2012/06/19/2554854.html) 第一种:实体类实现IComparable接口,而且必须实现 ...

  9. 【第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛-L】用来作弊的药水

    链接:https://www.nowcoder.com/acm/contest/90/L来源:牛客网 输入x,a,y,b,(1<=x,a,y,b<=10^9)判断x^a是否等于y^b 前面 ...

  10. Codeforces Round #394 (Div. 2) A. Dasha and Stairs

    A. Dasha and Stairs time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...