Problem D
Factor-Free Tree
A factor-free tree is a rooted binary tree where
every node in the tree contains a positive integer value that
is coprime with all of the values of its ancestors. Two
positive integers are coprime if their greatest common divisor
equals
The inorder sequence of a rooted binary tree can be generated recursively by traversing first the left subtree, then the root, then the right subtree. See Figure 1 below for the inorder sequence of one factor-free tree.
![\includegraphics[width=0.45\textwidth ]{sample1}](/problems/factorfree/file/statement/en/img-0001.png)
Given a sequence
Input
The input consists of:
-
One line with one integer
( ), the length of the sequence. -
One line with
integers ( for each ), the elements of the sequence.
Output
If there exists a factor-free tree whose inorder sequence is
the given sequence, output
If no such tree exists, output “impossible” instead.
Sample Input 1 | Sample Output 1 |
---|---|
6 2 7 15 8 9 5 |
2 0 4 2 4 5 |
Sample Input 2 | Sample Output 2 |
---|---|
6 2 7 15 8 9 6 |
impossible |