The Stern-Brocot tree is an elegant structure that allows the construction of the set of all non-negative fractions. It is built through an infinite sequence of iterations. At the zero iteration, we start with an array of two fractions:
(the second value, strictly speaking, is not a fraction; it can be understood as an irreducible fraction representing infinity).
Then, at each subsequent iteration, two adjacent fractions a/b and c/d are taken, and their mediant, that is, the fraction a+c/b+d, is inserted between them. Thus, the arrays for the next iterations will be:
[0/1, 1/2, 1/1, 2/1, 1/0]
[0/1, 1/3, 1/2, 2/3, 1/1, 3/2, 2/1, 3/1, 1/0]
It can be shown that any number that can be represented as a non-negative fraction will eventually appear in this array. Also, no fraction will appear in it more than once. Therefore, for each number, we will remember the iteration after which it first appeared. Thus, after the first iteration, the fraction 1/1 appeared, after the second iteration — fractions 1/2 and 2/1, and so on.
Now we just need to provide the definition of the Stern-Brocot tree itself. At the root of this infinite tree is the fraction 1/1, and to the left and right of the tree are the fractions 0/1 and 1/0. Each vertex of the tree has two children, each of which is obtained as the mediant of its left ancestor and right ancestor. Thus, at depth i of this tree, there will be all those fractions that appeared at the i-th iteration:
This tree has many remarkable properties, but they are not of interest to us today. The task is to find the fraction that is at depth N and is the M-th from the left.
Input
The input consists of a single line containing two integers N and M — the depth and the order from the left of the required fraction (1 ≤ N ≤ 109, 1 ≤ M ≤ min(2N−1, 109)).
Output
Output the numerator and denominator of this fraction separated by a space.
Samples
| input | output |
|---|
1 1 | 1 1 |
3 3 | 3 2 |
5 7 | 5 7 |
Problem Author: Vadim Barinov
Problem Source: University academic school olympiad in informatics 2023