POJ - 2478 Farey Sequence(phi打表)

题目:

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 <= 10 6). 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

这个题目无非就是求欧拉函数的前n项和呗

因为查询不知道有多少个,所以要先打表。

代码:

#include<iostream>
#include<stdio.h>
using namespace std;

long long phi[1000001];		//前i个数的phi之和

void get_phi()
{
	for (int i = 1; i <= 1000000; i++)phi[i] = i;
	for (int i = 2; i <= 1000000; i++)
	{
	if (phi[i] == i)for (int j = i; j <= 1000000; j += i)phi[j] = phi[j] / i*(i - 1);
	phi[i] += phi[i - 1];	//去掉这一行就是求欧拉函数
	}
}

int main()
{
	get_phi();
	int n;
	while (scanf("%d",&n))
	{
		if (n == 0)break;
		printf("%llu\n", phi[n] - 1);
	}
	return 0;
}
时间: 2024-10-16 12:39:23

POJ - 2478 Farey Sequence(phi打表)的相关文章

POJ 2478 Farey Sequence

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,

POJ 2478 Farey Sequence 筛选法求欧拉函数

题目来源:POJ 2478 Farey Sequence 题意:输入n 求 phi(2)+phi(3)+phi(4)+...+phi(n) 思路:用类似筛法的方式计算phi(1), phi(2), ..., phi(n) 再求前缀和 #include <cstdio> #include <cstring> #include <cmath> //欧拉phi函数 const int maxn = 1000010; typedef long long LL; int eule

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

http://poj.org/problem?id=2478 求欧拉函数的模板. 初涉欧拉函数,先学一学它基本的性质. 1.欧拉函数是求小于n且和n互质(包括1)的正整数的个数.记为φ(n). 2.欧拉定理:若a与n互质,那么有a^φ(n) ≡ 1(mod n),经常用于求幂的模. 3.若p是一个质数,那么φ(p) = p-1,注意φ(1) = 1. 4.欧拉函数是积性函数: 若m与n互质,那么φ(nm) = φ(n) * φ(m). 若n = p^k且p为质数,那么φ(n) = p^k - p

POJ 2478 Farey Sequence( 欧拉函数 + 法雷数列 )

POJ 2478 Farey Sequence ( 欧拉函数 + 法雷数列 ) #include <cstdio> #include <cstring> using namespace std; #define MAXN 1000005 typedef long long LL; int vis[ MAXN ], prime[ MAXN ], cnt, n; LL phi[ MAXN ]; void get_phi_prime( int N ) { phi[1] = 1; cnt

poj 2478 Farey Sequence(欧拉函数)

Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13204   Accepted: 5181 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)

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

题意 求欧拉函数的前n项和 水题 打表筛选即可 #include <iostream> #include <math.h> using namespace std; long long a[1000005]={0}; long long c[1000005]={0}; void enlur() { int i,j; for(i=2;i<1000005;i++) { if(!a[i]) { for(j=i;j<1000005;j=j+i) { if(!a[j]) a[j]

POJ 2478 欧拉函数打表的运用

http://poj.org/problem?id=2478 此题只是用简单的欧拉函数求每一个数的互质数的值会超时,因为要求很多数据的欧拉函数值,所以选用欧拉函数打表法. PS:因为最后得到的结果会很大,所以结果数据类型不要用int,改为long long就没问题了 #include <iostream> #include <stdio.h> using namespace std; #define LL long long LL F[1000100]; int phi[10001

欧拉函数 &amp; 【POJ】2478 Farey Sequence &amp; 【HDU】2824 The Euler function

http://poj.org/problem?id=2478 http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数模板裸题,有两种方法求出所有的欧拉函数,一是筛法,而是白书上的筛法. 首先看欧拉函数的性质: 欧拉函数是求小于n且和n互质(包括1)的正整数的个数.记为φ(n). 欧拉定理:若a与n互质,那么有a^φ(n) ≡ 1(mod n),经常用于求乘法逆元. 若p是一个质数,那么φ(p) = p-1,注意φ(1) = 1. 欧拉函数是积性函数

Farey Sequence POJ - 2478 (欧拉函数 前缀和)

Farey Sequence POJ - 2478 题目链接:https://vjudge.net/problem/POJ-2478 题目: 法理序列Fn是指对于任意整数n( n >= 2),由不可约的分数a/b(0 < a < b <= n),gcd(a,b) = 1升序排列构成的序列,最开始的几个如下 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,