The Euler function
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3817 Accepted Submission(s): 1580
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)
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
/* HODJ 2824 欧拉函数 欧拉函数的定义:E(k)=([1,n-1]中与n互质的整数个数). (a为N的质因数) 若(N%a==0 && (N/a)%a==0) 则有:E(N)=E(N/a)*a; 若(N%a==0 && (N/a)%a!=0) 则有:E(N)=E(N/a)*(a-1); */ #include<iostream> #include<stdio.h> #include<algorithm> using namespace std; #define N 3000001 int phi[N]; void phi_table() { int i,j; memset(phi,0,sizeof(phi)); phi[1]=1; for(i=2;i<=N;i++) { if(!phi[i]) { for(j=i;j<=N;j+=i) { if(!phi[j]) phi[j]=j; phi[j]=phi[j]/i*(i-1); } } } } int main() { int i,a,b; __int64 ans; phi_table(); //freopen("test.txt","r",stdin); while(scanf("%d%d",&a,&b)!=EOF) { ans=0; for(i=a;i<=b;i++) ans+=(__int64)phi[i]; printf("%I64d\n",ans); } return 0; }