A - Johnny and Ancient Computer CodeForces - 1362A

CodeForces - 1362A 

Johnny has recently found an ancient, broken computer. The machine has only one register, which allows one to put in there one variable. Then in one operation, you can shift its bits left or right by at most three positions. The right shift is forbidden if it cuts off some ones. So, in fact, in one operation, you can multiply or divide your number by 22, 44 or 88, and division is only allowed if the number is divisible by the chosen divisor.

Formally, if the register contains a positive integer xx, in one operation it can be replaced by one of the following:

  • x⋅2x⋅2
  • x⋅4x⋅4
  • x⋅8x⋅8
  • x/2x/2, if xx is divisible by 22
  • x/4x/4, if xx is divisible by 44
  • x/8x/8, if xx is divisible by 88

For example, if x=6x=6, in one operation it can be replaced by 1212, 2424, 4848 or 33. Value 66 isn't divisible by 44 or 88, so there're only four variants of replacement.

Now Johnny wonders how many operations he needs to perform if he puts aa in the register and wants to get bb at the end.

Input

The input consists of multiple test cases. The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases. The following tt lines contain a description of test cases.

The first and only line in each test case contains integers aa and bb (1≤a,b≤10181≤a,b≤1018) — the initial and target value of the variable, respectively.

Output

Output tt lines, each line should contain one integer denoting the minimum number of operations Johnny needs to perform. If Johnny cannot get bb at the end, then write −1−1.

Example

Input

10
10 5
11 44
17 21
1 1
96 3
2 128
1001 1100611139403776
1000000000000000000 1000000000000000000
7 1
10 8

Output

1
1
-1
0
2
2
14
0
-1
-1

Note

In the first test case, Johnny can reach 55 from 1010 by using the shift to the right by one (i.e. divide by 22).

In the second test case, Johnny can reach 4444 from 1111 by using the shift to the left by two (i.e. multiply by 44).

In the third test case, it is impossible for Johnny to reach 2121 from 1717.

In the fourth test case, initial and target values are equal, so Johnny has to do 00 operations.

In the fifth test case, Johnny can reach 33 from 9696 by using two shifts to the right: one by 22, and another by 33 (i.e. divide by 44 and by 88).

题意:t组输入,每组给定两个数a和b,用*2,*4,*8,/2,/4,/8,都算一步操作,问a可以通过几步这样的操作得到b,若不能则输出-1.

题解:六种操作当然要尽量化简啦,可以让a和b互换,让a成为其中较小的一个。反正a通过乘法能得到b,b也能通过除法得到a,所以到底谁乘谁除是无所谓的,我们只要尽量简化操作就行。为了求最少的操作,当然是从*8,*4,*2依次进行的咯,直接看代码吧,这个没什么需要解释的了。

#include<iostream>
#include<algorithm>
using namespace std;

int t;
long long a, b;
int main(){
	scanf("%d", &t);
	while(t--){
		scanf("%lld%lld", &a, &b);
		if(a>b){long long tem=a;a=b;b=tem;}//使 a <= b
		if(b%a==0){
			long long cnt = 0;
			while(a<b)//a *= 8
				if(a*8 <= b){a*=8;cnt++;}
				else	break;
			while(a<b)//a *= 4
				if(a*4 <= b){a*=4;cnt++;}
				else	break;
			while(a<b)//a *= 2
				if(a*2 <= b){a*=2;cnt++;}
				else	break;
			
			if(a==b)	printf("%lld\n", cnt);
			else		printf("-1\n");
		}else
			printf("-1\n");
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值