POJ 2478-Farey Sequence (Euler's function of Evaluate Method)

Source: Internet
Author: User

POJ 2478-Farey Sequence (Euler's function of Evaluate Method)

 

Farey Sequence Time Limit:1000 MS Memory Limit:65536KB 64bit IO Format:% I64d & % I64u Submit Status Practice POJ 2478 Appoint description: System Crawler)

Description

The Farey Sequence Fn for any integer n with n> = 2 is the set of irreducible rational numbers a/B with 0 <a <B <= n and gcd (a, B) = 1 arranged in increasing order. the first few are
F2 ={ 1/2}
F3 = {1/3, 1/2, 2/3}
F4 = {1/4, 1/3, 1/2, 2/3, 3/4}
F5 = {1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5}

You task is to calculate the number of terms in the Farey sequence Fn.

Input

There are several test cases. each test case has only one line, which contains a positive integer n (2 <= n <= 10 6 ). there are no blank lines between cases. A line with a single 0 terminates the input.

Output

For each test case, you shoshould output one line, which contains N (n) ---- the number of terms in the Farey sequence Fn.

Sample Input

23450

Sample Output

1359

 

Given a number of n, calculate the number of real scores composed of two or more numbers that are less than or equal to n.

Idea: This blog has reasoning about this question ----> click to open the link

 

#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include #include 
       
        #include 
        
         #include 
         
          #include
          using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);LL phi[1000010];LL res[1000010];void Euler(){ int i,j; memset(phi,0,sizeof(phi)); phi[1]=1; for(i=2;i<=1000010;i++) { if(!phi[i]) { for(j=i;j<=1000010;j+=i) { if(!phi[j]) phi[j]=j; phi[j]=phi[j]/i*(i-1); } } }}int main(){ int n; Euler(); memset(res,0,sizeof(res)); res[1]=res[2]=1; for(int i=3;i<1000010;i++) res[i]=res[i-1]+phi[i]; while(~scanf("%d",&n)){ if(!n) break; printf("%lld\n",res[n]); } return 0;}
         
        
       
      
     
    
   
  
 


 

Related Article

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.