Problem B. Rational Number Tree
来源:互联网 发布:淘宝和农村淘宝互通吗 编辑:程序博客网 时间:2024/06/13 22:47
The contest is over. You will be able to try solving the problems for practice soon.
Small input9 points
Large input
12 points
Problem
Consider an infinite complete binary tree where the root node is 1/1 and left and right childs of node p/q are p/(p+q) and (p+q)/q, respectively. This tree looks like:
1/1 ______|______ | | 1/2 2/1 ___|___ ___|___ | | | |1/3 3/2 2/3 3/1...It is known that every positive rational number appears exactly once in this tree. A level-order traversal of the tree results in the following array:
1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1, ...
Please solve the following two questions:
- Find the n-th element of the array, where n starts from 1. For example, for the input 2, the correct output is 1/2.
- Given p/q, find its position in the array. As an example, the input 1/2 results in the output 2.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of one line. The line contains a problem id (1 or 2) and one or two additional integers:
- If the problem id is 1, then only one integer n is given, and you are expected to find the n-th element of the array.
- If the problem id is 2, then two integers p and q are given, and you are expected to find the position of p/q in the array.
Output
For each test case:
- If the problem id is 1, then output one line containing "
Case #x: p q
", wherex
is the case number (starting from 1), andp
,q
are numerator and denominator of the asked array element, respectively. - If the problem id is 2, then output one line containing "
Case #x: n
", wherex
is the case number (starting from 1), andn
is the position of the given number.
Limits
1 ≤ T ≤ 100; p and q are relatively prime.
Small dataset
1 ≤ n, p, q ≤ 216-1; p/q is an element in a tree with level number ≤ 16.
Large dataset
1 ≤ n, p, q ≤ 264-1; p/q is an element in a tree with level number ≤ 64.
Sample
- Problem B. Rational Number Tree
- Problem B. Rational Number Tree -Calkin–Wilf tree 数节点编号 基本数据类型范围
- Google中国2014校园招聘笔试Round A China New Grad Test Problem B. Rational Number Tree
- Round A China New Grad Test 2014---很基本的一道题Problem B. Rational Number Tree
- Rational Number Tree
- Rational Number Tree
- ACdream 1187Rational Number Tree
- Google Code Jam Notes - Rational Number Tree - Java
- ACdream 1187 Rational Number Tree(搜索:DFS)
- ACdream 1187——Rational Number Tree(二进制,递归)
- pat 1088 Rational Number
- Rational Number.c
- Google 2014校园招聘9月23日笔试题:Rational number tree
- Codeforces Round #168 (Div. 1), problem: (B) Zero Tree, DFS
- PKU Campus 2011 B A Problem about Tree lca倍增
- B-tree、B+tree
- B-Tree/B+Tree
- B-tree B+tree
- CMUX帧结构以及开启
- 建立OpenCV 框架
- windows注册与删除tomcat服务
- 工作流引擎创建
- Problem A. Read Phone Number
- Problem B. Rational Number Tree
- Problem C. Sorting
- poj 1328 Radar Installation
- 超详细mysql left join,right join,inner join用法分析
- 罗马假日
- Problem D. Cross the maze
- 图的广度、深度遍历
- MySql中的标准差函数 STD, STDDEV_SAMP
- Types of Data