HDU - 2824 The Euler function

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

题意:求n到m的欧拉函数的值的和

思路:欧拉函数预处理然后遍历相加
AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
#define ll long long
const int N = 3000010 ;
int phi[N], prime[N];
int tot;
void Euler()
{
    phi[1] = 1;
    for(int i = 2; i < N; i ++)
{
        if(!phi[i])
        {
            phi[i] = i-1;
            prime[tot ++] = i;
        }
        for(int j = 0; j < tot && 1ll*i*prime[j] < N; j ++)
        {
            if(i % prime[j]) phi[i * prime[j]] = phi[i] * (prime[j]-1);
            else
            {
                phi[i * prime[j] ] = phi[i] * prime[j];
                break;
            }
        }
    }
}

int main()
{
    int n,m;
    Euler();
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ll t=0;
    for(int i=n;i<=m;i++)
    {
        t+=phi[i];
    }
    printf("%lld\n",t);
    }
}
发布了261 篇原创文章 · 获赞 14 · 访问量 7422

猜你喜欢

转载自blog.csdn.net/weixin_43244265/article/details/104005793