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.
输出
For each query, print the required answer in one line.
示例输入
5 1 2 2 3 4 3 1024 2048 3214567 9998877
示例输出
1 2 3 1 44
提示
来源
2014年山东省第五届ACM大学生程序设计竞赛
示例程序
求一颗满二叉树中任意两节点的距离
ACcode:
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define maxn 1010
#define eps 1e-10
using namespace std;
int main(){
int t,a,b;
scanf("%d",&t);
while(t--){
scanf("%d%d",&a,&b);
if(a<b)swap(a,b);
int ans=0;
while(a!=b){
while(a>b){
a>>=1;
ans++;
}
swap(a,b);
}
cout<<ans<<'\12';
}
return 0;
}