A Rational Sequence 3
InterviewTime limit2sMemory limit512 MB
Find the rational number at position N in the breadth-first level-order traversal of the Calkin-Wilf-style binary tree rooted at 1/1, where left and right children are p/(p+q) and (p+q)/q.
- Level
Medium5 of 10
- Topics
- Tree, Math, Number theory, Binary search
- Solved
- No attempts yet
Problem
An infinite complete binary tree is labeled with positive rational numbers by the following rules.
- The label of the root is .
- The left child of a node labeled is labeled .
- The right child of a node labeled is labeled .
The top of the tree looks like the figure below.

Reading the tree in level order, that is breadth first and left to right within each level, gives the rational sequence .
, , , , ,
Given an index , write a program that computes .
Input
The first line contains the number of data sets ().
Each of the next lines holds one data set. A line contains the data set number and the index of the element to compute (). The data sets are numbered through in the order they are given.
The data sets are independent and every one is processed the same way.
Output
Print one line for each data set. On that line print the data set number , a single space, the numerator of , the slash character /, and the denominator of , in that order with nothing between them. Do not put a space on either side of the slash.
The input is chosen so that neither the numerator nor the denominator exceeds a 32-bit unsigned integer.