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

2
3
4
5
0

Sample Output

1
3
5
9
思路:欧拉函数打表。
#include <cstdio>
using namespace std;
const int MAXN=;
long long euler[MAXN];
void sieve()
{
for(int i=;i<MAXN;i++) euler[i]=i;
for(int i=;i<MAXN;i+=) euler[i]/=;
for(int i=;i<MAXN;i+=)
{
if(euler[i]==i)
{
for(int j=i;j<MAXN;j+=i)
{
euler[j]=euler[j]*(i-)/i;
}
}
}
for(int i=;i<MAXN;i++)
{
euler[i]+=euler[i-];
}
}
int main()
{
sieve();
int n;
while(scanf("%d",&n)!=EOF&&n!=)
{
printf("%lld\n",euler[n]);
}
return ;
}

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. tech| kafka入门书籍导读

    J梳理了一下自己在入门 kafka 时读过的一些书, 希望能帮助到对 kafka 感兴趣的小伙伴. 涉及到的书籍: kafka 权威指南 Kafka: The Definitive Guide (ka ...

  2. win7 VC6.0 安装 fatal error C1083: Cannot open include file: 'Iphlpapi.h': No such file or directory

    解决方法:   第一步 安装SDK 第二步 将包含的api放在第一项

  3. linux rpm 卸载,简单说明

    平时Linux卸载文件总是遇到卸载不干净,各种依赖什么的,今天又是搞这玩意,就记录下一个比较常规的方法. 一.查询包括某关键字的软件(这里以卸载openoffice为例) 查询包括office的软件 ...

  4. C++(十六) — 类中引用成员函数、命名空间的使用

    1.为什么类中引用成员函数? 类将属性和方法做了封装.类是一种数据类型,也就是:固定大小内存块的别名. 类的定义是一个抽象的概念,定义时不分配内存,当用类定义对象时,才分配一个固定大小的内存块. 此时 ...

  5. 十二 web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录—获取Scrapy框架Cookies

    模拟浏览器登录 start_requests()方法,可以返回一个请求给爬虫的起始网站,这个返回的请求相当于start_urls,start_requests()返回的请求会替代start_urls里 ...

  6. Flask安装配置

    倒腾了一下午了,还是不太顺利,顺便记录一下. 硬件环境:win8.1 64位 + python2.7.9 32位 安装easy_install 需要先下载ez_setup.py(需要复制该链接中的内容 ...

  7. LeetCode OJ:Compare Version Numbers(比较版本字符串)

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  8. 条款2:尽量以const enum inline 来替换 #define

    这里说的意思其实相当于,宁可以用编译器来替换预处理器 因为使用预处理器可能使得被处理过的东西无法进入符号表,例如 #define MAXLEN 16 这里的MAXLEN并没有进入符号表,这样有编译错误 ...

  9. LeetCode OJ:Three Sum(三数之和)

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  10. HData——ETL 数据导入/导出工具

    HData是一个异构的ETL数据导入/导出工具,致力于使用一个工具解决不同数据源(JDBC.Hive.HDFS.HBase.MongoDB.FTP.Http.CSV.Excel.Kafka等)之间数据 ...