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
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

欧拉函数模板题,不能开两个数组,会超时。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long LL;
const int N =3000005;
LL euler[N];
//LL sum[N];
void getEuler()
{
    memset(euler,0,sizeof(euler));
    euler[1] = 1;
    for(int i = 2; i <= N; i++){
        if(!euler[i])
            for(int j = i; j <= N; j+= i)
            {
                if(!euler[j])
                    euler[j] = j;
                euler[j] = euler[j]/i*(i-1);
            }
        //sum[i]=sum[i-1]+(LL)euler[i];
    }
}

int main()
{
    getEuler();
    int a,b;
    while(~scanf("%d%d",&a,&b)){
        LL sum = 0;
        for(int i=a;i<=b;i++){
            sum+=euler[i];
        }
        printf("%lld\n",sum);
    }
    return 0;
}
时间: 2024-10-22 00:22:32

hdu 2824(欧拉函数)的相关文章

hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不会 就自己写了个容斥搞一下(才能维持现在的生活) //别人的题解https://blog.csdn.net/luyehao1/article/details/81672837 #include <iostream> #include <cstdio> #include <cstr

hdu 3307(欧拉函数+好题)

Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1071    Accepted Submission(s): 323 Problem Description an = X*an-1 + Y and Y mod (X-1) = 0.Your task is to cal

HDU 1286 欧拉函数。

[科普]什么是BestCoder?如何参加? 找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8077    Accepted Submission(s): 4250 Problem Description 新年快到了,"猪头帮协会"准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是

hdu 4983(欧拉函数)

Goffi and GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 992    Accepted Submission(s): 336 Problem Description Goffi is doing his math homework and he finds an equality on his text book: g

hdu 2588 欧拉函数

两个数的gcd为d,其实就是将这两个数同除以d后互质.本题中n是固定的,x是小于等于n的数,很容易想到可以枚举n的约数求出(n除以约数)的欧拉函数的和即是答案. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 typedef long long ll; 7 8 int euler_phi( int n ) 9 { 10 int ans =

hdu 3501 欧拉函数

容易想到容斥原理,但是结合欧拉函数的公式,我们得到: 小于n且与n互质的数的和为:n * phi(n) / 2 于是问题迎刃而解. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 typedef long long ll; 7 const int MOD = 1000000007; 8 9 int euler_phi( int n ) 10

hdu 1787(欧拉函数)

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

hdu 4002 欧拉函数

题意:求1-n内最大的x/phi(x) 通式:φ(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本身). 因此含质因数最多的即为所求,打表求出前n个积,之后找到比自己小的最大积 大数打表 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #i

hdu 4002 欧拉函数 2011大连赛区网络赛B

题意:求1-n内最大的x/phi(x) 通式:φ(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本身). 因此含质因数最多的即为所求,打表求出前n个积,之后找到比自己小的最大积 大数打表 2015-07-27:到此一游 1 #include<cstdio> 2 #include<iostream> 3 #include<al