Full Binary Tree
Time Limit: 2000MS Memory limit: 65536K
题目描述
In computer science, a binary tree is a tree data structure in which each node has at most two children. Consider an infinite full binary tree (each node has two children except the leaf nodes) defined as follows. For a node labelled v its left child will be labelled 2 * v and its right child will be labelled 2 * v + 1. The root is labelled as 1.
You are given n queries of the form i, j. For each query, you have to print the length of the shortest path between node labelled i and node labelled j.
输入
First line contains n(1 ≤ n ≤ 10^5), the number of queries. Each query consists of two space separated integers i and j(1 ≤ i, j ≤ 10^9) in one line.

本文介绍了如何解决2882题目的问题,即在完全二叉树中找出两个节点间的最短路径。通过分析节点编号规律,可以确定节点所在的层数,从而找到它们的最近公共祖先,以此计算出最小距离。
最低0.47元/天 解锁文章
900





