CF894C 题解

· · 题解

思路

我们设 g=\gcd(a_1,a_2,a_3,\dots,a_n),然后我们只要构造一个类似 g,a_1,g,a_2,g,a_3,\dots,g,a_n 就行了,长度 2\times n(反正限制只有 4000,够用了)。另外如果 g 不在 a 里就是无解。

代码

# include <bits/stdc++.h>
using namespace std;
int n, a[1005], g;
int main () {
    ios::sync_with_stdio (0);
    cin.tie (0);
    cout.tie (0);
    cin >> n;
    for (int i = 0; i < n; ++ i)
        cin >> a[i], g = __gcd (g, a[i]);
    for (int i = 0; i < n; ++ i)
        if (a[i] == g)
            goto there;
    cout << "-1";
    return 0;
there:
    ;
    cout << n * 2 << '\n';
    for (int i = 0; i < n; ++ i)
        cout << g << ' ' << a[i] << ' ';
    return 0;
}