zoukankan      html  css  js  c++  java
  • 杭电2824--The Euler function(欧拉函数)

    The Euler function

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4559    Accepted Submission(s): 1902


    Problem Description
    The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful characteristics. Here comes a very easy question: suppose you are given a, b, try to calculate (a)+ (a+1)+....+ (b)
     

     

    Input
    There are several test cases. Each line has two integers a, b (2<a<b<3000000).
     

     

    Output
    Output the result of (a)+ (a+1)+....+ (b)
     

     

    Sample Input
    3 100
     

     

    Sample Output
    3042
     

     

    Source
     

     

    Recommend
    gaojie   |   We have carefully selected several similar problems for you:  2818 2825 2817 2822 2821

    欧拉函数是指:对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) 。

    通式:φ(x)=x*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有质因数,x是不为0的整数。φ(1)=1(唯一和1互质的数就是1本身)。

    对于质数p,φ(p) = p - 1。注意φ(1)=1.

    欧拉定理:对于互质的正整数a和n,有aφ(n) ≡ 1 mod n。

    欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n)。

                                     若n是质数p的k次幂,φ(n)=p^k-p^(k-1)=(p-1)p^(k-1),因为除了p的倍数外,其他数都跟n互质。

    特殊性质:当n为奇数时,φ(2n)=φ(n)

    欧拉函数还有这样的性质:

    设a为N的质因数,若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N / a) * a;若(N % a == 0 && (N / a) % a != 0) 则有:E(N) = E(N / a) * (a - 1)。

     
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    const int N = 3000010;
    int prime[N], isprime[N];
    int phi[N];
    void Get_phi()
    {
        int i, j, cnt = 0;
        for(i = 2; i < N; i++){
            if(isprime[i] == 0){
                prime[cnt++] = i;
                phi[i] = i-1;
            }    
            for(j = 0; j < cnt && i*prime[j] < N; j++){
                isprime[i*prime[j]]=1;
                if(i%prime[j]==0)
                    phi[i*prime[j]]=phi[i]*prime[j];
                else
                    phi[i*prime[j]]=phi[i]*(prime[j]-1);
            }
         }     
    }  
    int main()
    {
        int a, b;
        Get_phi();
        while(~scanf("%d %d", &a, &b))
        {
            long long sum = 0;
            for(int i = a; i <= b; i++)
                sum += phi[i];
            cout << sum << endl;
        }
        return 0;
    }
     
  • 相关阅读:
    Java练习题
    循环练习
    Java String类详解
    Collections类
    TreeSet类
    IOS基础:NSTimer类的使用
    IOS基础:深入理解Objectivec中@class的含义
    IOS基础:retain,copy,assign及autorelease
    IOS基础:ActionSheet(上拉菜单)的实现
    Xcode 模拟器生成目标目录位置查找
  • 原文地址:https://www.cnblogs.com/soTired/p/4764321.html
Copyright © 2011-2022 走看看