Hdu2824 (Euler's function)

Source: Internet
Author: User
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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.