题目描述

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. UVALive 4394 String painter ——(区间DP)

    其实这个dp过程有点似懂非懂...代码如下: #include <stdio.h> #include <algorithm> #include <string.h> ...

  2. 2016"百度之星" - 初赛(Astar Round2A)1005 BD String(HDU5694)——找规律、字符串对称、分治

    分析:按照题目所给的意思每次处理得到的新的字符串都是具有高度对称性的,举个例子,如题目所给的第三个字符串,最中间的是B然后两边分散开去,一边是B的话另外一边关于这个中心对称的那个位置一定是D,反过来同 ...

  3. CF1197B

    CF1197B 题意: 出n个柱子,每个柱子一个圆盘,其半径各不相同,每次只能将柱子上只有一个圆盘的移到相邻位置,问能否全部移到一个柱子上. 解法: 思路题. 如果所有盘子都能移动到同一个柱子上,那么 ...

  4. onselectstart与onselect—禁止选择或禁止复制

    这两个事件看起来很相似,事实上却非常的不同. onselectstart 使用js禁止用户选中网页上的内容,IE及Chrome下的方法一样.使用onselectstart,例如 IE: <bod ...

  5. 一个XP SP3调用0地址蓝屏BUG

    0x00 蓝屏的堆栈 在XP SP3上跑POC之后,一段时间之后会出现蓝屏,蓝屏的堆栈如下,可以看出是ACKData里面CALL了一个0指针导致的蓝屏 0x01 蓝屏原因 1 ETW(Event Tr ...

  6. 【java】Java.math.BigDecimal.subtract()方法实例

    java.math.BigDecimal.subtract(BigDecimal subtrahend) 返回一个BigDecimal,其值为 (this - subtrahend), 精度为 max ...

  7. fsLayui数据表格使用

    fsLayui 是一个基于layui的快速开发插件,支持数据表格增删改查操作,提供通用的组件,通过配置html实现数据请求,减少前端js重复开发的工作. GitHub下载 码云下载 测试环境地址:ht ...

  8. 设置Python打印格式

    >>> def esc(code): ... return f'\033[{code}m' ... >>> print(esc('31;1;4') + 'reall ...

  9. 课下选做作业MyOD

    2019-2020-1 20175227 <信息安全系统设计基础> 课下选做作业MyOD 要求 复习c文件处理内容 编写myod.c 用myod XXX实现Linux下od -tx -tc ...

  10. [转][C#]无法创建虚拟目录。ASP.NET 2.0 尚未在 Web服务器上注册。

    参考:http://www.bubuko.com/infodetail-997666.html C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet ...