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

    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

    2009 Multi-University Training Contest 1 - Host by TJU

    Recommend

    gaojie

    思路:

    欧拉函数的定义:

    φ(n)为小于n且与n互质的数的个数

    欧拉函数有一个公式

    其中pi为x的质因子,x不等于0

    由此,可以用类似求素数的筛法

    样例代码(来自百度百科):

    /*线性筛O(n)时间复杂度内筛出maxn内欧拉函数值*/
    int m[maxn],phi[maxn],p[maxn],pt;         //m[i]是i的最小素因数,phi[i]是i的欧拉函数的值,p是素数,pt是素数个数
     
    int make()
    {
        phi[1]=1;
        int N=maxn;
        int k;
        for(int i=2;i<N;i++)
        {
            if(!m[i])//i是素数
                p[pt++]=m[i]=i,phi[i]=i-1;
            for(int j=0;j<pt&&(k=p[j]*i)<N;j++)
            {
                m[k]=p[j];
                if(m[i]==p[j])//为了保证以后的数不被再筛,要break
                {
                    phi[k]=phi[i]*p[j];
    /*这里的phi[k]与phi[i]后面的∏(p[i]-1)/p[i]都一样(m[i]==p[j])只差一个p[j],就可以保证∏(p[i]-1)/p[i]前面也一样了*/
                    break;    
                }
                else
                    phi[k]=phi[i]*(p[j]-1);//积性函数性质,f(i*k)=f(i)*f(k)
            }
        }
    }

    下面的是A题代码

    #include<bits/stdc++.h>
    using namespace std;
    
    int ola[3000001];//存储欧拉函数的值
    int prime[216817];
    bool isprime[3000001];
    
    int main()
    {
        __int64 n, a, b, i , j, k = 0;
        for(i = 2; i < 3000001; i++)
        {
            if(!isprime[i])
            {
                ola[i] = i-1;
                prime[++k] = i;
            }
            for(j = 1; j <= k && prime[j]*i < 3000001; j++)
            {
                isprime[prime[j] * i] = 1;
                if(i % prime[j] == 0)
                {
                    ola[prime[j]*i] = ola[i] * prime[j];
                    break;
                }
                else ola[prime[j] * i] = ola[i] * (prime[j]-1);
            }
        }
        while(cin >> a >> b)
        {
            __int64 ans = 0;
            while(a <= b)
            {
                ans += ola[a++];
            }
            cout << ans << endl;
        }
        return 0;
    }
  • 相关阅读:
    各IDE快捷键
    java的GUI之SWT框架 JavaFX框架 配置开发环境(包含但不限于WindowBuilder完整教程,解决Unknown GUI toolkit报错,解决导入SWT包错误)
    20180314 一个浮点数问题
    20180309 算最近新的感悟吧
    20171228 C#值类型和引用类型
    20171129 ASP.NET中使用Skin文件
    20171123初学demo爬去网页资料
    20171018 在小程序页面去获取用户的OpenID
    20171018 微信小程序客户端数据和服务器交互
    20171012 动态爬虫爬取预约挂号有号信息
  • 原文地址:https://www.cnblogs.com/liuzhanshan/p/6285713.html
Copyright © 2011-2022 走看看