time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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:
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
Copy
10 10 5 11 44 17 21 1 1 96 3 2 128 1001 1100611139403776 1000000000000000000 1000000000000000000 7 1 10 8
output
Copy
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).
解题说明:水题,先判断a与b之间是否为倍数关系,然后再判断a经过多少次转换能变成b。
#include
#include
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
long long int a, b;
scanf("%lld %lld", &a, &b);
long long int ans;
if ((a > b && a%b != 0) || (b > a && b%a != 0))
{
printf("-1\n");
}
else
{
if (a > b)
{
ans = a / b;
}
else
{
ans = b / a;
}
long long int ct = 0;
while (ans>1)
{
if (ans % 8 == 0)
{
ans = ans / 8;
ct++;
}
else if (ans % 4 == 0)
{
ans = ans / 4;
ct++;
}
else if (ans % 2 == 0)
{
ans = ans / 2;
ct++;
}
else
{
break;
}
}
if (ans == 1)
{
printf("%lld\n", ct);
}
else
{
printf("-1\n");
}
}
}
return 0;
}