Problem Description
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.
Input
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.
Output
For each query, print the required answer in one line.
Example Input
5 1 2 2 3 4 3 1024 2048 3214567 9998877
Example Output
1 2 3 1 44
题意:一个满二叉树,给你树上的两个点,看看从这个点到另一个点至少需要走几步;
思路:一个while就行了,让大的那个除以2,然后比较大小,接着让大的那个除以2,直到他俩相交于一个点,一开始想麻烦 了,写了三种判定;
ps:以后注意,不能把简单问题复杂化;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <stack>
#define LL long long
#define INF 0x7fffffff
#define MAX 200010
#define PI 3.1415926535897932
#define E 2.718281828459045
using namespace std;
int n,t;
long long x,y;
int main()
{
scanf("%d",&t);
while(t--)
{
cin>>x>>y;
int ans=0;
while(x!=y)
{
while(x>y)
{
x=x/2;
ans++;
}
if(x<y)
{
long long tmp=y;
y=x;
x=tmp;
}
}
cout<<ans<<endl;
}
return 0;
}