luogu#P7854. 「EZEC-9」GCD Tree

    ID: 7131 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>Special JudgeO2优化素数判断,质数,筛法构造

「EZEC-9」GCD Tree

Background

Let gcd(x,y)\gcd(x,y) denote the greatest common divisor of xx and yy, and let lca(x,y)\operatorname{lca}(x,y) denote the lowest common ancestor of node xx and node yy.

Problem Description

You are given nn nodes, numbered 1,2,,n1,2,\ldots,n, with weights a1,a2,,ana_1,a_2,\ldots,a_n.

Please use these nn nodes to construct a tree such that for all 1i<jn1 \le i < j \le n, gcd(ai,aj)=alca(i,j)\gcd(a_i, a_j) = a_{\operatorname{lca}(i, j)}.

If there is no solution, report it. Otherwise, output the shape of the tree.

Input Format

The first line contains an integer nn.

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots , a_n.

Output Format

If there is no solution, output -1.

Otherwise, output one line with nn integers. The ii-th number denotes the parent index of node ii. In particular, if node ii 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): n=2n = 2.
  • Subtask 2 (5 points): all aia_i are equal.
  • Subtask 3 (5 points): n5n \le 5.
  • Subtask 4 (10 points): a solution is guaranteed to exist.
  • Subtask 5 (15 points): n100n \le 100.
  • Subtask 6 (15 points): n103n \le 10^3.
  • Subtask 7 (15 points): n3×103n \le 3 \times 10^3.
  • Subtask 8 (30 points): no special restrictions.

For 100%100\% of the testdata, 2n1052 \le n \le 10^5 and 1ai1061 \le a_i \le 10^6.

Translated by ChatGPT 5