NYNU_省赛选拔题(7)

时间:2021-06-26 21:02:44

题目描述

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

样例输入

2
1024 2048
3214567 9998877

样例输出

1
44
 #include <iostream>
using namespace std;
int f[];
int len;
void pre()
{
f[]=;
for(int i=;;i++){
f[i]=f[i-]*;
if(f[i]>1e9){
len=i-;
break;
}
}
}
int a,b;
int t;
int main()
{
pre();
while(cin>>t)
while(t--){
cin>>a>>b;
if(a>b)swap(a,b);
int cnt=;
int l;
for(int i=;i<len;i++){
if(a>=f[i]&&a<f[i+]){
l=i;break;}
}
while() {
if(b>=f[l]&&b<f[l+])
break;
b/=;
cnt++;
}
while(a!=b){
a/=;
b/=;
cnt+=;
}
cout<<cnt<<endl;
}
return ;
}