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. cf 442 div2 F. Ann and Books(莫队算法)

    cf 442 div2 F. Ann and Books(莫队算法) 题意: \(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\) 每次查询区间\([l,r]内有多少对(i, ...

  2. js正则表达式,判断字符串是否以数字组结尾,并取出结尾的数字

    js正则表达式,判断字符串是否以数字组结尾,并取出结尾的数字 <!DOCTYPE html> <html> <head> <meta charset=&quo ...

  3. HDU2647 topsort

    Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...

  4. 使用 Rational AppScan 保证 Web 应用的安全性,第 2 部分: 使用 Rational AppScan 应对 Web 应用攻击

    1 当前 Web 安全现状 互联网的发展历史也可以说是攻击与防护不断交织发展的过程.目前,全球因特网用户已达 13.5 亿,用户利用网络进行购物.银行转账支付和各种软件下载,企业用户更是依赖于互联网构 ...

  5. vijos 1907 DP+滚动数组

    描述 Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了水管或者掉在地上的话,便宣告 ...

  6. aio 爬虫,去重,入库

    #aio 爬虫,去重,入库 import asyncio import aiohttp import aiomysql import re from pyquery import PyQuery st ...

  7. CentOS 7 vim显示中文乱码

    使用xshell的时候,发现有时候中文显示有乱码,一开始以为是Xshell没设置好,后来检查了一下xshell<<文件<<属性<<终端:右侧编码,显示的是Unico ...

  8. js监听浏览器后退事件

    $(document).ready(function(e) {             var counter = 0;            if (window.history && ...

  9. python调参神器hyperopt

    一.安装 pip install hyperopt 二.说明 Hyperopt提供了一个优化接口,这个接口接受一个评估函数和参数空间,能计算出参数空间内的一个点的损失函数值.用户还要指定空间内参数的分 ...

  10. python进行EDA探索性数据分析

    1.查看数据的类型概况 cols = [c for c in train.columns]   #返回数据的列名到列表里 print('Number of features: {}'.format(l ...