Johnny and Ancient Computer (Codeforces 1362A)
Tags: Implement, implementation

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 $$$2$$$, $$$4$$$ or $$$8$$$, and division is only allowed if the number is divisible by the chosen divisor. Formally, if the register contains a positive integer $$$x$$$, in one operation it can be replaced by one of the following:   $$$x \cdot 2$$$  $$$x \cdot 4$$$  $$$x \cdot 8$$$  $$$x / 2$$$, if $$$x$$$ is divisible by $$$2$$$  $$$x / 4$$$, if $$$x$$$ is divisible by $$$4$$$  $$$x / 8$$$, if $$$x$$$ is divisible by $$$8$$$ For example, if $$$x = 6$$$, in one operation it can be replaced by $$$12$$$, $$$24$$$, $$$48$$$ or $$$3$$$. Value $$$6$$$ isn't divisible by $$$4$$$ or $$$8$$$, so there're only four variants of replacement.Now Johnny wonders how many operations he needs to perform if he puts $$$a$$$ in the register and wants to get $$$b$$$ at the end.

Input: The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The following $$$t$$$ lines contain a description of test cases.The first and only line in each test case contains integers $$$a$$$ and $$$b$$$ ($$$1 \leq a, b \leq 10^{18}$$$) — the initial and target value of the variable, respectively.

Output: Output $$$t$$$ lines, each line should contain one integer denoting the minimum number of operations Johnny needs to perform. If Johnny cannot get $$$b$$$ at the end, then write $$$-1$$$.

Note: In the first test case, Johnny can reach $$$5$$$ from $$$10$$$ by using the shift to the right by one (i.e. divide by $$$2$$$).In the second test case, Johnny can reach $$$44$$$ from $$$11$$$ by using the shift to the left by two (i.e. multiply by $$$4$$$).In the third test case, it is impossible for Johnny to reach $$$21$$$ from $$$17$$$.In the fourth test case, initial and target values are equal, so Johnny has to do $$$0$$$ operations.In the fifth test case, Johnny can reach $$$3$$$ from $$$96$$$ by using two shifts to the right: one by $$$2$$$, and another by $$$3$$$ (i.e. divide by $$$4$$$ and by $$$8$$$).

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

Sample Outputs:
1
1
-1
0
2
2
14
0
-1
-1

Solution (Python):
t = int(input())

def solve(a, b):
    if a == b:
        return 0
    elif a % b != 0 and b % a != 0:
        return -1
    if a > b:
        t = a
        a = b
        b = t
    r = a
    while r % 2 == 0:
        r //= 2
    g = b
    while g % 2 == 0:
        g //= 2
    if g != r:
        return -1
    ans = 0
    b //= a
    while b >= 8:
        b //= 8
        ans += 1
    while b >= 4:
        b //= 4
        ans += 1
    while b > 1:
        b //= 2
        ans += 1
    return ans
for _ in range(t):
    word = [int(a) for a in input().split()]
    first = word[0]
    second = word[1]
    print(solve(first, second))

Solution (Python):
for _ in range(int(input())):
    (a, b) = map(int, input().split())
    if a > b:
        (a, b) = (b, a)
    ops = 0
    while a < b:
        a <<= 1
        ops += 1
    if a == b:
        print((ops + 2) // 3)
    else:
        print('-1')

Solution (Python):
binary = lambda x: bin(x)[2:]
for _ in range(int(input())):
    (a, b) = map(int, input().split())
    if a < b:
        miv = a
        mav = b
    else:
        miv = b
        mav = a
    miv = binary(miv)
    mav = binary(mav)
    l = len(miv)
    if miv == mav[:l]:
        res = mav[l:]
        if res == '0' * len(res):
            ans = len(res)
            (a1, ans) = (ans // 3, ans % 3)
            (a2, ans) = (ans // 2, ans % 2)
            a3 = ans
            print(a1 + a2 + a3)
        else:
            print(-1)
    else:
        print(-1)