【规律】Farey Sums

【参考博客】:

https://blog.csdn.net/meopass/article/details/82952087

Farey Sums

题目描述

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.

样例输入

4
1 6
2 15
3 57
4 9999

样例输出

1 35/2
2 215/2
3 2999/2
4 91180457/2

参考博客:

别人博客的推导公式:

我倒是觉得,这个题目就是找规律。因为看到答案都是分母为2,很容易想到其实这个题目就是找规律有关的。

联系互素就想到欧拉函数。写出前几个出来发现就是  ( 3phi(x) -  1 ) / 2



 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 }

原文地址:https://www.cnblogs.com/Osea/p/11387238.html

时间: 2024-10-26 20:37:35

【规律】Farey Sums的相关文章

【找规律】URAL - 2065 - Different Sums

就让0出现得尽可能多嘛--大概感受一下就是这样-- 0 0 ... 0 0 0 0 4 -4 3 -3 2 -2 1 -1 #include<cstdio> using namespace std; int n,m,a[510]; int main() { scanf("%d%d",&n,&m); int now=0; for(int i=n,j=1;i>=1;--i,++j) { if(m==1) a[i]=0; else { now=-now; i

URAL 2065 Different Sums (找规律)

题意:构造一个数列,使得它们的区间和的种类最少,其中数列中不同的数的数目不少于k. 析:我们考虑0这个特殊的数字,然后0越多,那么总和种类最少,再就是正负交替,那么增加0的数量. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath&g

1712: [Usaco2007 China]Summing Sums 加密

1712: [Usaco2007 China]Summing Sums 加密 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 338  Solved: 127[Submit][Status][Discuss] Description 那N只可爱的奶牛刚刚学习了有关密码的许多算法,终于,她们创造出了属于奶牛的加密方法.由于她们并不是经验十足,她们的加密方法非常简单:第i只奶牛掌握着密码的第i个数字,起始的时候是Ci(0≤Ci<90000000).加密的时

POJ 3086 Triangular Sums

Triangular Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6371   Accepted: 4529 Description The nth Triangular number, T(n) = 1 + - + n, is the sum of the first n integers. It is the number of points in a triangular array with n po

Backward Digit Sums(暴力)

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5664   Accepted: 3280 Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum ad

【BZOJ 1712】 [Usaco2007 China]Summing Sums 加密

1712: [Usaco2007 China]Summing Sums 加密 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 347 Solved: 129 [Submit][Status][Discuss] Description 那N只可爱的奶牛刚刚学习了有关密码的许多算法,终于,她们创造出了属于奶牛的加密方法.由于她们并不是经验十足,她们的加密方法非常简单:第i只奶牛掌握着密码的第i个数字,起始的时候是Ci(0≤Ci<90000000).加密的时

The Cow Lineup_找规律

Description Farmer John's N cows (1 <= N <= 100,000) are lined up in a row.Each cow is labeled with a number in the range 1...K (1 <= K <=10,000) identifying her breed. For example, a line of 14 cows might have these breeds: 1 5 3 2 5 1 3 4 4

初二|两级分化时期,你的学习规律养成了吗?

初中数学, 在初中养成良好的学习习惯有助于高中系统性学习,那么你初中的学习规律养成了吗?俗话说,初一不分上下,初二两级分化,初三天上地下,为什么会如此分,我们一起来了解一下. 1 从知识层面讲,初二所学的科目内容多,难度大. 初一我们所学的知识还是蛮多的,而且这些知识都是我们整个初中所学知识的基础部分.基本概念和基本定理比较多,需要记忆的东西也比较多,但是知识的难度都不是很大,而到了初二,数学知识点和难度都有了大幅度的提升,尤其是几何辅助线的大量涉及使很多同学很难适应,摸不着头脑,分式根式的加入

Codeforces 57C Array dp暴力找到规律

主题链接:点击打开链接 的非增量程序首先,计算, 如果不增加的节目数量x, 非减少一些方案是x 答案就是 2*x - n 仅仅需求得x就可以. 能够先写个n3的dp,然后发现规律是 C(n-1, 2*n-1) 然后套个逆元就可以. #include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define ll long long #