题目描述

An infinite full binary tree labeled by positive rational numbers is defi ned by:
• 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).

输入

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), 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, 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.

输出

For each data set there is a single line of output. It contains the data set number, K, followed by a single space which is then followed by the numerator of the fraction, followed immediately by a forward slash (‘/’) followed immediately by the denominator of the fraction. Inputs will be chosen such that neither the numerator nor the denominator will overfl ow a 32-bit integer.

样例输入

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的更多相关文章

  1. UVaLive 7363 A Rational Sequence (二叉树)

    题意:给定一个二叉树,并对每一个进行编号和规定,现在给你一个值,问你是第几个. 析:这个题,我想了好久才想出来,这个真是数据结构练的太差了,不够扎实,这个题,应该从下向上推,如果分子大于分母,那么这个 ...

  2. TensorFlow深度学习笔记 循环神经网络实践

    转载请注明作者:梦里风林 Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有问题可以到Issue区讨论 官方教程地址 视频/字幕下载 加 ...

  3. Twitter的SnowFlake分布式id生成算法

    二进制相关知识回顾 1.所有的数据都是以二进制的形式存储在硬盘上.对于一个字节的8位到底是什么类型 计算机是如何分辨的呢? 其实计算机并不负责判断数据类型,数据类型是程序告诉计算机该如何解释内存块. ...

  4. Live Archive 训练题

    7091 Height Ordering Mrs. Chambers always has her class line up in height order (shortest at the fro ...

  5. 理解分布式id生成算法SnowFlake

    理解分布式id生成算法SnowFlake https://segmentfault.com/a/1190000011282426#articleHeader2 分布式id生成算法的有很多种,Twitt ...

  6. SnowFlake --- 分布式id生成算法

    转载自:https://segmentfault.com/a/1190000011282426 概述 SnowFlake算法生成id的结果是一个64bit大小的整数,它的结构如下图: 1位,不用.二进 ...

  7. 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 ...

  8. HDU1005Number Sequence(找规律)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. 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. ...

随机推荐

  1. jquer属性 offset、position、scrollTop

    尺寸操作 1.获取宽高      a) jq对象.height/width () :只有获取高度/宽度         尺寸,不包括padding和margin 和 border 2.设置宽度     ...

  2. elasticsearch _update api 更新部分字段内容

    https://www.elastic.co/guide/cn/elasticsearch/guide/current/partial-updates.htmlupdate 请求最简单的一种形式是接收 ...

  3. oracle自定义排序和NULL值排序

    1.自定义顺序 当我们希望将某个查询结果指定的显示顺序展示的时候 order by case when column1=1 then 0 case when column1=1 then 1 else ...

  4. New in Python 3.8.0

    Python 3.8.0 发布时间: Oct. 14, 2019 这是一个Python3.8.0的稳定发行版. Python3.8.0是最新的Python编程语言发行版,ta包含了许多新的特征和优化. ...

  5. mysql -- 清空表中数据

    删除表信息的方式有两种 :truncate table table_name;delete * from table_name;注 : truncate操作中的table可以省略,delete操作中的 ...

  6. SqlServer自动锁定sa解决代码

    ALTER LOGIN sa ENABLE ; GO ALTER LOGIN sa WITH PASSWORD = '' unlock, check_policy = off, check_expir ...

  7. dll程序开发总结

    1.修改生成的dll名称 VS2012中选中某个项目,项目--属性--配置属性--连接器--常规--输出文件

  8. python项目生成及导入依赖的第三方库

    requirements.txt用来记录项目所有的依赖包和版本号,只需要一个简单的pip命令就能完成. pip freeze >requirements.txt 然后就可以用 pip insta ...

  9. 虚拟化技术实现 — KVM 的 CPU 虚拟化

    目录 文章目录 目录 前文列表 x86 体系结构的虚拟化 硬件辅助的 CPU 虚拟化 由 VMX 切换支撑的 CPU 虚拟化技术 KVM 的 CPU 虚拟化实现 vCPU 的调度方式 客户机 CPU ...

  10. scikit-learn机器学习(三)多项式回归(二阶,三阶,九阶)

    我们仍然使用披萨直径的价格的数据 import matplotlib matplotlib.rcParams['font.sans-serif']=[u'simHei'] matplotlib.rcP ...