894C - Marco and GCD Sequence - CodeForces Solution


constructive algorithms math *1900

Please click on ads to support us..

Python Code:

import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

m = int(input())
s = list(map(int, input().split()))
s.sort()
s0 = s[0]
ok = 1
for i in s:
    if i % s0:
        ok = 0
if not ok:
    ans = -1
    print(ans)
    exit()
ans = []
for i in s:
    ans.append(i)
    ans.append(s0)
m = len(ans)
print(m)
sys.stdout.write(" ".join(map(str, ans)))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cout.tie(NULL);
#define int long long
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
const int INF = 0x3f3f3f3f;

void debug(set<int> s) {
  for (int i : s) {
    cout << i << " ";
  }
  cout << endl;
}

void debug(unordered_set<int> s) {
  for (int i : s) {
    cout << i << " ";
  }
  cout << endl;
}

void debug(vector<string> s) {
  for (string i : s) {
    cout << i << " ";
  }
  cout << endl;
}

void debug(vector<pii> v) { 
  cout << "PAIR START" << endl;
  for (auto i : v) {
    cout << i.first << " " << i.second << endl;
  }
  cout << "PAIR END" << endl;
}

void debug(vector<int> v) { 
  for (int i : v) {
    cout << i << " ";
  }
  cout << endl;
}

void debug(map<int, int> v) { 
  cout << "PAIR START" << endl;
  for (auto i : v) {
    cout << i.first << " " << i.second << endl;
  }
  cout << "PAIR END" << endl;
}

void debug(vector<vector<int>> adj) {
  for (vector<int> i : adj) {
    for (int j : i) {
      cout << j << " ";
    }
    cout << endl;
  }
}
 
void iohelp(string s) {
	ios_base::sync_with_stdio(0); cin.tie(0); 
	freopen((s+".in").c_str(),"r",stdin);
	freopen((s+".out").c_str(),"w",stdout);
}

ll max(ll a, ll b) {
  return a > b ? a : b;
}

ll min(ll a, ll b) {
  return a < b ? a : b;
}

ll gcd(ll a, ll b) {
  ll temp = min(a, b);
  a = max(a, b);
  b = temp;
  return b == 0 ? a : gcd(b, a % b);
}
 
int32_t main() {
  fastio;
  int n;
  cin >> n;
  vi v(n);
  set<int> s;
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
    s.insert(v[i]);
  }
  int g = v[0];

  for (int i = 0; i < n; ++i) {
    if (v[i] % g != 0) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << 2 * n - 1 << endl;
  for (int i = 0; i < n; ++i) {
    cout << v[i] << " ";
    if (i != n - 1) {
      cout << g << " ";
    }
  }
  cout << endl;
}


Comments

Submit
0 Comments
More Questions

1169B - Pairs
1567D - Expression Evaluation Error
78A - Haiku
1287A - Angry Students
1428A - Box is Pull
234B - Reading
581B - Luxurious Houses
1481C - Fence Painting
935A - Fafa and his Company
22A - Second Order Statistics
1720B - Interesting Sum
1720A - Burenka Plays with Fractions
3A - Shortest path of the king
1720C - Corners
574A - Bear and Elections
352B - Jeff and Periods
1244A - Pens and Pencils
1670A - Prof Slim
1189A - Keanu Reeves
678A - Johny Likes Numbers
1699C - The Third Problem
1697D - Guess The String
754B - Ilya and tic-tac-toe game
760A - Petr and a calendar
1573A - Countdown
166A - Rank List
1631B - Fun with Even Subarrays
727A - Transformation from A to B
822B - Crossword solving
1623A - Robot Cleaner