We need to find the minimum number of operations to transform a into b using allowed ops: multiply or divide by 2,4,8 (i.e., shift left or right by up to 3 bits). Division only allowed if divisible.
We need to handle up to 1000 test cases, a,b up to 1e18.
Observations: The operations are essentially: multiply/divide by 2^k where k ∈ {1,2,3}. But we can combine multiple operations to achieve any product of powers of 2, but each factor's exponent is ±1, ±2, ±3 per step. However we can also have net effect of multiplication by any power of two that can be expressed as sum of these steps, but also can intermix division.
But there is also constraint: at each step we must have integer, and division only if divisible. So effectively we need to find a sequence of multiplications/divisions by 2,4,8 that changes a to b.
This is reminiscent of reducing by removing factors of 2 and then adjusting? Actually, the allowed factors are only powers of 2 (2,4,8). So any operation changes the number by multiplying or dividing by a power of 2. This means the odd part (the part not divisible by 2) cannot change at all, because multiplying/dividing by a power of two only affects the exponent of 2 in prime factorization. So the odd part must remain constant for transformation to be possible. Let’s denote a = a_odd * 2^e_a, b = b_odd * 2^e_b, where a_odd and b_odd are odd integers. Then we can only multiply/divide by powers of two, so the odd part stays same. Thus necessary condition: a_odd == b_odd (i.e., a and b have same odd part). Otherwise answer -1.
If that condition holds, then it's about changing the exponent of 2: we need to go from exponent e_a to e_b using steps of adding or subtracting 1,2,3 (with subtraction allowed only if current exponent >= the amount subtracted? Actually division by 2^k allowed only if number divisible by 2^k, i.e., current exponent >= k). So it's like we have an integer e (the exponent of 2) and we can add any d ∈ {+1,+2,+3} (multiply) or subtract any d ∈ {1,2,3} provided that after subtracting we don't go negative? Actually the condition is that the current number is divisible by 2^d. That means the current exponent e_cur must be at least d. So if we represent the state by the exponent e (non-negative integer, can be zero or positive), then we start at e_a, want to reach e_b. In each step we can change e by adding or subtracting an integer in {1,2,3}, with the constraint that we never go below 0 when subtracting (since division by 2^d is only allowed if number divisible, which is equivalent to e_cur >= d; after subtraction, new exponent = e_cur - d, which could be 0 or positive). So we need to find minimum steps to go from e_a to e_b using steps of size 1,2,3 in either direction, with no other restrictions.
That is essentially a shortest path on a line with step sizes 1,2,3 positive or negative. Since there's no cost difference between adding and subtracting? Each operation counts as 1. So it's like we can adjust by any amount in [-3,-2,-1] ∪ [1,2,3] per step, but subtraction only allowed if we have enough exponent, but since exponent can't go negative, we must ensure we don't subtract below 0. However, if e_a and e_b are both non-negative integers, we can consider path that may go below 0? Not allowed. But we can avoid going below 0 by ordering operations. But we can always first apply multiplications (increase exponent) if needed to avoid negative? Actually we want min steps. This is like we need to achieve a net change Δ = e_b - e_a. However, we can also overshoot and then come back, but overshooting might require extra steps. So the minimal number of steps is the minimal number of moves of size 1,2,3 (either direction) to achieve net Δ, but with the possibility that intermediate e can't be negative. However, if we are only subtracting, we must ensure we never need to subtract more than current e. But if Δ is negative (we need to reduce exponent), we can just subtract appropriate steps. But if we need to subtract but the total subtraction needed is more than e_a, we might need to first increase exponent (to add more factors of 2) and then subtract? That seems counterproductive: to reduce the exponent, you wouldn't want to increase it first; you could just subtract directly if e_a >= e_b, then you can subtract the difference in steps of up to 3. However, if e_a < e_b, we need to increase. No scenario would require increasing when we want to reduce because that would increase net difference. So the minimal steps for achieving net Δ is simply: we want to achieve Δ by sums of ±1, ±2, ±3, with the restriction that when subtracting we need enough current exponent? But when we do a subtraction step, we must have at least the amount we subtract. If we are trying to achieve a net negative Δ, we start at e_a, and each subtraction reduces e. If we plan to subtract a total of S (sum of sizes of subtraction steps) and also perhaps some addition steps, the net Δ = (additions - subtractions). For a path that never goes negative, we need that at each point the cumulative subtractions from start (including previous subtractions) does not exceed current exponent after considering previous additions. That is akin to the prefix sum of subtractions <= e_a + prefix sum of additions. But we can always order operations to satisfy this if e_a is sufficiently large? For minimal steps, we likely wouldn't intermix addition and subtraction unnecessarily if net Δ is of one sign. But sometimes mixing may reduce steps? For instance, suppose e_a=0, e_b=1. Then we can multiply by 2 in one step: net +1, done. That's fine.
But consider e_a=0, e_b=5. We can do multiply by 8 (+3) then multiply by 4 (+2)? Wait 8 then 4 gives +3+2=5, two steps. That's allowed. e_a=0, after first multiply e=3, second multiply e=5. OK.
Consider e_a=1, e_b=0. Subtract 1 (divide by 2) in one step, provided number divisible by 2. At start e_a=1, number is even, so divisible by 2. So one step. Good.
Consider e_a=2, e_b=0. We can subtract 2 (divide by 4) in one step, if divisible by 4. Since exponent 2 means number divisible by 4, yes. So one step. Or subtract 1+1 =2 steps but better is 1 step. So minimal steps: we can do a division by 2^k where k up to 3 as long as exponent >= k. So we can reduce by up to 3 per step.
Now consider e_a=0, e_b=0: 0 steps.
Now consider e_a=0, e_b=2: multiply by 4 (+2) in one step: allowed.
Thus for net Δ, minimal steps seems to be the minimal number of steps using steps of size up to 3 to achieve exactly Δ, because we can always choose operations that respect the divisibility constraint if we only go in the direction of net Δ (i.e., if Δ positive, we only multiply; if Δ negative, we only divide). For division, we must ensure that at each step we are dividing by a power of two that the current number is divisible by. But if we start at e_a and we only perform divisions, each division reduces exponent by k (1,2,3). The condition is that at each step the current exponent >= k. If we plan a sequence of divisions that sums to D = e_a - e_b (positive) to reduce exponent by D, we need to ensure that we can partition D into parts each ≤3 such that at each step the remaining exponent never goes below the part we subtract. But if we subtract in decreasing order (largest first), we might need to be careful: Suppose e_a=5, we want to go to e_b=0, so we need to subtract total 5. We could do subtract 3 (now e=2), then subtract 2 (now e=0). At step 2, we need to subtract 2, and e=2 is >=2, so okay. Another partition: 2+3: first subtract 2 (e=3), then subtract 3 (e=0) ok. 1+1+3: first subtract 1 (e=4), then subtract 1 (e=3), then subtract 3 (e=0) ok. So any partition works because the exponent after each subtraction is e_a - sum so far, and we require that each subtraction k ≤ current exponent, i.e., e_a - sum_{prev} >= k. But sum_{prev}+k <= e_a? Actually condition: before subtracting k, current exponent = e_a - sum_{prev}. Need e_a - sum_{prev} >= k => sum_{prev} + k <= e_a. Since total sum D = e_a - e_b, and we are doing steps until total sum D, we will have sum_{prev}+k ≤ e_a. But since total D could be less than e_a? For reduction to e_b >0? Actually if e_b>0, then D = e_a - e_b < e_a, but we stop when total subtractions = D. Then at final step, sum_{prev}+k = D ≤ e_a. So condition sum_{prev}+k ≤ e_a. This holds for all steps if we ensure that at each step, the amount we subtract does not exceed the remaining exponent to be removed plus e_b? Wait careful: Let's formalize.
We start with exponent E0 = e_a. We want to reach e_b (with e_b ≤ e_a). We will perform a sequence of divisions (subtractions). At step i, we subtract d_i ∈ {1,2,3} provided that current exponent before subtraction, E_{i-1}, is ≥ d_i. After subtraction, E_i = E_{i-1} - d_i. We want after m steps, E_m = e_b.
We need to choose d_i such that sum_{i=1}^m d_i = e_a - e_b = D. And for all i, E_{i-1} ≥ d_i, i.e., e_a - sum_{j=1}^{i-1} d_j ≥ d_i, i.e., sum_{j=1}^{i-1} d_j + d_i ≤ e_a.
Since sum_{j=1}^{i} d_j ≤ e_a? Actually because total sum D may be less than e_a, but at intermediate steps we could have sum_{j=1}^{i} d_j > e_a? That would require that we attempted to subtract more than initial exponent, which would violate condition because at that point exponent would be negative before subtraction? Actually the condition is E_{i-1} >= d_i, i.e., e_a - sum_{j=1}^{i-1} d_j >= d_i => sum_{j=1}^{i-1} d_j + d_i <= e_a. So the cumulative sum after step i must be ≤ e_a. But we ultimately want cumulative sum = D ≤ e_a. So as long as we never let the cumulative sum exceed e_a, it's fine. Since D ≤ e_a, we can always choose a partition of D into ≤3 steps? But is it always possible to schedule the divisions in an order such that each d_i ≤ current exponent? Suppose e_a=1, e_b=0. D=1, we can do d=1. Condition: before step, exponent=1 ≥1 ok.
Suppose e_a=2, e_b=0, D=2. Could we do d=2? Yes, since 2≥2. Could we do d=1 then d=1? Yes, first step exponent=2≥1, second step exponent=1≥1. So fine.
Suppose e_a=3, e_b=0, D=3. Could we do d=3? exponent=3≥3 ok. Could do 2+1: first exponent=3≥2, then exponent=1≥1. Or 1+2: first exponent=3≥1, then exponent=2≥2. All good.
Suppose e_a=4, e_b=1 (D=3). Need to subtract total 3 to reach exponent 1. Could do d=3 (exponent 4≥3, then exponent=1) ok. Could also do 2+1 etc. So fine.
Thus any partition of D into 1,2,3 steps will satisfy the divisibility condition as long as we do not try to subtract more than the current exponent. But is there any case where some partition fails? Suppose we want D=4, e_a=4, e_b=0. Could do d=3 then d=1? Check: first step: exponent 4≥3, becomes 1. Second step: exponent 1≥1, becomes 0. Ok. d=2+2: first: 4≥2 ->2, second: 2≥2 ->0. d=1+3: first:4≥1 ->3, second:3≥3 ->0. d=4 not allowed because max step is 3. So we need at least two steps. All partitions work.
Suppose e_a=5, e_b=1, D=4. Could do 3+1: first 5≥3 ->2? Wait after 3 becomes 2, then need to subtract 1 to reach 1? Actually target exponent is 1, we want after steps to be 1. Starting 5, subtract 3 ->2, then subtract 1 ->1. That's total 4, works. Could do 2+2: 5≥2 ->3, then 3≥2 ->1. Works. Could do 1+3: 5≥1 ->4, then 4≥3 ->1. Works. So fine.
Thus for reduction, any sequence of subtractions that sums to D and each step ≤ current exponent is feasible. But is there any scenario where a partition would cause an intermediate step where the current exponent is less than the next d_i? For example, if we schedule large subtraction after small ones, it might still be okay as long as the remaining exponent is at least that d_i. Since the remaining exponent after i-1 steps is e_a - sum_{j=1}^{i-1} d_j. To subtract d_i, we need that remaining >= d_i. This is always satisfied if sum_{j=1}^{i-1} d_j + d_i ≤ e_a, i.e., cumulative sum up to i ≤ e_a. Since total D ≤ e_a, cumulative sums are ≤ D ≤ e_a, so it's automatically satisfied? Wait: At intermediate step i, sum_{j=1}^{i} d_j could be less than D, but is it guaranteed to be ≤ e_a? Since e_a may be larger than D, but sum_{j=1}^{i} d_j ≤ D ≤ e_a, so indeed sum_{j=1}^{i} d_j ≤ e_a. Therefore the condition e_a - sum_{j=1}^{i-1} d_j ≥ d_i is equivalent to sum_{j=1}^{i-1} d_j + d_i ≤ e_a. Since sum_{j=1}^{i} d_j ≤ D ≤ e_a, it's always true. So for any prefix sum ≤ e_a, condition holds. Since D ≤ e_a, every prefix sum is ≤ D ≤ e_a. So indeed any sequence of subtractions (each ≤3) that sums to D works. Similarly, for increases (multiplications), there is no restriction; we can always multiply by any power of two up to 8, increasing exponent by 1,2,3 regardless of current exponent. So for net positive Δ, we can just do multiplications.
Thus the divisibility constraint does not impose additional restrictions beyond the necessary condition that a_odd == b_odd. Because if a_odd == b_odd, then transformation is possible if and only if we can adjust exponent from e_a to e_b using steps of ±1,±2,±3. But wait, there might be an edge case: a and b could be huge (up to 1e18), but exponents could be up to 60 (since 2^601e18). That's fine.
Now, is there any possibility that we need to mix multiplications and divisions to achieve the exponent change with fewer steps than just going directly? For example, suppose e_a=0, e_b=1. Direct: one multiplication by 2. That's 1 step. No mixing.
Suppose e_a=0, e_b=4. Direct: we can multiply by 8 (+3) then divide by 4? That would be +3-2=+1 net? Not 4. To get +4, we could do multiply by 4 (+2) then multiply by 4 (+2): 2 steps. Could we do multiply by 8 (+3) then divide by 2 (-1) gives +2? Not 4. Could we multiply by 8 (+3) then multiply by 8 (+3) gives +6 then divide by 4 (-2) gives +4 in 3 steps, worse. So direct is best.
But could mixing reduce steps for some Δ? Consider we can overshoot and then come back. For instance, if Δ = 5, direct: 3+2 (2 steps) works. Could we do 8-3? That's 5 net? 8 is not an operation; we can only use steps of up to 3. So overshoot by +3 then -? But if we overshoot, we would need to then divide, but dividing reduces exponent, so net might be achieved in 2 steps as well? For Δ=5, suppose we do +3 then +3 =6, then -1 =5, that's 3 steps. So not better.
What about Δ=4: direct: +2+2 (2 steps). Could we do +3 then +? Actually +3+3=6 then -2=4 (3 steps). Not better.
Δ=3: direct: one step.
Δ=6: direct: +3+3 (2 steps) or +2+2+2 (3 steps) so best 2 steps.
Δ=7: direct: +3+3+1 (3 steps) or +3+2+2 (3 steps) or +3+3+? Actually need sum 7 with 1,2,3. Minimum number of steps: ceil(7/3)=3 (since max per step 3). 3 steps can sum to 7 (3+2+2, or 3+3+1). So 3 steps. Could we do +8? not allowed.
Δ=8: min steps = ceil(8/3)=3 (3+3+2). 3 steps.
Δ=9: ceil(9/3)=3, (3+3+3). 3 steps.
So for positive Δ, min steps = minimum number of terms (each 1..3) that sum to Δ. That's essentially: let steps = ceil(Δ/3)? Not exactly because we could use combinations to fill any integer Δ ≥0. For Δ=1: ceil(1/3)=1 works. Δ=2: ceil(2/3)=1? Actually ceil(2/3)=1? That's not correct because 1 step max is 3, but we need sum 2, which can be done in 1 step (by multiply by 4? Wait multiply by 4 adds 2 to exponent, yes we can use step size 2. So 1 step is enough. So min steps is not simply ceil(Δ/3) because we can use step sizes 1 or 2 as well. Actually the minimum number of steps to achieve sum Δ using numbers from {1,2,3} is indeed ceil(Δ/3) when Δ is not 1? Let's test: Δ=4: ceil(4/3)=2, indeed we need 2 steps (2+2 or 3+1). Δ=5: ceil(5/3)=2, works (3+2). Δ=6: ceil(6/3)=2, works (3+3). Δ=7: ceil(7/3)=3, works. Δ=8: ceil(8/3)=3, works. Δ=9: ceil(9/3)=3, works. Δ=10: ceil(10/3)=4, but can we do 3+3+2+2? That's 4 steps. Is there a way in 3 steps? Max sum in 3 steps is 9. So 3 steps impossible, so min steps=4. So indeed min steps = ceil(Δ/3) for Δ>0? Check Δ=2: ceil(2/3)=1, correct. Δ=1: ceil(1/3)=1, correct. Δ=0: 0 steps. So for non-negative Δ, min steps = ceil(Δ/3). But wait Δ=4: ceil(4/3)=2, correct. So formula holds? Let's test edge: Δ=3: ceil(3/3)=1, correct. So yes, because the maximum sum in k steps is 3k. To achieve Δ, we need the smallest k such that 3k ≥ Δ, i.e., k ≥ Δ/3, so k = ceil(Δ/3). But is it always possible to achieve any integer Δ with exactly k steps where each step is between 1 and 3? Yes, because we can fill with 3's and adjust with 1 or 2 to get exact sum, as long as Δ is between k and 3k inclusive. Since k = ceil(Δ/3), we have Δ ≤ 3k and also Δ ≥ k because if k = ceil(Δ/3), then Δ/3 ≤ k => Δ ≤ 3k, and also since k-1 < Δ/3 => Δ > 3(k-1) => Δ ≥ 3k-2? Actually we need Δ ≥ k (minimum sum with k steps is k). Is it guaranteed that Δ ≥ k when k = ceil(Δ/3)? For Δ≥1, yes: Since Δ/3 ≤ k => Δ ≤ 3k, but we also need Δ ≥ k. For k= ceil(Δ/3), is it possible that Δ < k? That would mean Δ < ceil(Δ/3) => for Δ≥1, Δ/Δ? Let's test small: Δ=1, k=1, ok. Δ=2, k=1, ok. Δ=3, k=1, ok. Δ=4, k=2, 4 ≥ 2, ok. Δ=5, k=2, 5 ≥ 2, ok. Δ=6, k=2, 6 ≥ 2, ok. Δ=7, k=3, 7 ≥ 3, ok. So it holds because for any positive Δ, ceil(Δ/3) ≤ Δ (since Δ/3 ≤ Δ for Δ>0, but ceil can be larger? Actually if Δ=1, ceil(1/3)=1, equal. If Δ=2, ceil(2/3)=1 <2. So indeed Δ ≥ ceil(Δ/3) for all Δ≥1? For Δ=1, 1≥1 true. Δ=2, 2≥1 true. So always holds because ceil(Δ/3) ≤ Δ when Δ≥1? Check Δ=1: 1 ≤1, yes. For Δ=2: ceil(2/3)=1 ≤2. For Δ=3:1≤3. For Δ=4:2≤4. So yes. So existence is guaranteed.
Thus for positive Δ, min steps = ceil(Δ/3). For negative Δ, i.e., we need to reduce exponent, D = e_a - e_b >0, we need to subtract D. We can do division steps. Similarly, the minimum number of division steps is ceil(D/3). Because each division can reduce exponent by at most 3, and we can use sizes 1,2,3. So min steps = ceil(|Δ|/3) when Δ negative? But careful: if we need to go down, we can only divide, but we must ensure that we never need to divide by more than available exponent. However as argued, any partition of D into steps ≤3 works as long as total D ≤ e_a. But D = e_a - e_b ≤ e_a, so yes. So min steps = ceil(D/3) = ceil(|e_a - e_b|/3).
But is there any scenario where mixing multiplications and divisions could yield fewer total steps than just going directly? Consider if we need to go from e_a to e_b where e_a < e_b (need to increase) but maybe e_a is very small, and we could first divide (if possible) to increase later? That would increase the needed net increase, so not beneficial. Alternatively, if e_a > e_b (need to decrease) but maybe we could first multiply to make the division steps more efficient? That would increase net decrease needed, so extra steps. So mixing is never beneficial. So min steps = ceil(|e_a - e_b| / 3).
But wait: Are there constraints on using division if the number is not divisible by 2,4,8? That depends on the current number's exponent. If we need to divide, we can only divide by 2^k if the current number is divisible by 2^k. That is exactly the condition that the current exponent >= k. In our exponent model, that's exactly the condition that before subtracting k, e_cur >= k. And as we argued, for any sequence of subtractions that sums to D (≤ e_a), we can always order the steps such that the condition holds. But is there any nuance: The condition also requires that the number is divisible by 2^k, which depends on the entire number, not just exponent? Since the odd part remains unchanged, divisibility by 2^k is equivalent to exponent >= k. So indeed the condition reduces to exponent condition.
Thus the answer seems to be: if a_odd != b_odd, then -1; else compute e_a = exponent of 2 in a (i.e., v2(a)), e_b = v2(b); then answer = ceil(|e_a - e_b| / 3). However, we need to double-check the sample inputs.
Sample:
-
10 5: a=10=52^1, b=5=52^0. odd part 5 both, e_a=1, e_b=0, diff=1, ceil(1/3)=1 => output 1. matches.
-
11 44: 11 is odd, b=44=114? 44/11=4 => 114 =44, 4=2^2. So a_odd=11, b_odd=11 (since 44=11*4). e_a=0 (11 odd), e_b=2, diff=2, ceil(2/3)=1? But answer given is 1. Yes, because one multiply by 4. That matches ceil(2/3)=1.
-
17 21: a=17 odd, b=21 odd, but are odd parts equal? 17 vs 21, not equal. So -1. matches.
-
1 1: diff 0 => 0.
-
96 3: 96=332? 96/3=32 => 32^5, 3 is odd? 3 is odd. So a_odd=3? Actually 96=3*32, yes odd part 3. b=3 odd part 3. e_a=5 (since 32=2^5), e_b=0, diff=5, ceil(5/3)=2 (since 5/3≈1.666, ceil=2). Output 2. matches.
-
2 128: a=2=12^1? Actually 2 = 12^1, odd part 1. b=128=1*2^7, odd part 1. diff=6, ceil(6/3)=2, output 2. matches.
-
1001 1100611139403776: Let's compute: a=1001, b= huge. Need to check odd part. Let's factor: 1001 = 71113? Actually 71113=1001. So odd part is 1001 (odd). b = 1100611139403776. We need to see if it's a multiple of 1001 times a power of two. b / 1001? Let's compute approximate: 1001 * 2^? = b. b looks like maybe 1001 * 2^something. Let's test: 2^40 ≈ 1.0995e12, 2^50 ≈ 1.1259e15, 2^? Actually 1e15. b is about 1.1006e15? Wait 1100611139403776 is about 1.1006e15. 2^50 = 1.1259e15, close. 2^50/1001 ≈ 1.1259e15/1001 ≈ 1.124e12, not matching. But we can compute v2: b must be divisible by 1001 and after dividing by 1001, the quotient must be a power of two (since odd part must match). Let's compute b mod 1001? Actually we trust sample: answer 14. Let's compute diff exponent: a_odd=1001, e_a=0 (since 1001 odd). b_odd must equal 1001, so b = 1001 * 2^e_b. So e_b = log2(b / 1001). Compute b / 1001. Using calculator? We'll approximate: 1001 * 2^e = b => 2^e = b/1001. b = 1100611139403776. Divide by 1001: 1100611139403776 / 1001. 1001 ≈ 10^3, so quotient ≈ 1.1006e12. 2^40 = 1.0995e12, 2^40 = 1099511627776. 2^40 = 1,099,511,627,776. Multiply by 1001: 1001 * 2^40 = 1001 * 1099511627776 = 10011.099511627776e12 = about 1.100,??? Let's compute precisely: 1,099,511,627,776 * 1000 = 1,099,511,627,776,000; plus another 1,099,511,627,776 = 1,100,611,155,403,776? Wait that sum gives 1,100,611,155,403,776. But b given is 1,100,611,139,403,776? Actually sample: "1001 1100611139403776". Let's write it: 1,100,611,139,403,776? The number: 1100611139403776. Counting digits: 1100611139403776 - I see 16 digits? 1,100,611,139,403,776 has 16 digits? Let's count: 1 1 0 0 6 1 1 1 3 9 4 0 3 7 7 6 => 16 digits. 2^40 * 1001 would be: 2^40 = 1,099,511,627,776 (13 digits). Multiply by 1001 yields 16 digits? 1,099,511,627,776 * 1001 = 1,099,511,627,7761000 + same = 1,099,511,627,776,000 + 1,099,511,627,776 = 1,100,611,139,403,776. Yes that matches b exactly: 1100611139403776 (maybe missing some commas) -> 1,100,611,139,403,776. So indeed b = 1001 * 2^40. So e_b = 40. e_a = 0. diff = 40. ceil(40/3) = ceil(13.333...) = 14. Output 14. matches.
All sample outputs align.
Thus solution seems straightforward: For each test case:
-
Compute the odd parts: divide a and b by largest power of 2 (i.e., remove all factors of 2). Equivalent to while a%2==0: a/=2; same for b. Alternatively compute a / (2^{v2(a)}), b / (2^{v2(b)}). Then compare if these odd parts are equal.
-
If not equal, output -1.
-
Else, compute diff = abs(v2(a_original) - v2(b_original)). Then answer = (diff + 2) // 3? Because ceil(diff/3) = (diff + 2)//3 in integer arithmetic.
Edge cases: a and b up to 1e18, so v2 up to about 60. So diff is small. Fine.
But is there any scenario where we might need more steps due to intermediate numbers exceeding 1e18? Not relevant.
But is the condition a_odd == b_odd sufficient? Let's double-check some tricky cases.
What about a=1, b=2? a_odd=1, b_odd=1? 2=1*2, odd part 1, same. e_a=0, e_b=1, diff=1, ceil=1. Yes we can multiply by 2.
What about a=2, b=1? odd part both 1, e_a=1, e_b=0, diff=1, ceil=1. Yes divide by 2.
What about a=8, b=4? 8=18, odd part 1; 4=14, odd part 1. e_a=3, e_b=2, diff=1, ceil=1. Can we do one operation? Divide by 2? 8/2=4, yes. So 1 step.
a=8, b=2? diff=1? Actually e_a=3, e_b=1, diff=2. ceil(2/3)=1. Can we do one operation? Divide by 4? 8/4=2, allowed (divisible by 4). So 1 step.
a=8, b=1? e_a=3, e_b=0, diff=3, ceil=1. Divide by 8? allowed. So 1 step.
a=6, b=3? 6=3*2, odd part 3; 3 odd part 3. e_a=1, e_b=0, diff=1, ceil=1. Divide by 2 allowed => 3.
a=6, b=12? diff=1, multiply by 2 =>12.
a=6, b=24? diff=2, multiply by 4 => 24? 6*4=24, allowed? Multiply by 4 is allowed as an operation. So 1 step.
a=6, b=48? diff=3, multiply by 8 =>48, 1 step.
a=6, b=96? e_a=1, e_b? 96=616? Actually 616=96, 16=2^4, so e_b=4? But odd part 6? Wait 6 has odd part 3? Actually 6=32, odd part 3. 96=332, odd part 3. So e_a=1, e_b=5? 96=32^5? 332=96, yes e_b=5. diff=4, ceil(4/3)=2. Can we do in 2 steps? Multiply by 8 (+3) gives 48, then multiply by 2 (+1) gives 96? But 482=96, but is multiply by 2 allowed? Yes. So steps: 6->48 (multiply by 8), then 48->96 (multiply by 2) = 2 steps. Also could multiply by 4 then by 4: 6->24,24->96? 244=96, yes. So 2 steps.
Thus works.
Now, consider a case where a_odd == b_odd but the difference in exponents is such that direct steps may be impossible due to divisibility constraints when mixing? But we argued direct (all in one direction) works. However, consider a scenario where we need to reduce exponent, but the required reduction D is not achievable in ceil(D/3) steps because of the constraint that each division must be by 2,4,8 and the number must be divisible. But as argued, for any partition of D into sums of 1..3, we can do it in that many steps. But is it always possible to choose the division amounts such that at each step the number is divisible by the chosen divisor? For division by 2^k, we need that the current number has at least k factors of 2. At each step, the current exponent is decreasing. The partition we choose must respect that at each step, the amount we subtract (k) is ≤ the current exponent before subtraction. This is equivalent to ensuring that the cumulative sum of subtractions up to that point plus k ≤ initial exponent e_a (since current exponent = e_a - sum_prev). Since total D ≤ e_a, any prefix sum ≤ D ≤ e_a, so condition reduces to sum_prev + k ≤ e_a. Since sum_prev + k = sum up to current step ≤ D ≤ e_a, it holds. So any partition works. But is it possible that we cannot achieve exactly D in k steps if D is not between k and 3k? But we choose k = ceil(D/3). Then D ≤ 3k, and D ≥ k? For D small, say D=1, k=1, works. D=2, k=1? Wait for D=2, ceil(2/3)=1. But with 1 step we can subtract at most 3, but we need to subtract exactly 2. That's fine, we can choose to divide by 4 (subtract 2). So we can achieve D=2 in 1 step. So condition is that there exists a combination of numbers each ∈{1,2,3} that sum to D in exactly k steps. For k=ceil(D/3), D is between k and 3k inclusive. But is it always possible to represent D as sum of k numbers each 1..3? Yes, because we can start with all ones (sum = k) and then increase some to 2 or 3 to reach D, as long as the total increase does not exceed (3-1)*k = 2k. Since D - k ≤ 2k (because D ≤ 3k => D - k ≤ 2k). So it's possible. So for any D, we can find a representation. So no issue.
Thus answer appears to be as described.
But is there any subtlety with large numbers when a and b are equal? fine.
Thus solution: For each test case:
-
Compute v2(a) and v2(b). While a%2==0, count; store oddA = a after removing factors of 2; similarly for b.
-
If oddA != oddB, print -1.
-
else diff = abs(v2(a) - v2(b)); answer = (diff + 2) // 3.
But careful: For the original a,b, we need to compute v2(a) and v2(b) before altering them. So we can compute:
a_odd = a
e_a = 0
while a_odd % 2 == 0:
a_odd //= 2
e_a += 1
Similarly for b.
Then compare a_odd and b_odd.
Alternatively, we could compute using bit tricks: e_a = trailing_zeros(a), etc. But Python's integers can handle loops fine for up to 60 iterations.
Given t up to 1000, it's fine.
But wait, a and b up to 1e18, but Python int can be large.
Potential pitfalls: When a=0? Not possible because a,b ≥ 1.
Now, check one more: a=3, b=24. a_odd=3, b_odd? 24=3*8, odd part 3. e_a=0, e_b=3, diff=3, ceil(3/3)=1, answer 1. Indeed multiply by 8.
All good.
But is there any scenario where a_odd == b_odd but we cannot achieve transformation because the exponent difference is such that we might need to go through a number that exceeds 10^18? Not a problem; the computer can hold any integer? The problem doesn't impose a limit on register size; it's just a theoretical computer with one register. So we assume no overflow. So it's fine.
Thus solution is straightforward.
But wait, the sample includes case: 1001 to that huge number, diff=40, answer 14. So works.
Thus answer algorithm.
But let's double-check: The minimal number of steps formula is indeed ceil(|e_a - e_b| / 3). But are there cases where we can use zero steps? when diff=0.
Edge: diff=0 => 0.
Now, let's test some random cases manually:
a=12 (12=3*4, e_a=2), b=3 (e_b=0). diff=2, ceil=1. Can we do one step? Divide by 4: 12/4=3, yes. Good.
a=12, b=6: odd parts? 12=34, b=6=32. odd parts both 3. e_a=2, e_b=1, diff=1, ceil=1. Divide by 2: 12/2=6, yes.
a=12, b=24: e_a=2, e_b=3, diff=1, ceil=1. Multiply by 2: 12*2=24.
a=12, b=48: e_a=2, e_b=4, diff=2, ceil=1? Multiply by 4: 124=48, yes 1 step. Wait e_b=4? 48=316 => 32^4, so e_b=4. So diff=2, ceil=1, but multiply by 4 adds 2 to exponent, indeed 124=48. So 1 step.
a=12, b=96: e_a=2, e_b=5, diff=3, ceil=1? Multiply by 8: 12*8=96, yes 1 step.
a=12, b=192: e_a=2, e_b=6? 192=364, 2^6=64, so diff=4, ceil(4/3)=2. Options: multiply by 8 (->96, e=5) then multiply by 2 (->192, e=6) =2 steps. Or multiply by 4 then by 4: 124=48(e=4), 48*4=192(e=6) also 2 steps. Good.
So far so good.
Now, let's think if there is any case where we need to consider the fact that division is only allowed if the number is divisible by that power of two. Our exponent model ensures it's okay as long as we don't try to divide by more than current exponent. The direct path (only divisions) never tries to divide by more than the current exponent because the cumulative sum of divisions never exceeds initial exponent. But if we do divisions in some order, could there be a case where we try to divide by, say, 4, but the current number's exponent is only 1? That would happen if we attempted to subtract 2 when the current exponent is 1. In a partition that includes a 2 after we've already subtracted enough to bring exponent down to 1, then we would be trying to divide by 4 when the current number only has one factor of 2, i.e., not divisible by 4. But does our representation guarantee that we can avoid such sequences by ordering the division amounts appropriately? Because we can choose the order of operations. For a given multiset of division amounts (k_i) summing to D, we need to find an ordering such that before each division, the current exponent is at least the amount. Since we can decide the order, we can always choose to do larger divisions first (or more generally, we can order them descending). Because if we do larger subtractions first, the exponent is highest at the start, so condition holds. Let's prove: Suppose we have amounts d1,...,dm with each 1≤di≤3, sum D. If we sort them in non-increasing order, then before the i-th division, the remaining exponent is e_a - sum_{j=1}^{i-1} d_j. Since the d_j are sorted descending, we have d_i ≤ d_{i-1} ≤ ... ≤? Actually we need to ensure that e_a - sum_{j=1}^{i-1} d_j ≥ d_i. Since the sum_{j=1}^{i-1} d_j + d_i ≤ total sum D (because sorted descending doesn't guarantee that sum_{prev}+d_i ≤ e_a, but we only know D ≤ e_a, and sum_{prev}+d_i ≤ D (since sum_{prev}+d_i is part of total sum? Actually sum_{prev}+d_i is at most D because it's a prefix of the sorted list, but since the list may include numbers that after sorting, the prefix sum is some value ≤ D. But we need to guarantee that sum_{prev}+d_i ≤ e_a, which is true because sum_{prev}+d_i ≤ D ≤ e_a. So indeed, any ordering works because the prefix sum never exceeds D, and D ≤ e_a. So as long as D ≤ e_a, any ordering works. However, if we accidentally pick an ordering that tries to subtract more than the current exponent? For a given set of steps, we can always reorder. So the existence of some ordering yields a valid sequence. Since we can choose the order, we can always do them in any order, but careful: Actually if we pick an ordering, we need to ensure that before each division, the current exponent is at least the amount. The condition is sum_{prev} + d_i ≤ e_a. Since sum_{prev} is the sum of the amounts already subtracted, and d_i is the next amount, we need sum_{prev}+d_i ≤ e_a. But sum_{prev}+d_i is at most the total sum of the steps we will eventually do? But if we reorder, the set of amounts is fixed. For the ordering, sum_{prev} is the sum of the amounts of the steps already performed. At the moment of performing step i, sum_{prev} could be any subset sum of the multiset. To guarantee that there exists an ordering, we need that for every prefix of some ordering, the sum of those steps ≤ e_a. But is it always possible to find an ordering such that every prefix sum ≤ e_a? Since e_a ≥ D, we can simply do all steps; any prefix sum will be ≤ D ≤ e_a, so any ordering works! Actually check: If we take any ordering, the sum of the first i steps is some subset sum of the multiset, but that sum is always ≤ D (since D is total). Since D ≤ e_a, any prefix sum is ≤ e_a. So condition sum_{prev} ≤ e_a is always true, but we need sum_{prev} + d_i ≤ e_a? Wait the condition is before dividing by d_i, we need current exponent = e_a - sum_{prev} ≥ d_i, i.e., e_a - sum_{prev} ≥ d_i => sum_{prev} + d_i ≤ e_a. Since sum_{prev} is the sum of steps already done, and we add d_i, the sum becomes sum_{prev}+d_i. But this sum might be greater than e_a? Since sum_{prev}+d_i could be up to D + something? Actually sum_{prev} is at most D - min_remaining, but it could be that sum_{prev} is close to D and then adding d_i would exceed D? But you wouldn't add d_i after having already done all steps; you only add d_i if that step hasn't been done yet. The total of all steps is D. So sum_{prev} + d_i ≤ D? Not necessarily: If you haven't done all steps, sum_{prev}+d_i could be ≤ D? Actually since total sum of all steps is D, and sum_{prev} is sum of some subset, and d_i is one of the remaining steps, the sum sum_{prev}+d_i is at most D (if you include d_i from the remaining set, the maximum sum is when you take sum_{prev} as the sum of all steps except maybe some, but adding d_i could give at most D if the remaining steps include d_i and possibly others, but if you add d_i, you are not adding the others, so sum_{prev}+d_i could be less than or equal to D? Actually consider total set S with sum D. For any subset T ⊂ S, and an element x ∈ S\T, the sum of T plus x is ≤ D? It could be less than D if S\T contains more than just x. But it cannot exceed D because the total sum of all elements is D, and T∪{x} is a subset of S, so its sum is ≤ D. Yes, because S is the whole set, and T∪{x} ⊆ S, so its sum is ≤ sum(S) = D. So indeed sum_{prev}+d_i ≤ D ≤ e_a. So condition holds for any ordering. So no need to sort; any arbitrary order works. So indeed, for any multiset of divisions summing to D, any ordering yields valid sequence because each step's required divisibility condition is automatically satisfied. Wait is there a scenario where the condition could fail? Let's test with e_a=2, D=2, steps: divide by 2 (d=1) then divide by 2 (d=1). After first division, exponent becomes 1, then second division requires dividing by 2; but current exponent is 1, which is >=1, so ok. So fine. e_a=2, steps: divide by 3? Not allowed. So fine.
Thus division path always works.
Therefore solution stands.
But one more nuance: The operations allow both multiplication and division, but sometimes using both might reduce steps for certain exponent differences? For example, to go from e_a=1 to e_b=2, diff=1, we can do one multiply. That's 1 step. No mix.
From e_a=0 to e_b=1: 1 step.
What about e_a=0 to e_b=2: 1 step (multiply by 4). That's ceil(2/3)=1.
From e_a=0 to e_b=3: 1 step (multiply by 8). Ceil=1.
From e_a=0 to e_b=4: ceil(4/3)=2. Could we do multiply by 8 (+3) then divide by 2 (-1) to get +2? That would be net +2 not 4. Or multiply by 8 (+3) then multiply by 2 (+1) gives +4 but that's 2 steps. Could we do multiply by 2 (+1) then multiply by 4 (+2) => 2 steps. So 2 steps minimum.
From e_a=1 to e_b=5: diff=4, ceil=2. Options: multiply by 4 (+2) then multiply by 4 (+2) =>2 steps. Or multiply by 8 (+3) then multiply by 2 (+1) =>2 steps. Good.
From e_a=2 to e_b=5: diff=3, ceil=1: multiply by 8? That adds 3, yes.
Now, consider e_a=1 to e_b=4: diff=3, ceil=1: multiply by 8? That adds 3, from exponent 1 to 4. Indeed: 2 * 8 = 16? Wait a=2 (since a=2odd?), example: a=2 (odd part 1, exponent 1), target 16? Actually exponent 4 corresponds to 16. 28=16, yes 1 step.
Thus no mixing needed.
Could mixing reduce steps for some diff? Suppose diff=5. ceil(5/3)=2. We have 2 steps: 3+2. Could there be a 1-step solution using both multiply and divide? For one step you can only do one operation, so cannot achieve net 5 in one step. So 2 is minimal.
For diff=7, ceil=3. Could we do 2 steps? Max net change in 2 steps is 6 (if both +3). So 3 steps needed.
Thus the direct approach is optimal.
Therefore answer is simple.
Now, we must be careful with large numbers: a,b up to 10^18. Compute odd parts by repeatedly dividing by 2. That's fine. But note: The odd part could be large (up to 10^18). But comparing equality is fine.
Implementation:
def solve():
import sys
t = int(sys.stdin.readline())
out = []
for _ in range(t):
a_str, b_str = sys.stdin.readline().split()
a = int(a_str)
b = int(b_str)
a_orig = a
b_orig = b
# compute v2 and odd part
e_a = 0
while a % 2 == 0:
a //= 2
e_a += 1
e_b = 0
while b % 2 == 0:
b //= 2
e_b += 1
if a != b:
out.append("-1")
else:
diff = abs(e_a - e_b)
steps = (diff + 2) // 3 # ceil division
out.append(str(steps))
print("\n".join(out))
That's it.
But wait: Are there any cases where a_odd and b_odd are equal but the transformation is impossible due to something like we need to divide by 8 but the current number is not divisible by 8, even though the exponent is enough? Actually if exponent >=3, the number is divisible by 8. So it's fine.
What about negative exponent? Not possible.
Thus solution is done.
But we should consider that maybe the register can hold any integer, so fine.
Double-check with some random tests to ensure no off-by-one.
diff=0 => (0+2)//3 = 0? (2//3=0) Actually (0+2)//3 = 2//3 = 0 in integer division. correct.
diff=1 => (1+2)//3 = 3//3=1. correct.
diff=2 => (2+2)//3=4//3=1. correct.
diff=3 => (3+2)//3=5//3=1? Wait 5//3=1 in integer division? Actually 5//3 = 1 (floor). But we need ceil(3/3)=1, so okay.
diff=4 => (4+2)//3=6//3=2, correct.
diff=5 => (5+2)//3=7//3=2, correct.
diff=6 => (6+2)//3=8//3=2? 8//3=2 floor, but ceil(6/3)=2, okay.
diff=7 => (7+2)//3=9//3=3, correct.
diff=8 => (8+2)//3=10//3=3, correct.
diff=9 => (9+2)//3=11//3=3, correct.
So formula works.
Thus final answer.
But is there any possibility that we need to consider using operations that change the odd part? No, because multiplication/division by powers of two doesn't change odd part.
Therefore answer.
Now, let's think about the problem statement: "shift its bits left or right by at most three positions. The right shift is forbidden if it cuts off some ones." That is exactly division by 2^k only if the number is divisible by 2^k. So our interpretation correct.
Now, check if there is any nuance: 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. So shift left by k means multiply by 2^k. Shift right by k means divide by 2^k, but only if the lower k bits are zero (i.e., divisible). So yes.
Thus solution stands.
But before finalizing, ensure we haven't missed a scenario where the odd part equality is necessary but not sufficient? What if a_odd == b_odd, but the exponent difference is such that we cannot achieve because we might need to go negative intermediate? As argued, we never go negative because we only subtract total D ≤ e_a. But if we need to increase exponent, we never go negative anyway. So sufficient.
Consider a=1, b=2^60. a_odd=1, b_odd=1, diff=60, ceil(60/3)=20. Works.
Consider a=2^60, b=1, diff=60, ceil=20. Should work: we can divide by 8 repeatedly? 2^60 divided by 8 repeatedly reduces exponent by 3 each time. After 20 divisions by 8, exponent becomes 0? 60 - 20*3 = 0. Yes. But need to check that at each step, number is divisible by 8. Since exponent is at least 3 until the last steps. But in the last steps, if we do division by 8 when exponent is less than 3, it wouldn't be divisible. However we can choose division amounts differently to avoid needing to divide by 8 when exponent <3. For D=60, we need 20 steps of 3, but after 19 steps of 3, exponent = 60 - 57 = 3, then last step of 3 is okay. So fine. But if D is not a multiple of 3, we might have a final step of 1 or 2. For example D=61, ceil(61/3)=21. That would be 20 steps of 3 and one step of 1. After 20 steps of 3, exponent = 60 - 60 = 0? Actually start 60? Wait if e_a=61? I'm mixing. Let's test: e_a=61, e_b=0, D=61. ceil=21. Could we do 20 steps of 3 (total 60) and then a step of 1? After 20 steps of 3, exponent = 61-60 = 1, then divide by 2 (step 1) works. So ok.
But what if e_a=2, e_b=0, D=2, we do one step of 2? That's allowed. So no issue.
Thus all fine.
Now, ensure that the odd part computed by dividing repeatedly by 2 yields the odd part correctly even for large numbers. Python's integer division fine.
Now, about performance: t up to 1000, each a,b up to 1e18, while loops for trailing zeros up to about 60 iterations, so total ~60k operations, trivial.
Thus solution is done.
But I'll also think about an alternative: using bit_length and trailing zeros via bit operations. But not necessary.
Now, one more nuance: The problem says "shift its bits left or right by at most three positions". That means you can shift by 1,2, or 3 bits. So yes.
Thus answer is simple.
But wait, the sample case 1001 1100611139403776 gave answer 14. Our formula gave 14. Good.
Now, just to be absolutely thorough, check the case where a and b have the same odd part but maybe a is divisible by a higher power of 2 than b, but the number of steps computed as ceil(diff/3) may not be achievable if we have to divide by a power of two but the number might not be divisible by that specific power due to odd part? But divisibility only depends on exponent of 2, not odd part. So if the odd part is same, the number is of the form odd * 2^e. Divisibility by 2^k requires e >= k. So if we are subtracting total D, we just need to ensure we never try to subtract more than current e. As we argued, it's fine.
Thus final solution.
I'll produce the final answer with explanation.
But I see that the problem might be from Codeforces (maybe problem "Shifts" or something). Usually these problems require BFS or DP but this one simplifies nicely.
Edge Cases: a or b could be 0? No, given 1 ≤ a,b.
Now, we need to print answer per test case.
Now, I think we can produce the final code.
But before final, let's test some more tricky cases:
Case: a = 2^3 * 3 = 24, b = 3*2^0 = 3. diff=3, steps=1. Divide by 8? 24/8=3, allowed. Yes.
Case: a = 2^3 * 3 = 24, b = 3*2^1 = 6. diff=2, steps=1. Divide by 4? 24/4=6, allowed.
Case: a = 2^0 * 3 = 3, b = 32^2 = 12. diff=2, steps=1. Multiply by 4? 34=12, allowed.
Case: a = 2^1 * 3 = 6, b = 32^4 = 48. diff=3, steps=1. Multiply by 8? 68=48, allowed.
Case: a = 2^1 * 3 = 6, b = 3*2^5 = 96. diff=4, steps=2. Multiply by 8 (to 48) then multiply by 2 (to 96) => 2 steps.
Case: a = 2^2 * 3 = 12, b = 32^5 = 96. diff=3, steps=1. Multiply by 8? 128=96, yes.
Now, case where odd parts equal but a is not divisible by 2 (e_a=0) and b is huge exponent, works.
Case: a = 1, b = 2^0 = 1, diff=0, steps=0.
Case: a = 2, b = 8? 2=12^1, 8=12^3, diff=2, steps=1? Multiply by 4: 2*4=8, yes 1 step.
All good.
Thus answer.
One more check: In the sample, last test case 10 8 -> -1 because odd parts differ. Indeed 10's odd part=5, 8's odd part=1. So correct.
Therefore final answer.
But we should also consider if there is any possibility of using left shift and right shift in a sequence that changes the odd part? No, because shifting bits left/right by up to 3 positions does not affect the higher bits beyond the shift? Actually if you shift right, you lose lower bits, but if those lower bits are ones, it's forbidden. So the number's binary representation's higher bits (the part that is not shifted out) remain, but shifting left adds zeros at the right. So effectively, it's multiplication/division by powers of two. So the odd part (the value after removing all factors of 2) remains unchanged because multiplication by 2^k adds factor 2^k, division removes factor 2^k. So odd part constant.
Thus solution complete.
Now, I'll prepare final message with explanation and code. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
out = []
idx = 1
for _ in range(t):
a = int(data[idx]); b = int(data[idx+1]); idx += 2
# remove factors of 2 and count exponents
a_odd = a
e_a = 0
while a_odd % 2 == 0:
a_odd //= 2
e_a += 1
b_odd = b
e_b = 0
while b_odd % 2 == 0:
b_odd //= 2
e_b += 1
if a_odd != b_odd:
out.append("-1")
else:
diff = abs(e_a - e_b)
# ceil(diff / 3)
steps = (diff + 2) // 3
out.append(str(steps))
sys.stdout.write("\n".join(out))
if name == "main":
solve()