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.

Sample Input

5
1 2
2 3
4 3
1024 2048
3214567 9998877
题解 只要找到公共节点即可,水题一个;
#include<stdio.h>
#include<string.h>
int main()
{
long long int i,j,a,b,m,n,p;
scanf("%lld",&p);
for(int k=;k<p;k++)
{
scanf("%lld%lld",&a,&b);
m=a>b?a:b;
n=a>b?b:a;
long long int count1=,count2=;
while(m!=n)
{
if(n>m)
n/=;
if(m==n)
break;
if(m>n)
m/=;
}//找到公共节点;
while(a!=m)
{
a/=;
count1++;
}
while(b!=m)
{
b/=;
count1++;
}//统计下每一节点到公共节点的距离
printf("%lld\n",count1); }
return ;
}

Full Binary Tree(sdut 2882)的更多相关文章

  1. 1110 Complete Binary Tree (25 分)

    1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...

  2. 【CF438E】The Child and Binary Tree(多项式运算,生成函数)

    [CF438E]The Child and Binary Tree(多项式运算,生成函数) 题面 有一个大小为\(n\)的集合\(S\) 问所有点权都在集合中,并且点权之和分别为\([0,m]\)的二 ...

  3. 2019PAT春季考试第4题 7-4 Structure of a Binary Tree (30 分)

    题外话:考试的时候花了一个小时做了27分,由于Siblings这个单词不知道意思,所以剩下的3分就没去纠结了,后来发现单词是兄弟的意思,气哭~~ 这道题的麻烦之处在于如何从一个字符串中去找数字.先首先 ...

  4. 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】

    [104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary t ...

  5. LeetCode——Balanced Binary Tree(判断是否平衡二叉树)

    问题: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  6. Leetcode 104. Maximum Depth of Binary Tree(二叉树的最大深度)

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

  7. Binary Tree(二叉树+思维)

    Binary Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  8. LeetCode 606. Construct String from Binary Tree (建立一个二叉树的string)

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  9. 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 ...

随机推荐

  1. Python基础学习(第9天)

    第九课:动态类型 1.在Python中,有一类特殊的对象,是专门用来存储数据的,常见的有数字.字符串.数列.字典等.这些对象是存储在内存中的实体,我们并不能直接接触到它们,在程序中接触的是对象名,是指 ...

  2. postgresql recovery.conf改变需要重启吗

    之前在研究pgpoll时,发现trigger_file参数指定的文件存在后,会自动将standby节点提升为可写节点.不需要手动执行pg_ctl promote,但是这个时间一般有延迟,因为进程会定期 ...

  3. PostgreSQL 全文检索

    PostgreSQL 8.3.1  全文检索(转) 在postgreSQL 8.3自带支持全文检索功能,在之前的版本中需要安装配置tsearch2才能使用,安转配置tsearch2就不再多说了,主要介 ...

  4. Azure VM Scalable Sets -- 适用IAAS架构

    1. 选择Virtual Machine scale set2. 选择resource manager(或创建最新)3. 基本信息配置4. 配置scale的参数(可伸缩的balance模型):VM最小 ...

  5. 在Blender上安装RenderMan插件

    2018/3/27 Download and Install Blender itself Download RenderMan Installer (Need to register an acco ...

  6. (九)jsMath

    1.Math对象 作用:用于执行数学任务,把Math作为对象就可以调用其方法和属性. eg:    typeof Math); 2.Math属性     PI:圆周率(约等于 3.1415926); ...

  7. (二)Vue常用7个属性

    学习vue我们必须之到它的7个属性,8个 方法,以及7个指令.787原则 el属性 用来指示vue编译器从什么地方开始解析 vue的语法,可以说是一个占位符. data属性 用来组织从view中抽象出 ...

  8. Hadoop集群中节点角色定义

    Hadoop分别从两个角度将主机划分为两种角色. 最基本的划分原则为Master和Slave,即主人和奴隶: 第一,从HDFS的角度,将主机划分为NameNode和DataNode(在分布式文件系统中 ...

  9. 细说 const

    1.const 简单应用 const int pp=0 //pp 为整形常量,不能修改 还有另外一种不常用的方式 但是最容易误导 int const pp=0 //pp 为整形常量,不能修改 记住这两 ...

  10. windows服务是如何被调用的?

    1.服务介绍 操作系统在启动的时候,会启动一些不需要用户交互的进程.这些进程被称为服务.当操作系统启动后它就自动被运行. 2.组成 服务程序.服务控制程序(SCP,service control pr ...