CF894C.Marco and GCD Sequence

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

In a dream Marco met an elderly man with a pair of black glasses. The man told him the key to immortality and then disappeared with the wind of time.

When he woke up, he only remembered that the key was a sequence of positive integers of some length n, but forgot the exact sequence. Let the elements of the sequence be _a_1, _a_2, ..., a__n. He remembered that he calculated gcd(a__i, a__i + 1, ..., a__j) for every 1 ≤ i ≤ j ≤ n and put it into a set S. gcd here means the greatest common divisor.

Note that even if a number is put into the set S twice or more, it only appears once in the set.

Now Marco gives you the set S and asks you to help him figure out the initial sequence. If there are many solutions, print any of them. It is also possible that there are no sequences that produce the set S, in this case print -1.

在梦中,马可遇见了一位戴着黑色眼镜的老人。老人告诉了他永生的关键,随后便随时间之风消散而去。

当他醒来时,只记得这个关键是一段长度为 nn 的正整数序列,却忘记了具体的序列内容。设该序列的元素为 a1,a2,…,ana_1, a_2, \dots, a_n。他记得自己计算了所有满足 1≤i≤j≤n1 \le i \le j \le n 的区间 [i,j][i, j] 的 gcd⁡(ai,ai+1,…,aj)\gcd(a_i, a_{i+1}, \dots, a_j),并将所有结果放入集合 SS 中。此处 gcd⁡\gcd 指 最大公约数。

注意:即使某个数值被多次加入集合 SS,它在集合中也仅出现一次。

现在马可将集合 SS 告诉了你,请你帮他还原出原始序列。若存在多个解,输出任意一个即可;也有可能不存在能生成集合 SS 的序列,此时请输出 −1-1。

输入格式

The first line contains a single integer m (1 ≤ m ≤ 1000) — the size of the set S.

The second line contains m integers _s_1, _s_2, ..., s__m (1 ≤ s__i ≤ 106) — the elements of the set S. It's guaranteed that the elements of the set are given in strictly increasing order, that means _s_1 < _s_2 < ... < s__m.

第一行包含一个整数 mm(1≤m≤10001 \le m \le 1000)—— 集合 SS 的大小。

第二行包含 mm 个整数 s1,s2,…,sms_1, s_2, \ldots, s_m(1≤si≤1061 \le s_i \le 10^6)—— 集合 SS 的元素。保证集合中的元素以严格递增顺序给出,即 s1<s2<…<sms_1 < s_2 < \ldots < s_m。

输出格式

If there is no solution, print a single line containing -1.

Otherwise, in the first line print a single integer n denoting the length of the sequence, n should not exceed 4000.

In the second line print n integers _a_1, _a_2, ..., a__n (1 ≤ a__i ≤ 106) — the sequence.

We can show that if a solution exists, then there is a solution with n not exceeding 4000 and a__i not exceeding 106.

If there are multiple solutions, print any of them.

如果无解,输出一行 -1。

否则,第一行输出一个整数 nn,表示序列的长度,且 nn 不得超过 4000。

第二行输出 nn 个整数 a1, a2, ..., ana_1,\,a_2,\,...,\,a_n(满足 1≤ai≤1061\le a_i\le 10^6)——即所求序列。

可以证明:若解存在,则必存在一个满足 n≤4000n\le 4000 且 ai≤106a_i\le 10^6 的解。

若存在多个解,输出任意一个即可。

输入输出样例

  • 输入#1

    4
    2 4 6 12

    输出#1

    3
    4 6 12
  • 输入#2

    2
    2 3

    输出#2

    -1

说明/提示

In the first example 2 = gcd(4, 6), the other elements from the set appear in the sequence, and we can show that there are no values different from 2, 4, 6 and 12 among gcd(a__i, a__i + 1, ..., a__j) for every 1 ≤ i ≤ j ≤ n.

在第一个例子中,2 = \gcd(4, 6),集合中的其余元素均出现在该序列中,且可以证明:对于所有满足 1 ≤ i ≤ j ≤ n 的区间 [i, j],\gcd(a_i, a_{i+1}, ..., a_j) 的值均不会超出 {2, 4, 6, 12}。

输入解题思路,AI测评打分。不知道怎么写?

首页