The Euler function
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3674 Accepted Submission(s):
1509
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
总结:最初,我用sum[i]数组存储1到i的欧拉函数值得和,但是因为开了两个数组,超了空间,本想用此法优化时间的,结果还是要从a循环到b取每个数的欧拉函数的值相加
1 #include<stdio.h> 2 #include<string.h> 3 __int64 euler[3000000]; 4 int main() 5 { 6 __int64 ans; 7 memset(euler,0,sizeof(euler)); 8 euler[1] = 1; 9 int a,b,i,j; 10 for(i = 2; i <3000000; i++) 11 { 12 if(!euler[i]) 13 for(j = i; j <3000000; j += i) 14 { 15 if(!euler[j]) 16 euler[j] = j; 17 euler[j] = euler[j]/i*(i-1); 18 } 19 } 20 while(scanf("%d%d",&a,&b)!=EOF) 21 { 22 ans=0; 23 for(i=a; i<=b; i++) 24 ans+=euler[i]; 25 printf("%I64d ",ans); 26 } 27 return 0; 28 }