The Euler function
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3284 Accepted Submission(s): 1350
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
Recommend
欧拉函数....
代码:
1 #include<stdio.h> 2 #include<string.h> 3 const int maxn=3000001; 4 __int64 phi[maxn]; 5 int main() 6 { 7 int st,en,i,j; 8 for(i=1;i<maxn;i++) 9 phi[i]=i; 10 for(i=2;i<maxn;i+=2) 11 phi[i]/=2; 12 for(i=3;i<maxn;i+=2) 13 { 14 if(phi[i]==i) 15 { 16 for(j=i;j<maxn;j+=i) 17 { 18 phi[j]=phi[j]/i*(i-1); /*from left to right*/ 19 } 20 } 21 } 22 while(scanf("%d%d",&st,&en)!=EOF) 23 { 24 25 __int64 ans=0; 26 for(i=st;i<=en;i++) 27 { 28 ans+=phi[i]; 29 } 30 printf("%I64d ",ans); 31 } 32 33 return 0; 34 }