HDOJ The Euler function 2824【欧拉函数】

The Euler function

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4389    Accepted Submission(s): 1821

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   |   We have carefully selected several similar problems for you:  2818 2825 2822 2821 2820

打表求欧拉函数值

注意数组开成long long 型

第一种:稍微快一点

#include <stdio.h>
#include <math.h>
#include <vector>
#include <queue>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define maxn 3000000+100

using namespace std;

long long E[maxn];

void euler()
{
    E[1]=1;
    for(int i=2;i<maxn;i++)
        E[i]=i;
    for(int i=2;i<maxn;i++){
        if(E[i]==i)
        for(int j=i;j<maxn;j+=i){
            E[j]=E[j]/i*(i-1);
        }
    }
    for(int i=2;i<maxn;i++)
        E[i]+=E[i-1];
}
int main()
{
    euler();
    int a,b;
    while(scanf("%d%d",&a,&b)!=EOF)
        printf("%lld\n",E[b]-E[a-1]);
    return 0;
}

第二种

#include <stdio.h>
#include <math.h>
#include <vector>
#include <queue>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define maxn 3000000+100

using namespace std;

long long E[maxn];

void euler()
{
    for(int i=2;i<maxn;i++){
        if(!E[i])
        for(int j=i;j<maxn;j+=i){
            if(!E[j])E[j]=j;
            E[j]=E[j]/i*(i-1);
        }
        E[i]+=E[i-1];
    }
}

int main()
{
    euler();
    int a,b;
    while(scanf("%d%d",&a,&b)!=EOF)
        printf("%lld\n",E[b]-E[a-1]);
    return 0;
}

版权声明:本文为博主原创文章,转载请注明出处。

时间: 2024-10-07 11:48:54

HDOJ The Euler function 2824【欧拉函数】的相关文章

hdu2824 The Euler function(欧拉函数个数)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数性质: 1:(百科):http://baike.baidu.com/link?url=r-yneKCCyS9N6bhbQCqiZX0V2OCYq9r7iHSzHTSs03H7qRvu1OfUzlOxfVEs2PmR 2:http://www.cppblog.com/doer-xee/archive/2009

The Euler function(欧拉函数筛)

这题用欧拉函数会超时,要用函数筛. 解析:(转) 定义:对于正整数n,φ(n)是小于或等于n的正整数中,与n互质的数的数目. 例如:φ(8)=4,因为1,3,5,7均和8互质. 性质:1.若p是质数,φ(p)= p-1. 2.若n是质数p的k次幂,φ(n)=(p-1)*p^(k-1).因为除了p的倍数都与n互质 3.欧拉函数是积性函数,若m,n互质,φ(mn)= φ(m)φ(n). 根据这3条性质我们就可以推出一个整数的欧拉函数的公式.因为一个数总可以写成一些质数的乘积的形式. E(k)=(p1

HDU2824 The Euler function(欧拉函数)

题目求φ(a)+φ(a+1)+...+φ(b-1)+φ(b). 用欧拉筛选法O(n)计算出n以内的φ值,存个前缀和即可. φ(p)=p-1(p是质数),小于这个质数且与其互质的个数就是p-1: φ(p*a)=(p-1)*φ(a)(p是质数且p|a),因为欧拉函数是积性函数,φ(p*a)=φ(p)*φ(a): φ(p*a)=p*φ(a)(p是质数且p不能整除a),不知怎么理解.. 1 #include<cstdio> 2 #include<cstring> 3 using names

杭电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 theo

The Euler function(欧拉函数)

The Euler function Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 39   Accepted Submission(s) : 19 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description The Euler function

HDOJ 1787 GCD Again(欧拉函数)

GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2611    Accepted Submission(s): 1090 Problem Description Do you have spent some time to think and try to solve those unsolved problem a

hdu 2824(欧拉函数)

The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5235    Accepted Submission(s): 2225 Problem Description The Euler function phi is an important kind of function in number theo

HDOJ GCD Again 1787【欧拉函数】

GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2625    Accepted Submission(s): 1099 Problem Description Do you have spent some time to think and try to solve those unsolved problem a

hdoj 1787 GCD Again【欧拉函数】

GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2673    Accepted Submission(s): 1123 Problem Description Do you have spent some time to think and try to solve those unsolved problem af

HDOJ 3501 Calculation 2(欧拉函数拓展——求非互质数和)

Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2548    Accepted Submission(s): 1064 Problem Description Given a positive integer N, your task is to calculate the sum of the posit