Given a positive integer, N, the sequence of all fractions a/b with (0 < a ≤ b), (1 < b ≤ N) and a and b relatively prime, listed in increasing order, is called the Farey Sequence of order N.
For example, the Farey Sequence of order 6 is:
0/1, 1/6, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 5/6, 1/1
If the denominators of the Farey Sequence of order N are:
b[1], b[2], . . . , b[K]
then the Farey Sum of order N is the sum of b[i]/b[i + 1] from i = 1 to K—1.
For example, the Farey Sum of order 6 is:
1/6 + 6/5 + 5/4 + 4/3 + 3/5 + 5/2 + 2/5 + 5/3 + 3/4 + 4/5 + 5/6 + 6/1 = 35/2
Write a program to compute the Farey Sum of order N (input).
The first line of input contains a single integer P, (1 ≤ P ≤ 10000), which is the number of data sets that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input. It contains the data set number, K, followed by the order N, (2 ≤ N ≤ 10000), of the Farey Sum that is to be computed.
For each data set there is a single line of output. The single output line consists of the data set number,K, followed by a single space followed by the Farey Sum as a decimal fraction in lowest terms. If the denominator is 1, print only the numerator.
1 #pragma GCC optimize("Ofast,no-stack-protector")
2 #pragma GCC optimize("O3")
3 #pragma GCC optimize(2)
4 #include <bits/stdc++.h>
5 #define inf 0x3f3f3f3f
6 #define linf 0x3f3f3f3f3f3f3f3fll
7 #define pi acos(-1.0)
8 #define nl "\n"
9 #define pii pair<ll,ll>
10 #define ms(a,b) memset(a,b,sizeof(a))
11 #define FAST_IO ios::sync_with_stdio(NULL);cin.tie(NULL);cout.tie(NULL)
12 using namespace std;
13 typedef long long ll;
14 const int mod = 998244353;
15 ll qpow(ll x, ll y){ll s=1;while(y){if(y&1)s=s*x%mod;x=x*x%mod;y>>=1;}return s;}
16 //ll qpow(ll a, ll b){ll s=1;while(b>0){if(b%2==1)s=s*a;a=a*a;b=b>>1;}return s;}
17 inline int read(){int x=0,f=1;char ch=getchar();while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}while(ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();return x*f;}
18
19 const int N = 1e4+5;
20
21 ll pki[N];
22
23 void get_pki()
24 {
25 for(int i=1;i<N;i++) pki[i] = i;
26 for(int i=2;i<N;i++){
27 if(pki[i]==i)for(int j=i;j<N;j+=i)
28 pki[j]=pki[j]/i*(i-1);
29 pki[i] += pki[i-1];
30 }
31 }
32
33 int main()
34 {
35 get_pki();
36 int _, cas, n;
37 for(scanf("%d",&_);_--;)
38 {
39 scanf("%d",&cas);
40 scanf("%d",&n);
41 printf("%d %lld/2\n",cas,3*pki[n]-1);
42
43 }
44
45 }
View Code