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. 如何将桌面的路径定义到其它盘符,如d:\users\桌面

    首先要在“开始”——“运行”内输入“regedit”打开注册表编辑器,然后要在“文件”下拉菜单中的“导出”功能备份好注册表,以防万一,接着在左侧窗口依次打开: HKEY_CURRENT_USER\So ...

  2. Spring Boot下如何自定义Repository中的DAO方法

    环境配置介绍 jdk 1.8, Spring Boot 1.5.3.RELEASE, Mysql, Spring Data, JPA 问题描述 Spring Data提供了一套简单易用的DAO层抽象与 ...

  3. OpenCV获取RTSP解码播放

    #include <opencv2/opencv.hpp> int main(int argc, char **argv){ IplImage *pFrame = NULL, *srcIm ...

  4. 如何在公司Http代理后使用NuGet官方源

    文章转自CSDN 霍力强的专栏 有些公司上网使用的是Http代理.默认情况下,VS是无法访问外部网络的.如果要使用NuGet,通常只能在局域网里架一个自己的NuGet服务器.但这种方法不论是packa ...

  5. TCP, Scoket, HTTP(转)

    1.TCP连接 要想明白Socket连接,先要明白TCP连接.手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上 ...

  6. TCP传输控制协议

    TCP 在TCP/IP协议模型中, 传输层协议有TCP和UDP, 这里主要介绍下可靠传输TCP协议, 目前是传输层协议首选. 特点 面向数据流(字节流形式) 虚电路连接 有缓冲传输(提供push机制 ...

  7. linux centos6.5 安装gcc-c++时出现 yum install gcc-c++ cannot find a valid baseurl for repo...

    1.输入命令:cd /etc/sysconfig/network-scripts/ 2.ls查看该文件夹下 3.vi ifcfg-eth0 按i进行编辑,添加如下两行后,esc →shift+:→wq ...

  8. nginx中在超全局变量$_SERVER中增加变量

    业务中可能会用到一些自定义的超全局变量,需要在nginx中生成的,比如,每次nginx请求的id,可以在nginx中配置 如: location ~ \.php$ { root           / ...

  9. envoy 测试试用

    备注: 为了简单测试使用的是docker 镜像进行的测试   1.  Dockerfile   FROM lyft/envoy:latest RUN apt-get update COPY envoy ...

  10. Linux 驱动层实现阻塞和非阻塞

    linux应用层的函数默认是阻塞型的,但是要想真正实现阻塞,还需要驱动的支持才行. 例:open().scanf().fgets().read().accept() 等 1.默认情形,驱动层不实现阻塞 ...