题目链接:https://vjudge.net/problem/POJ-2478

Farey Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17753   Accepted: 7112

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

题解:

单纯的欧拉函数。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+; int euler[MAXN];
void getEuler()
{
memset(euler, , sizeof(euler));
euler[] = ;
for(int i = ; i<MAXN; i++) if(!euler[i]) {
for(int j = i; j<MAXN; j += i)
{
if(!euler[j]) euler[j] = j;
euler[j] = euler[j]/i*(i-);
}
}
} LL f[MAXN];
void init()
{
getEuler();
f[] = ;
for(int i = ; i<MAXN; i++)
f[i] = f[i-] + euler[i];
} int main()
{
int n;
while(scanf("%d", &n)&&n)
printf("%lld\n", f[n]);
}

POJ2478 Farey Sequence —— 欧拉函数的更多相关文章

  1. poj2478 Farey Sequence (欧拉函数)

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

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

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

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

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

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

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

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

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

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

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

  7. POJ-2478-Farey Sequence(欧拉函数)

    链接: https://vjudge.net/problem/POJ-2478 题意: The Farey Sequence Fn for any integer n with n >= 2 i ...

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

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

  9. POJ2478 - Farey Sequence(法雷级数&&欧拉函数)

    题目大意 直接看原文吧.... The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rat ...

随机推荐

  1. CentOS配置sshd

    用SSH来远程管理计算机,就不用到计算机实际地点来回跑了 环境:服务器:CentOS6.6,客户机win8.1 putty 配置服务器: 1.检查SSHD是否安装(默认情况下是系统自带的),使用命令 ...

  2. OSI-ISO 七层协议通信模型

  3. BZOJ2527Meteors

    BZOJ2527 整体二分模板题 整体二分: 主要用于解决第K大问题 #include<cstdio> #include<cctype> #include<vector& ...

  4. C#深入学习:泛型修饰符in,out、逆变委托类型和协变委托类型

    在C#中,存在两个泛型修饰符:in和out,他们分别对应逆变委托和协变委托. 我们知道,在C#中要想将一个泛型对象转换为另一个泛型对象时,必须要将一个泛型对象拆箱,对元素进行显式或隐式转换后重新装箱. ...

  5. java后4位打成*显示

    /** * [固定电话] 后四位,其他隐藏<例子:****1234> * * @param num * @return */ public static String fixedPhone ...

  6. 机器学习(十三)——机器学习中的矩阵方法(3)病态矩阵、协同过滤的ALS算法(1)

    http://antkillerfarm.github.io/ 向量的范数(续) 范数可用符号∥x∥λ表示. 经常使用的有: ∥x∥1=|x1|+⋯+|xn| ∥x∥2=x21+⋯+x2n−−−−−− ...

  7. 安装 - LNMP一键安装包

    https://lnmp.org/ 系统需求: CentOS/RHEL/Fedora/Debian/Ubuntu/Raspbian Linux系统 需要5GB以上硬盘剩余空间 需要128MB以上内存( ...

  8. VMware安装黑群暉5.2

      选择典型就可以了,点击下一步. 选择 稍后安装操作系统,点击下一步. 客户机操作系统选择Linux,版本选择其他Linux2.6.x内核64位, 填写虚拟机名称和虚拟机文件保存位置的.填写好后点击 ...

  9. es6 对象浅拷贝的2种方法

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  10. Eoeclient源代码分析---SlidingMenu的使用

    Eoeclient源代码分析及代码凝视 使用滑动菜单SlidingMenu,单击滑动菜单的不同选项,能够通过ViewPager和PagerIndicator显示相应的数据内容. 0  BaseSlid ...