题目描述

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.

输入

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.

输出

For each query, print the required answer in one line.

示例输入

5
1 2
2 3
4 3
1024 2048
3214567 9998877

示例输出

1
2
3
1
44

分析:对于一颗满的完全二叉树来说,从叶子节点i 到根节点的最短路径长度为logi 向下取整。由于i ≤ 10^9,因此最短路径长度不会超过31,所以树中任意两个节点间的最短路径长度不会超过62。这说明用int 可以存下最多路径长度。至于求这个长度,简单递归(类比于求最大公共祖先)就可以办到:

#include<stdio.h> // 40MS
int count;
void f(int i, int j);
int main(void)
{
int n, i, j; scanf("%d", &n);
while(n--)
{
scanf("%d%d", &i, &j);
count = ;
f(i, j);
printf("%d\n", count);
}
} void f(int i, int j)
{
if(i == j)
return;
count++;
if(i > j)
return f(i/, j);
else
return f(i, j/); }

2014年山东省第五届ACM大学生程序设计竞赛F题:Full Binary Tree的更多相关文章

  1. angry_birds_again_and_again(2014年山东省第五届ACM大学生程序设计竞赛A题)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2877 题目描述 The problems ca ...

  2. “浪潮杯”山东省第五届ACM大学生程序设计竞赛(总结贴)

    第一次參加省赛有点小激动,尽管是作为打星队參赛,但心情却是上下起伏. 5月9号晚上11点多到威海,有点略冷.可是空气比淄博好多了,大家到了旅馆的时候都非常晚了,抱怨了一下三星级的酒店的待遇,喝杯咖啡早 ...

  3. 2013年山东省第四届ACM大学生程序设计竞赛J题:Contest Print Server

    题目描述     In ACM/ICPC on-site contests ,3 students share 1 computer,so you can print your source code ...

  4. 2013年山东省第四届ACM大学生程序设计竞赛E题:Alice and Bob

    题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynom ...

  5. 2013年山东省第四届ACM大学生程序设计竞赛-最后一道大水题:Contest Print Server

    点击打开链接 2226: Contest Print Server Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 53  Solved: 18 [Su ...

  6. Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)

    Alice and Bob Time Limit: 1000ms   Memory limit: 65536K 题目描述 Alice and Bob like playing games very m ...

  7. 山东省第四届ACM大学生程序设计竞赛解题报告(部分)

    2013年"浪潮杯"山东省第四届ACM大学生程序设计竞赛排名:http://acm.upc.edu.cn/ranklist/ 一.第J题坑爹大水题,模拟一下就行了 J:Contes ...

  8. [2012山东省第三届ACM大学生程序设计竞赛]——n a^o7 !

    n a^o7 ! 题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2413 Time Lim ...

  9. sdut Mountain Subsequences 2013年山东省第四届ACM大学生程序设计竞赛

    Mountain Subsequences 题目描述 Coco is a beautiful ACMer girl living in a very beautiful mountain. There ...

随机推荐

  1. webservice作用(优,缺点;作用)

    1其实我们平时的应用,有一方面考虑是部署方便,维护容易~!如果是DLL,部署,更新需要每个应用了这个DLL的应用程序都作相应的引用更新...而如果用了Ws,则不用,因为它通过网络部署,通过网络引用,基 ...

  2. 入门servlet:request获取请求头数据

    @WebServlet("/RequestDemo2") public class RequestDemo2 extends HttpServlet { protected voi ...

  3. TZOJ 4024 游戏人生之梦幻西游(连续子段和绝对值最小)

    塔神酷爱玩梦幻西游这款游戏,这款游戏以著名的章回小说<西游记>故事为背景,透过Q版的人物,营造出浪漫的网络游戏风格.塔神以追求天下无敌为目标,从一个默默无闻的菜鸟,打拼到了登峰造极的大师, ...

  4. 【html、CSS、javascript-12】jquery-效果

    一.jQuery 效果- 隐藏和显示 通过 jQuery,您可以使用 hide() 和 show() 方法来隐藏和显示 HTML 元素: $("#hide").click(func ...

  5. 【python之路38】Python正则表达式匹配反斜杠“\”

    一.引入 在学习了Python特殊字符和原始字符串之后,我觉得答案应该是这样的: 1)普通字符串:'\\'2)原始字符串:r'\'但事实上在提取诸如“3\8”反斜杠之前的数字时,我屡次碰壁,始终得不到 ...

  6. leetcode 492-543 easy

    492. Construct the Rectangle Input: 4 Output: [2, 2] Explanation: The target area is 4, and all the ...

  7. []==![] 为什么等于true?

    最近碰到这样一个问题: []==![]  为什么等于true? 首先分析  !的优先级较==高,先运算==两侧的操作数: typeof []; //"object" typeof ...

  8. Mac安装软件新方法:Homebrew-cask

    Homebrew是Ruby社区极富想象力的一个作品,使得Mac下安装Mysql等常用包不再困难.那么,是否也可以通过brew install mysql这样简单的方式来安装chrome浏览器? 近期, ...

  9. git仓库远程连接GitHub

    1 先下载git for windows 然后一直点下一步安装. 2 安装之后,选择git gui.生成ssh 链接 git 公钥. 用命令也可以: $ ssh-keygen -t rsa -C &q ...

  10. linux系统级别的计划任务及其扩展anacrontab

    这个是系统设置好了,清理系统垃圾或者是自动执行某些脚本的系统任务,一般我们做了解就行了,不要更改配置文件是/etc/conrtab SHELL:就是运行计划任务的解释器,默认是bash PATH:执行 ...