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)