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 descriptionthe 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)
Inputthere are several test cases. Each line has two integers A, B (2 <A <B <3000000 ).
Outputoutput the result of (a) + (a + 1) +... + (B)
Samples input3 100
Summary of sample output3042: At first, I used the sum [I] array to store the Euler's functions from 1 to I. However, because of the fact that two arrays are opened, the space is exceeded, if I want to use this method to optimize the time, the result is still to get the sum of values of the Euler function of each number from a loop to 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\n",ans);26 }27 return 0;28 }View code