Description
如下图,由正整数1,2,3,…组成一棵无限大的满二叉树。从某一个结点到根结点(编号是1的结点)都有一条唯一的路径,比如10到根节点的路径是(10,5,2,1),由4到根节点的路径是(4,2,1),从根结点1到根结点的路径上只包含一个结点1,因此路径是(1)。
对于两个结点X和Y,假设它们到根结点的路径分别是(X1,X2,…,1)和(Y1,Y2,…,1)(这里显然有X=X1,Y=Y1),那么必然存在两个正整数i和j,使得从Xi和Yj开始,有Xi=Yj,Xi+1=Yj+1,…,现在的问题就是,给定X和Y,
要求Xi(也就是Yj)。
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Input
输入的第一行是一个整数T,表示测试用例个数。以下T行,每行对应一个测试用例。
每个测试用例包括两个整数X和Y,这两个整数都不大于1000。
Output
对每个测试用例,单独一行输出一个整数Xi。
Sample Input
1
10 4
7 13
Sample Output
2
/**
cin x y
vector<int> A B
i = x
for i != 1
A.push_back(i)
i = i%2 == 0 ? i/2 : (i+1)/2
and y
int number
for each j in A
for each k in B
if j == k
number = j
break
cout number
**/
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int cases;
scanf("%d", &cases);
while(cases--)
{
int x, y;
scanf("%d %d", &x, &y);
int Max = x >= y ? x : y;
int Min = x + y - Max;
int result = 0;
while(Min != 0) {
if(Min == Max) {
result = Min;
break;
}
int max = Max%2 == 0 ? Max/2 : (Max-1)/2;
if(Min < max)
Max = max;
else if(Min == max) {
result = Min;
break;
} else {
Max = Min;
Min = max;
}
}
printf("%d\n", result);
}
}