DMOPC '22 Contest 2 P3 - Good Permutations

View as PDF

Submit solution


Points: 12 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

Mr. Gregory recently discovered the existence of gcd. Enthralled by its beauty, he challenges you to a puzzle. Mr. Gregory gives you a target permutation T of the integers 1,2,,N. He tells you that a permutation P is good if it can be turned into T using the following operation any number of times: choose an integer i (1iN1), such that gcd(Pi,Pi+1)=1, and swap elements Pi and Pi+1. The answer to the puzzle is the lexicographically maximal good permutation P.

Prove your worth by solving the puzzle!

Constraints

1N5×105

1TiN

T is a permutation of the integers 1,2,,N.

Subtask 1 [40%]

1N3×103

Subtask 2 [60%]

No additional constraints.

Input Specification

The first line contains the integer N.

The next line contains N space-separated integers, representing the target permutation T.

Output Specification

Output N space-separated integers P1,P2,,PN, the lexicographically maximal good permutation P.

Sample Input

Copy
4
2 1 3 4

Sample Output

Copy
3 2 4 1

Comments