【规律】A Rational Sequence
题目描述
• The label of the root is 1/1.
• The left child of label p/q is p/(p+q).
• The right child ofl abel p/q is (p+q)/q.
The top of the tree is shown in the following figure:

A rational sequence is defined by doing a level order (breadth first) traversal of the tree (indicated by the light dashed line). So that:
F(1) = 1/1, F(2) = 1/2, F(3) = 2/1, F(4) = 1/3, F(5) = 3/2, F(6) = 2/3, . . .
Write a program which takes as input a rational number, p/q, in lowest terms and fi nds the next rational number in the sequence. That is, if F(n) = p/q, then the result is F(n + 1).
输入
Each data set consists of a single line of input. It contains the data set number, K, which is then followed by a space, then the numerator of the fraction, p, followed immediately by a fonward slash (/),followed immediately by the denominator of the fraction, q. Both p and q will be relatively prime and 0 ≤ p, q ≤ 2147483647.
输出
样例输入
5
1 1/1
2 1/3
3 5/2
4 2178309/1346269
5 1/10000000
样例输出
1 1/2
2 3/2
3 2/5
4 1346269/1860498
5 10000000/9999999
【题解】
题意就是让大家根据这颗树构造来跑到下一个点。大家通过观察可以看到,除了最右侧的那个节点外,其他情况都是往上爬树,然后又往下爬。
面对像图 3/2 -> 2/3 这种情况来讲:
1、其实是往上爬,也就是 分子在减少,分母不变。后来发现,一直减去分母直到无法减为止,其实也就是相当于取余运算。
2、然后往右翻一下。分子变成原来的分母,分母变成原来的分母减分子。
3、然后在第一步取余后不是获取了层数吗?然后直接利用层数对分子进行加上分母×层数。
【代码】
异常简单,如果发现规律的话。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int T,kase;
ll p,q;
scanf("%d",&T);
while(T--){
scanf("%d",&kase);
scanf("%lld/%lld",&p,&q);
if( q == ){
printf("%d %d/%lld\n",kase,,p+);
}else if( p < q ){
ll tmp = q;
q = tmp - p;
p = tmp ;
printf("%d %lld/%lld\n",kase,p,q);
}else{
ll dep = ;
dep = p/q; p = p%q;
//printf("Dep : %d\n",dep);
ll tmp = q;
q = tmp - p;
p = tmp ; //printf("%d %d + %d\n",p,q,dep*p); q = q + dep*p;
printf("%d %lld/%lld\n",kase,p,q);
}
}
return ;
}
【规律】A Rational Sequence的更多相关文章
- UVaLive 7363 A Rational Sequence (二叉树)
题意:给定一个二叉树,并对每一个进行编号和规定,现在给你一个值,问你是第几个. 析:这个题,我想了好久才想出来,这个真是数据结构练的太差了,不够扎实,这个题,应该从下向上推,如果分子大于分母,那么这个 ...
- TensorFlow深度学习笔记 循环神经网络实践
转载请注明作者:梦里风林 Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有问题可以到Issue区讨论 官方教程地址 视频/字幕下载 加 ...
- Twitter的SnowFlake分布式id生成算法
二进制相关知识回顾 1.所有的数据都是以二进制的形式存储在硬盘上.对于一个字节的8位到底是什么类型 计算机是如何分辨的呢? 其实计算机并不负责判断数据类型,数据类型是程序告诉计算机该如何解释内存块. ...
- Live Archive 训练题
7091 Height Ordering Mrs. Chambers always has her class line up in height order (shortest at the fro ...
- 理解分布式id生成算法SnowFlake
理解分布式id生成算法SnowFlake https://segmentfault.com/a/1190000011282426#articleHeader2 分布式id生成算法的有很多种,Twitt ...
- SnowFlake --- 分布式id生成算法
转载自:https://segmentfault.com/a/1190000011282426 概述 SnowFlake算法生成id的结果是一个64bit大小的整数,它的结构如下图: 1位,不用.二进 ...
- Codeforces Round #384 (Div. 2) B. Chloe and the sequence(规律题)
传送门 Description Chloe, the same as Vladik, is a competitive programmer. She didn't have any problems ...
- HDU1005Number Sequence(找规律)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- uva 10706 Number Sequence(数学规律)
题目连接:10706 - Number Sequence 题目大意:有一个有0 ~ 9组成的序列,1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 ....就是第一位为1. ...
随机推荐
- Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config
目录 Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config Spring Cloud Config(二):基于Git搭建配置中心 Spring Cl ...
- 基于Kafka+ELK搭建海量日志平台
早在传统的单体应用时代,查看日志大都通过SSH客户端登服务器去看,使用较多的命令就是 less 或者 tail.如果服务部署了好几台,就要分别登录到这几台机器上看,等到了分布式和微服务架构流行时代,一 ...
- mysql测试记录
一直觉得mysql的Memony内存引擎挺好,其他数据库都没有,正好有空,所以试试. 版本:mysql-installer-community-8.0.17.0 os:windows10 SSD硬盘, ...
- Xshell登录Vagrant方式
Xshell登录Vagrant方式 我上一篇文章 介绍了vagrant 如何创建虚拟机集群,在上篇文章的基础上,用xshell 登录 虚拟机发现 默认是无法使用账号密码登录root账号,只能使用vag ...
- Linux下批量ping某个网段的脚本
比如现在需要对192.168.0.0/24网段的ip进行检查,检查哪些ip现在被占用,哪些ip没有被占用,可以通过ping命令来检查,也可以通过nmap接参数来检查 ping命令脚本如下: [root ...
- R-CNN/Fast R-CNN/Faster R-CNN
一.R-CNN 横空出世R-CNN(Region CNN,区域卷积神经网络)可以说是利用深度学习进行目标检测的开山之作,作者Ross Girshick多次在PASCAL VOC的目标检测竞赛中折桂,2 ...
- post请求导出Excel表格
axios.interceptors.response.use((response) =>{ if(response.config && response.config.resp ...
- python3 __mian和__name__的区别
1.新建 test.py 模块: def GetModuleName(): print('__name__ = ', __name__) def PrintName(): print('PrintNa ...
- @value取值
配置文件的书写 valm.DlUrl=http://14.168.55.203:5199/FOSSecMngTemplate?wsdl vals.DlUrl=http://14.168.55.203: ...
- 将ByteBuffer保存成文件
String dest = "d:/download/" + name; Path path = Paths.get(dest).getParent().toAbsolutePat ...