POJ2478 Farey Sequence

Farey Sequence

Time Limit: 1000MS   Memory Limit: 65536K
     

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:[email protected]

欧拉函数模板题,16ms的O(n)线性筛,代码是抄贾教的。。

Codes:

 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 int n,phi[1001000],tot,prime[500000];
 5 long long sum[1001000];
 6 bool check[1000100];
 7 void PHI(int n){
 8     phi[1] = 1;
 9     for(int i=2;i<=n;i++){
10         if(!check[i]){
11             prime[++tot] = i;
12             phi[i] = i - 1;
13         }
14         for(int j=1;j<=tot;j++){
15             if(prime[j]*i>n) break;
16             check[prime[j]*i] = true;
17             if(i%prime[j]==0){
18                 phi[i*prime[j]] = phi[i] * prime[j];
19                 break;
20             }else   phi[i*prime[j]] = phi[i] * (prime[j]-1);
21         }
22     }
23 }
24
25 int main(){
26     PHI(1000000);
27     sum[2] = 1;
28     for(int i=3;i<=1000000;i++) sum[i]+=sum[i-1] + phi[i];
29     while(scanf("%d",&n)!=EOF && n) cout<<sum[n]<<endl;
30     return 0;
31 }

POJ2478 Farey Sequence

时间: 2024-10-11 21:45:58

POJ2478 Farey Sequence的相关文章

poj-2478 Farey Sequence(dp,欧拉函数)

题目链接: Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14230   Accepted: 5624 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

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,

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(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,

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( 欧拉函数 + 法雷数列 )

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

【poj2478】Farey Sequence

题意: 就是求2~n的所有欧拉函数值的和,这里就用到了快速求欧拉函数的方法.(不能暴力求了,不然必定TLE啊) 说说欧拉筛法,感觉十分机智啊~~ 对于上述代码的几个问题: 1.问:为什么i%prime==0时break?   答:欧拉筛法每次合成时都是用最小质因子合成的,如果我们在程序加一行记录,即可先行求出1~Maxn的最小质因子.这样避免不必要的重复,提高效率. 2.为什么当i%prime[j]==0时,phi[i*prime[j]]=phi[i]*prime[j]? 否则,phi[i*pr