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 = 0;
    for( int i = 2; i <= N; ++i )
    {
        if( !vis[i] )
        {
            prime[ cnt++ ] = i;
            phi[i] = i - 1;
        }
        for( int j = 0; j < cnt; ++j )
        {
            if( i * prime[j] > N ) break;
            vis[ i * prime[j] ] = 1;
            if( i % prime[j] == 0 ){
                phi[ i * prime[j] ] = phi[i] * prime[j];
                break;
            }
            else
                phi[ i * prime[j] ] = phi[i] * ( prime[j] - 1 );
        }
    }
}

void cal()
{
    for( int i = 3; i < MAXN; ++i )
        phi[i] += phi[i - 1];
}

int main()
{
    get_phi_prime( MAXN );
    cal();
    while( ~scanf( "%d", &n ) && n )
    {
        printf( "%lld\n", phi[n] );
    }
    return 0;
}

代码君

时间: 2024-10-05 21:58:54

POJ 2478 Farey Sequence( 欧拉函数 + 法雷数列 )的相关文章

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

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(欧拉函数)

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 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(欧拉函数)

链接: 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 =

POJ 2154 Color (ploya欧拉函数)

ploya定理,然后公式利用欧拉函数优化,gcd必然是因子,这样只要枚举因子,每个因子利用欧拉函数计算出现次数 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int t, n, p; int pow_mod(int x, int k) { x %= p; int ans = 1; while (k) { if (k&1) ans = ans *

POJ 2407 Relatives(欧拉函数入门题)

Relatives Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz. Input There are several t