Farey Sequence
Time Limit: 1000MS   Memory Limit: 65536K
     

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

Source

POJ Contest,Author:Mathematica@ZSU

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. using namespace std;
  5. #define ll long long
  6. #define esp 1e-13
  7. const int N=1e3+,M=1e6+,inf=1e9+,mod=;
  8. ll p[M],ji;
  9. bool vis[M];
  10. ll phi[M];
  11. ll sum[M];
  12. void get_eular(int n)
  13. {
  14. ji = ;
  15. memset(vis, true, sizeof(vis));
  16. for(int i = ; i <= n; i++)
  17. {
  18. if(vis[i])
  19. {
  20. p[ji ++] = i;
  21. phi[i] = i - ;
  22. }
  23. for(int j = ; j < ji && i * p[j] <= n; j++)
  24. {
  25. vis[i * p[j]] = false;
  26. if(i % p[j] == )
  27. {
  28. phi[i * p[j]] = phi[i] * p[j];
  29. break;
  30. }
  31. else
  32. phi[i * p[j]] = phi[i] * phi[p[j]];
  33. }
  34. }
  35. }
  36. int main()
  37. {
  38. int x,i;
  39. get_eular(M);
  40. memset(sum,,sizeof(sum));
  41. for(i=;i<=1e6;i++)
  42. sum[i]=sum[i-]+phi[i];
  43. while(~scanf("%d",&x))
  44. {
  45. if(!x)break;
  46. printf("%lld\n",sum[x]);
  47. }
  48. return ;
  49. }

poj 2478 Farey Sequence 欧拉函数前缀和的更多相关文章

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

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

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

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

  3. POJ2478 Farey Sequence —— 欧拉函数

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

  4. poj2478 Farey Sequence (欧拉函数)

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

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

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

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

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

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

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

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

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

  9. Poj 2478-Farey Sequence 欧拉函数,素数,线性筛

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14291   Accepted: 5647 D ...

随机推荐

  1. Cocos2d-x Lua中多场景切换生命周期

    在多个场景切换时候,场景的生命周期会更加复杂.这一节我们介绍一下场景切换生命周期.多个场景切换时候分为几种情况:情况1,使用pushScene函数从实现GameScene场景进入SettingScen ...

  2. 1714 B君的游戏(Nim博弈)

    1714 B君的游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 B君和L君要玩一个游戏.刚开始有n个正整数  ai  . 双方轮流操作.每次操作,选一个正整数 ...

  3. Quartz实现定时功能

    ---------------------------------博主讲废话 在自己实现爬取某个网站的信息后,发现,如果要自己每次把程序跑一遍不太现实(麻烦),所以有没有什么可以实现 定时的功能,只要 ...

  4. MySql最左前缀原则

    简单整理记录下,之前一直都没有关注过这个问题 最左前缀原则:顾名思义是最左优先,以最左边的为起点任何连续的索引都能匹配上, 注:如果第一个字段是范围查询需要单独建一个索引 注:在创建多列索引时,要根据 ...

  5. [转载]mvc:view-controller

    1.重定向 ? 1 <mvc:view-controller path="/" view-name="redirect:/admin/index"/> ...

  6. LeetCode-day05

    45. Single Number 在个数都为2的数组中找到个数为1的数 46. Missing Number 在数组中找到从0到n缺失的数字 47. Find the Difference 找两个字 ...

  7. gearman background后台job状态获取

    GearmanClient background job有一个方法叫: public array GearmanClient::jobStatus ( string $job_handle ) Get ...

  8. python基础知识——包

    包是一种通过使用“模块名”来组织python模块的名称空间的方式. 无论是import形式还是from...import形式,凡是在导入语句中(不是在使用时)遇到带点的,就需要意识到——这是包. 包是 ...

  9. Vue:实践学习笔记(3)——组件使用

    Vue:实践学习笔记(3)——组件使用 全局注册 1.注册组件 Vue.component('my-component',{ //选项 }) 说明:my-component就是注册的组件自定义的标签名 ...

  10. rails 增删改查

    class InvoicesController < ApplicationController def index @invoices = Invoice.all end def show @ ...