Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12802   Accepted: 4998

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
/**
题意:求F[i] 的元素的个数,并且0<a<b<=i, gcd(a,b)=1
做法:欧拉 用phi[n]存储小于等于n的素数的个数,然后f[i] = phi[i] + f[i]
**/
#include <iostream>
#include <string.h>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#define maxn 1000100
using namespace std;
long long mmap[maxn];
int phi[maxn];
long long n;
void geteuler()
{
memset(phi,,sizeof(phi));
phi[] = ;
for(int i=; i<=maxn; i++)
{
if(!phi[i])
{
for(int j=i; j<=maxn; j+=i)
{
if(!phi[j])
phi[j] = j;
phi[j] = phi[j]/i*(i-);
}
}
}
}
int main()
{
geteuler();
mmap[] = ;
for(int i=; i<=maxn; i++)
{
mmap[i] = mmap[i-] + phi[i];
}
//freopen("in.txt","r",stdin);
while(~scanf("%lld",&n))
{
if(n == ) break;
printf("%lld\n",mmap[n]);
}
return ;
}

POJ - 2478的更多相关文章

  1. 欧拉函数 &【POJ 2478】欧拉筛法

    通式: $\phi(x)=x(1-\frac{1}{p_1})(1-\frac{1}{p_2})(1-\frac{1}{p_3}) \cdots (1-\frac{1}{p_n})$ 若n是质数p的k ...

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

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

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

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

  4. POJ 2478 欧拉函数打表的运用

    http://poj.org/problem?id=2478 此题只是用简单的欧拉函数求每一个数的互质数的值会超时,因为要求很多数据的欧拉函数值,所以选用欧拉函数打表法. PS:因为最后得到的结果会很 ...

  5. poj 3090 &amp;&amp; poj 2478(法雷级数,欧拉函数)

    http://poj.org/problem?id=3090 法雷级数 法雷级数的递推公式非常easy:f[1] = 2; f[i] = f[i-1]+phi[i]. 该题是法雷级数的变形吧,答案是2 ...

  6. POJ 2478 Farey Sequence

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

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

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

  8. 【欧拉函数】 poj 2478

    递推法求欧拉函数: #include <iostream> #include <cstdio> #include <cstring> using namespace ...

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

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

随机推荐

  1. [Leetcode] Binary tree inorder traversal二叉树中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  2. 洛谷 P3527 [POI2011]MET-Meteors 解题报告

    P3527 [POI2011]MET-Meteors 题意翻译 \(\tt{Byteotian \ Interstellar \ Union}\)有\(N\)个成员国.现在它发现了一颗新的星球,这颗星 ...

  3. POJ.1287 Networking (Prim)

    POJ.1287 Networking (Prim) 题意分析 可能有重边,注意选择最小的边. 编号依旧从1开始. 直接跑prim即可. 代码总览 #include <cstdio> #i ...

  4. HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)

    HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和ric ...

  5. some of the properties associated with the solution could not be read解决方法

    基于TFS管理的解决方案打开时提示:“some of the properties associated with the solution could not be read”,并不影响项目加载,O ...

  6. POI 2018.10.27

    [POI2015]LOG 维护一个长度为n的序列,一开始都是0,支持以下两种操作:1.U k a 将序列中第k个数修改为a.2.Z c s 在这个序列上,每次选出c个正数,并将它们都减去1,询问能否进 ...

  7. k好数 数位dp

    问题描述 如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数.求L位K进制数中K好数的数目.例如K = 4,L = 2的时候,所有K好数为11.13.20.22 ...

  8. 002.比较vector对象是否相等

    1.使用vector模板 //编写一段程序,比较vector对象是否相等 //注:该例类似于一个[彩票游戏] #include <iostream> #include <ctime& ...

  9. HDU2833 最短路 floyd

    WuKong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  10. array_value

    <?php $a=array("Name"=>"Bill","Age"=>"60","Cou ...