【参考博客】:

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


 #pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("O3")
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fll
#define pi acos(-1.0)
#define nl "\n"
#define pii pair<ll,ll>
#define ms(a,b) memset(a,b,sizeof(a))
#define FAST_IO ios::sync_with_stdio(NULL);cin.tie(NULL);cout.tie(NULL)
using namespace std;
typedef long long ll;
const int mod = ;
ll qpow(ll x, ll y){ll s=;while(y){if(y&)s=s*x%mod;x=x*x%mod;y>>=;}return s;}
//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;}
inline int read(){int x=,f=;char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}while(ch>=''&&ch<='') x=x*+ch-'',ch=getchar();return x*f;} const int N = 1e4+; ll pki[N]; void get_pki()
{
for(int i=;i<N;i++) pki[i] = i;
for(int i=;i<N;i++){
if(pki[i]==i)for(int j=i;j<N;j+=i)
pki[j]=pki[j]/i*(i-);
pki[i] += pki[i-];
}
} int main()
{
get_pki();
int _, cas, n;
for(scanf("%d",&_);_--;)
{
scanf("%d",&cas);
scanf("%d",&n);
printf("%d %lld/2\n",cas,*pki[n]-); } }

【规律】Farey Sums的更多相关文章

  1. 【poj 2478】Farey Sequence(数论--欧拉函数 找规律求前缀和)

    题意:定义 Fn 序列表示一串 <1 的分数,分数为最简分数,且分母 ≤n .问该序列的个数.(2≤N≤10^6) 解法:先暴力找规律(代码见屏蔽处),发现 Fn 序列的个数就是 Φ(1)~Φ( ...

  2. URAL 2065 Different Sums (找规律)

    题意:构造一个数列,使得它们的区间和的种类最少,其中数列中不同的数的数目不少于k. 析:我们考虑0这个特殊的数字,然后0越多,那么总和种类最少,再就是正负交替,那么增加0的数量. 代码如下: #pra ...

  3. 【找规律】URAL - 2065 - Different Sums

    就让0出现得尽可能多嘛……大概感受一下就是这样…… 0 0 ... 0 0 0 0 4 -4 3 -3 2 -2 1 -1 #include<cstdio> using namespace ...

  4. 51nod1161 Partial Sums

    开始想的是O(n2logk)的算法但是显然会tle.看了解题报告然后就打表找起规律来.嘛是组合数嘛.时间复杂度是O(nlogn+n2)的 #include<cstdio> #include ...

  5. Backward Digit Sums(暴力)

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5664   Accepted: 32 ...

  6. 1712: [Usaco2007 China]Summing Sums 加密

    1712: [Usaco2007 China]Summing Sums 加密 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 338  Solved: 12 ...

  7. Codeforces 1091D New Year and the Permutation Concatenation 找规律,数学 B

    Codeforces 1091D New Year and the Permutation Concatenation https://codeforces.com/contest/1091/prob ...

  8. 【计数】cf223C. Partial Sums

    考试时候遇到这种题只会找规律 You've got an array a, consisting of n integers. The array elements are indexed from ...

  9. Farey sequences

    n阶的法里数列是0和1之间最简分数的数列,由小至大排列,每个分数的分母不大于n. Stern-Brocot树(SB Tree)可以生成这个序列 {0/1,1/1} {0/1,1/2,1/1} {0/1 ...

随机推荐

  1. OpenGL ES: (2) OpenGL ES 与 EGL、GLSL的关系

    OpenGL ES 是负责 GPU 工作的,目的是通过 GPU 计算,得到一张图片,这张图片在内存中其实就是一块 buffer,存储有每个点的颜色信息等.而这张图片最终是要显示到屏幕上,所以还需要具体 ...

  2. 如何向Makefile传递参数?

    答: 直接在make命令的后面加上参数,如: make HOST_CFLAGS=-I. 注意事项: HOST_CFLAGS变量将会替换相应Makefile中的HOST_CFLAGS,也就是Makefi ...

  3. It’s worth noting值得注意的是

    It’s worth noting that in JavaScript applications the Model is often connected via Ajax to a back-en ...

  4. ubuntu下编译和使用libxml2

    安装: #sudo apt-get install libxml2 #sudo apt-get install libxml2-dev sudo apt-get install libxml2-dev ...

  5. javascript中的Error对象

    在javascript中一旦代码解析或运行时发生错误,javascript引擎就会自动产生并抛出一个Error对象的实例,然后整个程序就中断在发生错误的地方. Error对象的实例有三个基本的属性: ...

  6. PAT 甲级 1058 A+B in Hogwarts (20 分) (简单题)

    1058 A+B in Hogwarts (20 分)   If you are a fan of Harry Potter, you would know the world of magic ha ...

  7. ElasticSearch——Curator索引管理

    简介 curator 是一个官方的,可以管理elasticsearch索引的工具,可以实现创建,删除,段合并等等操作.详见官方文档 功能 curator允许对索引和快照执行许多不同的操作,包括: 从别 ...

  8. redis未设置idle超时时间导致连接过多

    今天ELK收集日志的时候,发现收集失败,查找各方面原因,最后在redis日志里面发现报错:[2489] 02 Jun 10:43:42 # Error allocating resoures for ...

  9. Spring Boot中在程序中获得application.properties中的值

    方法是使用@Value注解的方式注解一个值. 首先,在我们的application.properties中添加一个值如下: zifeiy.tmpfile.location=D:/duty 然后在我们的 ...

  10. PowerDesigner的安装和数据库创建

    PowerDesigner安装方法:  http://dev.firnow.com/course/3_program/java/javajs/20090908/174375.html 安装完这2个软件 ...