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.
 

Sample Input

5
1 2
2 3
4 3
1024 2048
3214567 9998877

Sample Output

1
2
3
1
44

Hint

题目意思:这是一个二叉树,如上图所示,给出二叉树上的而已两个点求出一点到另一点最短的距离或者说是步数。

解题思路:我们知道二叉树相当于一个自上而下的三角形,任意两个点在上方一定有一个点相交,称其为节点,我们只要找到那个节点就可以了,两点到节点的距离就是最短距离。

 #include<stdio.h>
#include<string.h>
int main()
{
int t;
long long a,b,m,n,count;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&a,&b);
count=;
while()
{
if(a>b)///两个数较大的那一个减半上移,不断逼近节点
{
a=a/;
count++;
}
if(b>a)
{
b=b/;
count++;
}
if(b==a)
break;
}
printf("%lld\n",count); }
return ;
}

Full Binary Tree(二叉树找规律)的更多相关文章

  1. [HDOJ5573]Binary Tree(找规律,贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5573 这个题……规律暂时还找不到,先贡献两发TLE的代码吧,一个dfs一个状压枚举. #include ...

  2. HDU 5573 Binary Tree(找规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5573 题意:给你一个完全二叉树,节点为自然数的排列(第一行1,第二行2 3,第三行4 5 6 7... ...

  3. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  4. [LeetCode] 543. Diameter of Binary Tree 二叉树的直径

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  5. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  6. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  7. [LeetCode] Closest Leaf in a Binary Tree 二叉树中最近的叶结点

    Given a binary tree where every node has a unique value, and a target key k, find the value of the n ...

  8. [LeetCode] Second Minimum Node In a Binary Tree 二叉树中第二小的结点

    Given a non-empty special binary tree consisting of nodes with the non-negative value, where each no ...

  9. LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy

    要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...

随机推荐

  1. CentOS7 更换OpenStack-queens源

    根据官网的安装文档来对OpenStack搭建时碰到一个问题,安装完centos-release-openstack-queens后相当于是增加了一个OpenStack的源,但是因为这个源是在国外安装一 ...

  2. iOS 12.0-12.1.2 越狱教程

    unc0ver V3.0.0~b29 越狱工具已经开始公测,支持搭载 A8X-A11 处理器的 iOS 12.0-12.1.2 设备完整越狱,Cydia 商店和 Substrate 插件可正常安装并运 ...

  3. Java编码算法和摘要算法

    编码算法 编码算法是将一种形式转换成等价的另外一种形式.主要是为了方便某种特定场景的处理. 字母如何在计算机中表示呢? 用ASCII编码 那中文字符如何在计算机中表示呢? 用Unicode编码 如何同 ...

  4. ubuntu18.04 VirtualBox 开启虚拟机出错 Kernel driver not installed (rc=-1908)

    写的很明白了 提示缺少GCC PERL MAKE,安装 重试..... 重启VM 搞定....

  5. 柱体内温度分布图 MATLAB

    对于下底面和侧面绝热,上底面温度与半径平方成正比的柱体,绘制柱体内温度分布图. 这里给出两种尝试:1.散点图:2.切片云图 1. 散点图仿真 首先使用解析算法求的场解值的解析表达,其次求解Bessel ...

  6. 记数据结构MOOC-二叉树

    主要的学习内容 在本章中,主要学习了二叉树的实现以及各种遍历的方法.着重介绍了前序.中序.后序三种遍历方法的递归实现,同时也描述了前序中序遍历的迭代方法. 教材的主要内容 教材是以哈夫曼编码树为主要脉 ...

  7. 20155235 2016-2017-2 《Java程序设计》第9周学习总结

    20155235 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 整合数据库 JDBC入门 JDBC简介 连接数据库 使用Statement.Res ...

  8. Spring SimpleJdbcOperations 批量更新

    1.控制台代码 import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowM ...

  9. [arc065E]Manhattan Compass[曼哈顿距离和切比雪夫距离转换]

    Description 传送门 Solution 题目要求的是曼达顿距离,对于每个点(x,y),我们把它变为(x-y,x+y),就可以转换成求切比雪夫距离了. 证明如下:$max(\left | (x ...

  10. Swift - 重写导航栏返回按钮

    // 重写导航栏返回按钮方法 func configBackBtn() -> Void { // 返回按钮 let backButton = UIButton(type: .custom) // ...