#P16470. [GKS 2013 #A] Rational Number Tree
[GKS 2013 #A] Rational Number Tree
题目描述
Consider an infinite complete binary tree where the root node is and left and right childs of node are and , respectively. This tree looks like:
:::align{center}
:::
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:
, , , , , , ,
Please solve the following two questions:
- Find the -th element of the array, where starts from . For example, for the input , the correct output is .
- Given , find its position in the array. As an example, the input results in the output .
输入格式
The first line of the input gives the number of test cases, . test cases follow. Each test case consists of one line. The line contains a problem id ( or ) and one or two additional integers:
- If the problem id is , then only one integer is given, and you are expected to find the -th element of the array.
- If the problem id is , then two integers and are given, and you are expected to find the position of in the array.
输出格式
For each test case:
- If the problem id is , then output one line containing "Case #x: p q", where is the case number (starting from ), and are numerator and denominator of the asked array element, respectively.
- If the problem id is , then output one line containing "Case #x: n", where is the case number (starting from ), and is the position of the given number.
4
1 2
2 1 2
1 5
2 3 2
Case #1: 1 2
Case #2: 2
Case #3: 3 2
Case #4: 5
提示
Limits
; and are relatively prime.
Test set 1 - Visible
; is an element in a tree with level number .
Test set 2 - Hidden
; is an element in a tree with level number .