POJ2478(SummerTrainingDay04-E 欧拉函数)

Farey Sequence

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16927   Accepted: 6764

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]

求1-n的欧拉函数和即可。

 1 //2017-08-04
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6
 7 using namespace std;
 8
 9 const int N = 1000010;
10 int phi[N],prime[N],tot;
11 long long ans[N];
12 bool book[N];
13
14 void getphi()//线性欧拉函数筛
15 {
16    int i,j;
17    phi[1]=1;
18    for(i=2;i<=N;i++)//相当于分解质因式的逆过程
19    {
20        if(!book[i])
21        {
22             prime[++tot]=i;//筛素数的时候首先会判断i是否是素数。
23             phi[i]=i-1;//当 i 是素数时 phi[i]=i-1
24         }
25        for(j=1;j<=tot;j++)
26        {
27           if(i*prime[j]>N)  break;
28           book[i*prime[j]]=1;//确定i*prime[j]不是素数
29           if(i%prime[j]==0)//接着我们会看prime[j]是否是i的约数
30           {
31              phi[i*prime[j]]=phi[i]*prime[j];break;
32           }
33           else  phi[i*prime[j]]=phi[i]*(prime[j]-1);//其实这里prime[j]-1就是phi[prime[j]],利用了欧拉函数的积性
34        }
35    }
36 }
37
38 int main()
39 {
40     int n;
41     getphi();
42     ans[2] = 1;
43     for(int i = 3; i < N; i++){
44         ans[i] = ans[i-1]+phi[i];
45     }
46     while(scanf("%d", &n) && n){
47         printf("%lld\n", ans[n]);
48     }
49
50     return 0;
51 }
时间: 2024-10-26 07:54:58

POJ2478(SummerTrainingDay04-E 欧拉函数)的相关文章

poj2478 欧拉函数水题

poj2478 欧拉函数水题 Y - Farey Sequence Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2478 Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational number

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

poj2478欧拉函数

打表欧拉函数,求2到n的欧拉函数和 #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cassert> #include<iomanip> #include<cstdlib> #include<c

poj2478 (欧拉函数)

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

筛法求欧拉函数(poj2478

求1-n的欧拉函数的值 #include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(long lo

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

链接: https://vjudge.net/problem/POJ-2478 题意: 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 =

欧拉函数

void Euler_Sieve_Method(int * euler, int n) { euler[1] = 1; for (int i = 2; i < n; i++) { euler[i] = i; } for (int i = 2; i < n; i++) { if (euler[i] == i) { for (int j = i; j < n; j += i) { euler[j] = euler[j] / i * (i - 1); } } } } void Euler_Si

hdu1695(莫比乌斯)或欧拉函数+容斥

题意:求1-b和1-d之内各选一个数组成数对,问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个可以简化成1-b/k 和1-d/k 的互质有序数对的个数.假设b=b/k,d=d/k,b<=d.欧拉函数可以算出1-b与1-b之内的互质对数,然后在b+1到d的数i,求每个i在1-b之间有多少互质的数.解法是容斥,getans函数参数的意义:1-tool中含有rem位置之后的i的质因子的数的个数. 在 for(int j=rem;j<=factor[i

欧拉函数常用性质

欧拉函数定义:设n 为正整数,则1,2......,n中与n互质的整数个数记作f(n). 1.1 若n为素数,f(n)=n-1; 1.2 整数n=p*q,p,q为不同素数,则f(n)=f(p)*f(q)=(p-1)*(q-1) 1.3 n=p^a*q^b,f(n)=f(p^a)*f(q^b)=n*(1-1/p)*(1-1/q) 1.4 分解质因子相乘,f(n)=n*(1-1/p1)*(1-1/p2)*.......*(1-1/pk). f(100)=f(2^2*5^2)=100*1/2*4/5=