#LCA0000016. Cây tăng trưởng vô hạn (Evergrowing Tree)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Cây tăng trưởng vô hạn (Evergrowing Tree)
Evergrowing Tree
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
Consider an infinite -ary rooted tree: every vertex has exactly children. The root is labeled . All positive integers label vertices in breadth-first order, from upper levels to lower levels and from left to right within a level.
For each of vertex pairs , find their lowest common ancestor: the deepest vertex that is an ancestor of both, considering each vertex an ancestor of itself.
The tree has infinitely many vertices; print the required labels without materializing it.
Input
- The first line contains .
- Each of the next lines contains , with .
Output
Print lines; each line gives the lowest common ancestor label for one pair.
Subtasks
- Subtask 1 (20 points): ; ; ; ; ; .
- Subtask 2 (30 points): ; ; ; ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Input
3 3
5 7
8 11
3 9
Output
2
1
3
Explanation
For , vertices and share parent . Vertices and share only root , while vertex is an ancestor of .