#P8093. [USACO22JAN] Searching for Soulmates S

[USACO22JAN] Searching for Soulmates S

Problem Description

Each of Farmer John’s cows wants to find her soulmate—another cow with similar traits that is as compatible with her as possible. Each cow’s personality is described by an integer pip_i (1pi10181 \leq p_i \leq 10^{18}). Two cows with the same personality are soulmates. A cow can change her personality using “change operations”: multiply it by 22, divide it by 22 (when pip_i is even), or add 11.

Farmer John initially paired up his cows in an arbitrary way. He is curious how many change operations are needed to make each pair become soulmates. For each pair of cows, find the minimum number of change operations that the first cow in the pair must perform so that she can become soulmates with the second cow.

Input Format

The first line contains NN (1N101 \le N \le 10), the number of cow pairs. The next NN lines each describe one pair of cows and contain two integers, their personality values. The first integer is the personality of the cow that needs to be changed to match the other cow.

Output Format

Output NN lines. For each pair, output the minimum number of operations the first cow needs to perform so that her personality matches the second cow’s.

6
31 13
12 8
25 6
10 24
1 1
997 120
8
3
8
3
0
20

Hint

[Sample Explanation]

For the first test case, one optimal sequence of operations is $31 \implies 32 \implies 16 \implies 8 \implies 9 \implies 10 \implies 11 \implies 12 \implies 13$.

For the second test case, one optimal sequence of operations is 12    6    7    812 \implies 6 \implies 7 \implies 8.

[Constraints]

  • Test points 1-4 satisfy pi105p_i \le 10^5.
  • Test points 5-12 have no additional constraints.

Translated by ChatGPT 5