#P13995. 【MX-X19-T4】「FeOI Round 4.5」Supernova

【MX-X19-T4】「FeOI Round 4.5」Supernova

Description

Haruka gives you three non-negative integers x,y,px, y, p. You can perform the following two operations any number of times (including zero) in any order:

  1. Let x(x+1)px \gets (x + 1) \mathbin{\vert} p.
  2. Let xx(p+1)x \gets x \mathbin{\vert} (p + 1).

Here, \mathbin{\vert} denotes the bitwise OR operation, and aba \gets b means assigning the value of bb to aa.

Haruka wants to know whether it is possible to transform xx into yy through these operations. If possible, she also wants to know the minimum number of operations required.

Please help her solve this problem.

Input Format

The input contains multiple test cases.

The first line contains an integer TT, indicating the number of test cases. For each test case:

  • One line containing three non-negative integers x,y,px, y, p.

Output Format

For each test case, output one line. If there is a solution, output a non-negative integer representing the minimum number of operations; otherwise, output -1.

8
1 2 4
2 6 5
2 9 1
1 11 2
4 9 2
114514 1919810 2
105 163 33
10000000000 1000000000000000000 262144
-1
1
4
5
-1
902648
13
499999994999741440

Hint

【Sample Explanation】

For the first test case, it can be proven that no sequence of operations exists.

For the second test case, perform operation 2 once: xx changes as 262 \to 6.

For the third test case, perform operation 1 four times: xx changes as 235792 \to 3 \to 5 \to 7 \to 9.

【Data Range】

This problem uses bundled testing.

Subtask ID TT \le x,yx, y \le Special Properties Score
11 55 1010 None 99
22 10710^7 1717
33 10410^4 101810^{18} A 2222
44 B 55
55 C 1414
66 None 3333
  • Special Property A: If a solution exists, there is an optimal solution that uses only operation 1.
  • Special Property B: If a solution exists, there is an optimal solution that uses only operation 2.
  • Special Property C: p=1p = 1.

For all test cases, 1T1041 \le T \le 10^4, 0x,y,p10180 \le x, y, p \le 10^{18}.

Translated by DeepSeek V3.1