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. OC-Foundation框架

    ========================== Foundation框架下的常用类 ========================== 一.[NSNumber]================ ...

  2. AS3中ASCII码和字符互转函数

    AS3中ASCII码和字符互转函数 字符转成ASCII码: 格式:字符串变量.charCodeAt(字符位置); var str:String = “A”; trace(str.charCodeAt( ...

  3. CRC-16校验原理

    最详细易懂的CRC-16校验原理(附源程序) 1.循环校验码(CRC码): 是数据通信领域中最常用的一种差错校验码,其特征是信息字段和校验字段的长度可以任意选定. 2.生成CRC码的基本原理: 任意一 ...

  4. 作为一名Java开发工程师需要掌握的专业技能

    在学习Java编程完之后,学员们面临的就是就业问题.作为一名Java开发工程师,企业在招聘的时候,也是有一定的标准的. 为了帮助大家更好的找到适合自己的工作,在这里分享了作为一名Java开发工程师需要 ...

  5. 旧书重温:0day2【8】狙击windows的异常处理实验

    现在进入0day2的第六章内容 其中第六章的书本内容我都拍成了图片格式放在了QQ空间中(博客园一张一传,太慢了)http://user.qzone.qq.com/252738331/photo/V10 ...

  6. 对结合BDD进行DDD开发的一点思考和整理

    引言 二十年前的我,还在学校里抱着一台DIY机(德州486+大众主板+16M内存+3.5inch软驱+昆腾320M硬盘,当时全校最快主机没有之一),揣着一本<Undocumented DOS&g ...

  7. Spring 实现发送电子邮件的两种方法

     1.通过xml文件配置主要属性: xml文件:test.xml <bean id="mailSender" class="org.springframewor ...

  8. cordic算法的fpga实现

    cordic算法参考:http://wenku.baidu.com/view/6c623aa8910ef12d2bf9e732.html 这是百度文库的一个文档,详细介绍了cordic算法的基本内容. ...

  9. 基于jwt和角色的访问控制方案-从0开始

    上一版本的构想:http://www.cnblogs.com/vhyc/p/7953779.html 缺陷:dubbo的filter里面不能注入mapper的bean,而且每次查询都访问数据库速度影响 ...

  10. Java 编码规范

    package(包) 包名的命名规范:1.小写 2.至少有一层目录 3.域名倒置书写 package baidu; package com.baidu.www; Class(类)-----大驼峰法 类 ...