Decryption (Codeforces 1419E)
Tags: 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: 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: 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.

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.

Sample Inputs:
3
6
4
30

Sample Outputs:
2 3 6 
1
2 4 
0
2 30 6 3 15 5 10 
0

Solution (Python):
n = 40000 + 1
primes = []
sieve = [1] * n
for i in range(2, n):
    if sieve[i]:
        primes.append(i)
        for j in range(i * i, n, i):
            sieve[j] = 0

def all_fct(l):
    ret = [1]
    for (p, q) in l:
        pp = 1
        l_ret = len(ret)
        for _ in range(q):
            pp *= p
            ret.extend((ret[i] * pp for i in range(l_ret)))
    return ret
for tt in range(int(input())):
    n = int(input())
    nn = n
    ff = []
    cnt = 0
    for p in primes:
        while nn % p == 0:
            cnt += 1
            nn //= p
        if cnt > 0:
            ff.append((p, cnt))
            cnt = 0
            if n == 1:
                break
    if nn != 1:
        ff.append((nn, 1))
    ans = []
    kk = len(ff)
    transition = {}
    if kk == 1:
        print(' '.join(map(str, [a for a in all_fct(ff) if a != 1])))
        print(0)
        continue
    elif kk == 2 and max(ff[0][1], ff[1][1]) == 1:
        print(' '.join(map(str, [ff[0][0], ff[1][0], ff[0][0] * ff[1][0]])))
        print(1)
        continue
    for i in range(kk):
        transition[ff[i][0] * ff[(i + 1) % kk][0]] = i
    if kk == 2:
        if ff[0][1] > 1:
            transition[ff[0][0] * ff[0][0] * ff[1][0]] = 0
        else:
            transition[ff[0][0] * ff[1][0] * ff[1][0]] = 0
    all_f = all_fct(ff)
    ans = [[] for i in range(kk)]
    ans_ = []
    for f in all_f:
        if f not in transition:
            for ind in range(kk):
                if f % ff[ind][0] == 0:
                    ans[ind].append(f)
                    break
    for tr in transition:
        ans[transition[tr]].append(tr)
    for aa in ans:
        ans_.extend(aa)
    print(' '.join(map(str, ans_)))
    print(0)

Solution (Python):
n = 40000 + 1
primes = []
sieve = [1] * n
for i in range(2, n):
    if sieve[i]:
        primes.append(i)
        for j in range(i * i, n, i):
            sieve[j] = 0

def all_fct(l):
    ret = [1]
    for (p, q) in l:
        pp = 1
        l_ret = len(ret)
        for _ in range(q):
            pp *= p
            ret.extend((ret[i] * pp for i in range(l_ret)))
    return ret
for tt in range(int(input())):
    n = int(input())
    nn = n
    ff = []
    cnt = 0
    for p in primes:
        while nn % p == 0:
            cnt += 1
            nn //= p
        if cnt > 0:
            ff.append((p, cnt))
            cnt = 0
            if n == 1:
                break
    if nn != 1:
        ff.append((nn, 1))
    ans = []
    kk = len(ff)
    if kk == 1:
        print(' '.join(map(str, [a for a in all_fct(ff) if a != 1])))
        print(0)
        continue
    elif kk == 2 and max(ff[0][1], ff[1][1]) == 1:
        print(' '.join(map(str, [ff[0][0], ff[1][0], ff[0][0] * ff[1][0]])))
        print(1)
        continue
    transition = {}
    for i in range(kk):
        transition[ff[i][0] * ff[(i + 1) % kk][0]] = i
    if kk == 2:
        if ff[0][1] > 1:
            transition[ff[0][0] * ff[0][0] * ff[1][0]] = 0
        else:
            transition[ff[0][0] * ff[1][0] * ff[1][0]] = 0
    all_f = all_fct(ff)
    ans = [[] for i in range(kk)]
    for f in all_f:
        if f not in transition:
            for ind in range(kk):
                if f % ff[ind][0] == 0:
                    ans[ind].append(f)
                    break
    for tr in transition:
        ans[transition[tr]].append(tr)
    ans_ = []
    for aa in ans:
        ans_.extend(aa)
    print(' '.join(map(str, ans_)))
    print(0)

Solution (Python):
n = 40000 + 1
primes = []
sieve = [1] * n
for i in range(2, n):
    if sieve[i]:
        primes.append(i)
        for j in range(i * i, n, i):
            sieve[j] = 0

def all_fct(l):
    ret = [1]
    for (p, q) in l:
        pp = 1
        l_ret = len(ret)
        for _ in range(q):
            pp *= p
            ret.extend((ret[i] * pp for i in range(l_ret)))
    return ret
for tt in range(int(input())):
    n = int(input())
    nn = n
    ff = []
    cnt = 0
    for p in primes:
        while nn % p == 0:
            cnt += 1
            nn //= p
        if cnt > 0:
            ff.append((p, cnt))
            cnt = 0
            if n == 1:
                break
    if nn != 1:
        ff.append((nn, 1))
    ans = []
    kk = len(ff)
    if kk == 1:
        print(' '.join(map(str, [a for a in all_fct(ff) if a != 1])))
        print(0)
        continue
    elif kk == 2 and max(ff[0][1], ff[1][1]) == 1:
        print(' '.join(map(str, [ff[0][0], ff[1][0], ff[0][0] * ff[1][0]])))
        print(1)
        continue
    transition = {}
    for i in range(kk):
        transition[ff[i][0] * ff[(i + 1) % kk][0]] = i
    if kk == 2:
        if ff[0][1] > 1:
            transition[ff[0][0] * ff[0][0] * ff[1][0]] = 0
        else:
            transition[ff[0][0] * ff[1][0] * ff[1][0]] = 0
    all_f = all_fct(ff)
    ans = [[] for i in range(kk)]
    for f in all_f:
        if f not in transition:
            for ind in range(kk):
                if f % ff[ind][0] == 0:
                    ans[ind].append(f)
                    break
    for tr in transition:
        ans[transition[tr]].append(tr)
    ans_ = []
    for aa in ans:
        ans_.extend(aa)
    print(' '.join(map(str, ans_)))
    print(0)