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. NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第一轮Day2题解

    上星期打的...题有点水,好多人都AK了 T1排个序贪心就好了 #include<iostream> #include<cstring> #include<cstdlib ...

  2. 四连测Day2

    题目:链接: https://pan.baidu.com/s/1ef_9hGBhczW0B4dz5IUKmw 密码: qgjy T1: hash后直接二分查询即可 #include<iostre ...

  3. 【计数】【UVA11401】 Triangle Counting

    传送门 Description 把1……n这n个数中任取3个数,求能组成一个三角形的方案个数 Input 多组数据,对于每组数据,包括: 一行一个数i,代表前i个数. 输入结束标识为i<3. O ...

  4. 解决无法安装cnpm,cnpm卡顿问题

    # 注册模块镜像 npm set registry https://registry.npm.taobao.org # node-gyp 编译依赖的 node 源码镜像 npm set disturl ...

  5. BigDATA面试题

    Big Data 面试题总结 JAVA相关 1-1)List 与set 的区别? 老掉牙的问题了,还在这里老生常谈:List特点:元素有放入顺序,元素可重复 ,Set特点:元素无放入顺序,元素不可重复 ...

  6. HDU 5650 异或

    so easy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  7. hadoop设置公平队列

    http://hadoop.apache.org/docs/r1.2.1/fair_scheduler.html fair-scheduler.xml文档 <?xml version=" ...

  8. floor ceil

    echo floor(11/10); echo ceil(11/10); swiper  可以用在ajax的success中,如果用ejs拼接的,放ajax里面才可以轮播

  9. C# 生成订单号的几种方式

    public class RandomNumber { public static object _lock = new object(); ; public string GetRandom1() ...

  10. Getting Private/Public IP address of EC2 instance using AWS-cli [closed]

    For private IP address: aws ec2 describe-instances --instance-ids i-b78a096f | grep PrivateIpAddress ...