题目描述

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. 警告: [SetPropertiesRule]{Context/Loader} Setting property 'useSystemClassLoaderAsParent' to 'false' did not find a matching property.

    警告: [SetPropertiesRule]{Context/Loader} Setting property 'useSystemClassLoaderAsParent' to 'false' d ...

  2. jdbc原始连接

    public class JdbcInstrt { public static void main(String[] args) { Connection conn = null; PreparedS ...

  3. 微信小程序示例

    http://www.cnblogs.com/ihardcoder/p/6097941.html http://www.wxapp-union.com/ http://blog.csdn.net/li ...

  4. WPF 动画执行后属性无法修改

    在做了一个类似QQ展开的动画时,设置了TopProperty,通过改变Window.Top属性来实现展开特效, 但是动画执行了之后,再去设置Window.Top的时候发现修改不了,代码调试后发现值设置 ...

  5. 码云及git使用

    首次使用码云,将本地文件与之关联(创建仓库之后的页面截图) git -- 版本控制(协同开发软件) git add . # 将当前文件下所有内容添加到临时缓存区 git commit -m " ...

  6. Comparator进行List集合排序

    对数据库中查询到的结果进行排序,一般开发中,实体类是没有实现Comparable接口的,所以不能实现compareTo()方法进行排序, 只能用Comparator去进行排序,只需要在带排序的集合中加 ...

  7. List的深度序列化Demo

    今天项目中出现了这个问题...就是使用一个List去进行其他的操作,生成一个新的List.但是却将原来的List的值也给改了...这应该是引用传递的问题,查了资料发现这是浅拷贝造成的.(ps:使用ad ...

  8. linux查看现在在运行的进程 $ pstree -a$ ps aux

    这都是查看现有进程的. ps aux 的结果比较杂乱 pstree -a 的结果比较简单明了,可以看到正在运行的进程及相关用户.

  9. mvp例子与MVVM例子

    VMP例子 <!-- 从百度CDN上面找个jquery的链接 --> <!DOCTYPE html> <html lang="en"> < ...

  10. MR25H10-1Mb密度SPI串行接口MRAM

    everspin的MR25H10是一个1,048,576位磁阻随机存取存储器(MRAM)设备,由131,072个8位字组成.MR25H10提供串行EEPROM和串行闪存兼容的读/写时序,没有写延迟,并 ...