Codeforces 894 C Marco and GCD Sequence 思维

  题目链接: http://codeforces.com/problemset/problem/894/C

  题目描述: 一个序列所有区间的gcd构成一个M集合, 现在给你这个M集合, 让你构造出这个序列, 若无解输出-1

  解题思路: 最大区间的gcd一定是最小的那个, 所以无解的情况就是最小的那个a[1]不是每一个的因子, 构造的时候输出原来的序列肯定是不行的, 因为会有相邻的两个 数 的因子是a[1]的倍数的情况发生, 那么我构造出来的序列就将a[1]插入到两个相邻之间就可以了

  代码: 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <iterator>
using namespace std;

const int maxn = 1005;
int a[maxn];

int main() {
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for(int i = 1; i <= n; i++) {
        if(a[i] % a[1] != 0) {
            cout << "-1" << endl;
            return 0;
        }
    }
    cout << 2 * n << endl;
    for(int i = 1; i <= n; i++) {
        cout << a[i] << " " << a[1] << " ";
    }
    cout << endl;
    return 0;
}
View Code

  思考: 这个就是思维题, 继续加油

原文地址:https://www.cnblogs.com/FriskyPuppy/p/7902887.html