# 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

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;
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);
}
}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 