poj-2478 Farey Sequence (DP, Euler function)

Source: Internet
Author: User

Topic Links:

Farey Sequence

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14230 Accepted: 5624

Description

The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers A/b with 0 < a < a <= 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 is several test cases. The which contains a positive integer n (2 <= n <= 106). There is no blank lines between cases. A line with a single 0 terminates the input.

Output

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

Sample Input

23450

Sample Output

1359
Test instructions: Q meet A/b with 0 < a < b <= N and gcd (A, b) = 1, number of pairs there;
Idea: Euler function of dp[i]=dp[i-1]+n;
AC Code:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intn=1e6+2;Long LongDp[n],a[n];intget_a () {memset (a),0,sizeof(a));  for(intI=2; i<n;i++)    {        if(!A[i]) {             for(intj=i;j<n;j+=i) {if(!a[j]) a[j]=J; A[J]=a[j]/i* (I-1); }        }    }}intFun () {get_a (); dp[1]=0; dp[2]=1;  for(intI=3; i<n;i++) {Dp[i]=dp[i-1]+A[i]; }}intMain () {intN;    Fun ();  while(1) {scanf ("%d",&N); if(!n) Break; cout<<dp[n]<<"\ n"; }    return 0;}

poj-2478 Farey Sequence (DP, Euler function)

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.