luogu#P7854. 「EZEC-9」GCD Tree
「EZEC-9」GCD Tree
Background
Let denote the greatest common divisor of and , and let denote the lowest common ancestor of node and node .
Problem Description
You are given nodes, numbered , with weights .
Please use these nodes to construct a tree such that for all , .
If there is no solution, report it. Otherwise, output the shape of the tree.
Input Format
The first line contains an integer .
The second line contains integers .
Output Format
If there is no solution, output -1.
Otherwise, output one line with integers. The -th number denotes the parent index of node . In particular, if node is the root, output 0.
If there are multiple solutions, output any one of them.
5
1 2 3 4 5
0 1 1 2 1
5
1 2 3 4 6
-1
Hint
【Constraints and Notes】
This problem uses bundled testdata.
- Subtask 1 (5 points): .
- Subtask 2 (5 points): all are equal.
- Subtask 3 (5 points): .
- Subtask 4 (10 points): a solution is guaranteed to exist.
- Subtask 5 (15 points): .
- Subtask 6 (15 points): .
- Subtask 7 (15 points): .
- Subtask 8 (30 points): no special restrictions.
For of the testdata, and .
Translated by ChatGPT 5