Description
我们可以通过对一个整数
A
进行加1操作或者乘2操作使其转换为另一个整数
B
。
给出两个整数
X
,
Y
,计算至少需要多少步才能将
X
转换为
Y
。
.
Input
输入的第一行包含一个整数
T
(1
≤
T
≤
5
00
),表示一共有
T
组测试数据。
每组测试数据占一行,包含两个整数
X
,
Y
(1
≤
X
≤
Y
≤
10
18
)。
Output
对于每组测试数据,输出至少需要多少步才能将
X
转换为
Y
。
Sample Input
3
1 1
3 10
2 11
Sample Output
0
3
4
#include<iostream> #include<cstdio> using namespace std; #define MAX(a,b) ((a)>(b) ? (a):(b)) #define MIN(a,b) ((a)<(b) ? (a):(b)) int main() { int t; scanf("%d",&t); while(t--) { long long x,y,sum=0; scanf("%lld %lld",&x,&y); if(x==y) {printf("0\n"); continue;} while(y/2>=x) { if(y%2==0) sum+=1; else sum+=2; y/=2; } sum+=y-x; printf("%lld\n",sum); } return 0; }