Number Transformation

View as PDF

Submit solution


Points: 0.20 (partial)
Time limit: 1.0s
Memory limit: 256M
Input: stdin
Output: stdout

Author:
Problem type
Allowed languages
C, C++, Go, Java, Kotlin, Pascal, PyPy, Python, Rust, Scratch

Given two positive integers ~n~ and ~m~. You are allowed to perform the following operation (zero or more times):

  • Choose any number ~x~ ~(1 \le x \le m)~, then assign ~n \leftarrow n \times \gcd(n,x)~. Here, ~\gcd(n, x)~ is the greatest common divisor of ~n~ and ~x~.

The people on the planet Arrakis have always passed down a prophecy that whoever finds a way to transform the number ~n~ into ~m~ using the fewest operations will become the savior of the kingdom, The Messiah. The one who takes on this challenge must provide a sequence of operations to transform the number ~n~ or indicate that there is no way to transform ~n~ into ~m~ using the given operation.

Arrakis is in danger, a holy war is about to break out, so try to solve the challenge and see if you are the chosen one!

Input

Each test consists of multiple test cases. The first line contains the number of test cases ~t~ (~1 \le t \le 100~).

Each test case consists of a single line containing two positive integers ~n, m~ (~1 \leq n, m \leq 10^9~).

Output

For each test case, if there is no way to transform the number ~n~ into ~m~ using the given operation, print ~-1~. Otherwise, print on one line the integer ~k~ (~1 \le k \le 200~) — the minimum number of steps to transform ~n~ into ~m~, followed by ~k~ integers ~x_1, x_2, \ldots, x_k~ — the integers chosen for each step in order.

If there are multiple sequences of operations with the minimum number of steps to transform ~n~ into ~m~, print any of the sequences.

It can be shown that if it is possible to transform from ~n~ to ~m~, there will exist a sequence of transformations with no more than ~200~ operations.

Scoring

The total score for this problem is ~1250~.

Sample Input 1

5
2 2
1 6
6 216
2 12
4 2

Sample Output 1

0
-1
2 6 6
-1
-1

Notes

In the first test case, ~n = m = 2~, so no further operations are performed.

In the second test case, ~n = 1~. We can only choose ~x = 1~, and then assign ~n \gets \gcd(1, 1) = 1~. The operation cannot change ~n~, so there is no valid sequence of operations.

In the third test case, ~n = 6~, ~m = 216~. Coincidentally, ~216 = 6^3~, we can choose 2 operations with ~x~ both equal to ~6~. It can be shown that there is no sequence of operations to transform ~n = 6~ into ~m = 216~ with just 1 operation.

In the fifth test case, ~n = 4~, ~m = 2~. Since ~n > m~, and the operations cannot decrease the number ~n~, there is no sequence of operations that satisfies the condition.


Comments

Please read the guidelines before commenting.


There are no comments at the moment.