Decryption
Contest: Codeforces Round 671 (Div. 2)
Difficulty rating: 2100
Tags: constructive algorithms, implementation, math, number theory

An agent called Cypher is decrypting a message, that contains a composite number $$$n$$$. All divisors of $$$n$$$, which are greater than $$$1$$$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.

In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.

A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.

Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.

Input format:
The first line contains an integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of test cases. Next $$$t$$$ lines describe each test case.

In a single line of each test case description, there is a single composite number $$$n$$$ $$$(4 \le n \le 10^9)$$$ — the number from the message.

It's guaranteed that the total number of divisors of $$$n$$$ for all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case in the first line output the initial order of divisors, which are greater than $$$1$$$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message.

If there are different possible orders with a correct answer, print any of them.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first test case $$$6$$$ has three divisors, which are greater than $$$1$$$: $$$2, 3, 6$$$. Regardless of the initial order, numbers $$$2$$$ and $$$3$$$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $$$2, 6, 3, 6$$$, and every two adjacent numbers are not coprime.

In the second test case $$$4$$$ has two divisors greater than $$$1$$$: $$$2, 4$$$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.

In the third test case all divisors of $$$30$$$ greater than $$$1$$$ can be placed in some order so that there are no two adjacent numbers that are coprime.

Example input:
3
6
4
30

Example output:
2 3 6 
1
2 4 
0
2 30 6 3 15 5 10 
0

Editorial:
Let's factorize $$$n$$$: $$$n = p_1^{q_1} \cdot p_2^{q_2} \cdot \dots \cdot p_k^{q_k}$$$
• If $$$k = 2$$$ and $$$q_1 = q_2 = 1$$$ (i.e. $$$n$$$ is the product of two different prime numbers)Divisors $$$p_1$$$ and $$$p_2$$$ will definately be adjacent and they are coprime so we should make one operation to insert their lcm between them. After that the circle will be $$$p_1$$$, $$$p_1 \cdot p_2 = n$$$, $$$p_2$$$, $$$p_1 \cdot p_2 = n$$$ and there will be no such two adjacent numbers that are coprime. The answer is 1.
• If $$$k = 2$$$ and $$$q_1 > 1$$$ or $$$q_2 > 1$$$, then we can firstly place numbers $$$p_1, p_1 \cdot p_2, p_2, n$$$. After that we can insert all unused divisors that are multiples of $$$p_1$$$ between $$$p_1$$$ and $$$n$$$, all divisors that are multiples of $$$p_2$$$ between $$$p_2$$$ and $$$n$$$. It is easy to see that in this case the answer is 0.
• In another case it is possible to arrange the divisors so that there are no such two adjacent numbers that are coprime. Firstly, we need to arrange in a circle these numbers: $$$p_1$$$, $$$p_2$$$, $$$p_3$$$, ..., $$$p_k$$$ After that we need to write down the products of these numbers between them: $$$p_1$$$, $$$p_1 \cdot p_2$$$, $$$p_2$$$, $$$p_2 \cdot p_3$$$, ..., $$$p_k$$$, $$$p_k \cdot p_1$$$ From now on we can just place unused numbers that way: insert all unused divisors, that are multiples of $$$p_1$$$, after $$$p_1$$$, insert all unused divisors, that are multiples of $$$p_2$$$, after $$$p_2$$$ and so on. If the solution is still unclear you may take a look at the image below. The answer in this case is 0.