atcoder#RELAY2B. Evergrowing Tree
Evergrowing Tree
Score : points
Problem Statement
You are given an integer . Consider an infinite -ary tree as shown below:
Figure: an infinite -ary tree for the case
As shown in the figure, each vertex is indexed with a unique positive integer, and for every positive integer there is a vertex indexed with it. The root of the tree has the index . For the remaining vertices, vertices in the upper row have smaller indices than those in the lower row. Among the vertices in the same row, a vertex that is more to the left has a smaller index.
Regarding this tree, process queries. The -th query is as follows:
- Find the index of the lowest common ancestor (see Notes) of Vertex and .
Notes
- In a rooted tree, the lowest common ancestor (LCA) of Vertex and is the farthest vertex from the root that is an ancestor of both Vertex and . Here, a vertex is considered to be an ancestor of itself. For example, in the tree shown in Problem Statement, the LCA of Vertex and is Vertex , the LCA of Vertex and is Vertex , and the LCA of Vertex and is Vertex .
Constraints
Input
Input is given from Standard Input in the following format:
Output
Print lines. The -th line must contain the index of the lowest common ancestor of Vertex and .
3 3
5 7
8 11
3 9
2
1
3
The queries in this case correspond to the examples shown in Notes.
100000 2
1 2
3 4
1
1