Largest Common Vertex on a Rooted Tree
InterviewTime limit2sMemory limit512 MB
For two nodes in a complete binary tree numbered heap-style, find the deepest common ancestor k and print 10k.
- Level
Easy3 of 10
- Topics
- Tree, Math, Bit manipulation
- Solved
- No attempts yet
Problem
A rooted tree has 1023 vertices. Vertex 1 is the root, and the two children of vertex are vertex and vertex . Under this rule each natural number from 1 to 1023 is attached to exactly one vertex. Vertices numbered 512 to 1023 have no children. The top four levels look like this.
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Let be the largest vertex number that lies on both the path from vertex 1 to vertex and the path from vertex 1 to vertex . For example, and .
Given two natural numbers and , let . Write a program that computes .
Input
The first line contains the number of test cases (). Each of the next lines holds one test case: two integers and (, ) separated by a single space.
Output
For each test case, print the value of on its own line.