题目描述

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. ntp时间同步服务器的搭建

    CentOS系统一般自带安装有ntp服务,仅需做相关配置即可. 一.配置ntp服务器: 在选定的ntp服务器上vim /etc/ntp.conf 添加一行:restrict default nomod ...

  2. 【Centos】搭建 SVN 服务器

    1.如果仅仅只是搭建 svn 服务器: (a).先检查 svn 是否已经安装了 rpm -qa subversion #输入这个命令后,会出现 subversion 版本号   (b).如果没有安装, ...

  3. Flutter移动电商实战 --(16)切换后页面状态的保持AutomaticKeepAliveClientMixin

    底栏切换每次都重新请求是一件非常恶心的事,flutter 中提供了AutomaticKeepAliveClientMixin 帮我们完成页面状态保存效果. 1.AutomaticKeepAliveCl ...

  4. gensim word2vec实践

    语料下载地址 # -*- coding: utf-8 -*- import jieba import jieba.analyse # suggest_freq调节单个词语的词频,使其能(或不能)被分出 ...

  5. Oracle查询表和字段

    查看表字段.类型.注释 SELECT A.COLUMN_NAME,B.comments,A.DATA_TYPE FROM USER_TAB_COLUMNS A LEFT JOIN user_col_c ...

  6. 通过id获取指定元素内容(标签里面的 标签内容获取)

    html页面如下 <tr style="background-color:#fff;"> <td colspan="2" align=left ...

  7. python3 高级编程(三) 使用@property

    @property装饰器就是负责把一个方法变成属性调用的. @property广泛应用在类的定义中,可以让调用者写出简短的代码,同时保证对参数进行必要的检查,这样,程序运行时就减少了出错的可能性 cl ...

  8. 关于对多层嵌套的json字符串取目标值的问题

    import java.util.HashMap;import java.util.Iterator;import java.util.Map;import net.sf.json.JSONObjec ...

  9. 怎样获取java新IO的Path文件大小

    import org.junit.Test; import java.io.IOException; import java.nio.file.Files; import java.nio.file. ...

  10. PHPCMS get SQL 返回单条的办法

    {pc:get sql="SELECT * FROM v9_member_detail where userid=1" num="1" return=" ...