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. vue面试题!!!

    由于公司需要,需要把项目拆分,前端使用vue框架.最近面试vue总结的试题 1:mvvm框架是什么?它和其他框架的区别是什么? mvvm 全称model view viewModel,model数据模 ...

  2. node.js 基于cheerio的爬虫工具,需要登录权限的爬虫工具

    公司有过一个需求,需要拿一个网页的的表格数据,数据量达到30w左右:为了提高工作效率. 结合自身经验和网上资料.写了一套符合自己需求的nodejs爬虫工具.也许也会适合你的. 先上代码.在做讲解 'u ...

  3. bootstrap世界探索2——万物的起源(网格系统)

    万物的起源是非常神奇的,谁又能想到小小的细胞(文字排版)竟能构建大千世界. <!DOCTYPE html> <html lang="en"> <hea ...

  4. armv7学习记录

    ARM架构支持跨大范围性能点的实现.ARM处理器的架构简单性导致了非常小的实现,而小的实现意味着设备可以具有非常低的功耗.实现大小.性能和非常低的功耗是ARM体系结构的关键属性. ARM架构是一个精简 ...

  5. aircrack-ng 破解无线网络

    1.科普当今时代,wifi 已成为我们不可缺少的一部分,上网.看视频.玩游戏,没有 wifi 你就等着交高额的流量费吧,本来我想单独的写 wpa 破解和 wps 破解,后来觉得分开写过于繁琐,索性合并 ...

  6. PTA基础编程题目集6-4求自定类型元素的平均 (函数题)

    6-4 求自定类型元素的平均 (10 分)  本题要求实现一个函数,求N个集合元素S[]的平均值,其中集合元素的类型为自定义的ElementType. 函数接口定义: ElementType Aver ...

  7. Java动态代理代码快速上手

    动态代理的两个核心的点是:代理的行为 和 代理机构. 举个例子,上大学的时候,很多同学吃午饭的时候都是叫别人带饭,有一个人H特别热心肠,想了一个办法,他在门口挂了个公示牌,每天有谁想要找人带饭就写公告 ...

  8. 20155239《Java程序设计》实验二(面向对象程序设计)实验报告

    实验内容 初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握UML建模 4.熟悉S.O.L.I.D原则 5.了解设计模式 实验步骤 单元测试 1.三种代码: 伪代码 ...

  9. 20155317 《Java程序设计》实验五网络编程与安全实验报告

    20155317 <Java程序设计>实验五网络编程与安全实验报告 遇到问题 在刚开始启动客户端或者服务端时,出现了一系列的错误情况,总是提示异常信息 后来经过询问同学,反应将端口号修改一 ...

  10. 微信小程序判断按钮是否显示,或者隐藏

    js中: onLoad:function(options) { this.setData({ orderstate:'待送检' }) },   WXML中: <view wx:if=" ...