Created
November 19, 2017 15:31
-
-
Save chermehdi/5637d66e52e1975fc80fef37e5385f2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.OutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.PrintWriter; | |
import java.util.Arrays; | |
import java.util.StringTokenizer; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.io.InputStream; | |
/** | |
* Built using CHelper plug-in | |
* Actual solution is at the top | |
* | |
* @author MaxHeap | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
InputStream inputStream = System.in; | |
OutputStream outputStream = System.out; | |
FastReader in = new FastReader(inputStream); | |
PrintWriter out = new PrintWriter(outputStream); | |
TaskC solver = new TaskC(); | |
solver.solve(1, in, out); | |
out.close(); | |
} | |
static class TaskC { | |
public void solve(int testNumber, FastReader in, PrintWriter out) { | |
int n = in.nextInt(); | |
Integer[] arr = new Integer[n]; | |
for (int i = 0; i < n; i++) arr[i] = in.nextInt(); | |
for (int i = 0; i < arr.length; i++) { | |
int g = arr[i]; | |
for (int j = i + 1; j < arr.length; j++) { | |
g = IntMath.gcd(g, arr[j]); | |
} | |
if (Arrays.binarySearch(arr, g) < 0) { | |
out.println(-1); | |
return; | |
} | |
} | |
out.println(n); | |
for (int i = 0; i < n; i++) { | |
if (i > 0) out.print(" "); | |
out.print(arr[i]); | |
} | |
} | |
} | |
static class FastReader { | |
BufferedReader reader; | |
StringTokenizer st; | |
public FastReader(InputStream stream) { | |
reader = new BufferedReader(new InputStreamReader(stream)); | |
st = null; | |
} | |
public String next() { | |
while (st == null || !st.hasMoreTokens()) { | |
try { | |
String line = reader.readLine(); | |
if (line == null) { | |
return null; | |
} | |
st = new StringTokenizer(line); | |
} catch (Exception e) { | |
throw new RuntimeException(); | |
} | |
} | |
return st.nextToken(); | |
} | |
public int nextInt() { | |
return Integer.parseInt(next()); | |
} | |
} | |
static class IntMath { | |
public static int gcd(int a, int b) { | |
if (b != 0) | |
return gcd(b, a % b); | |
return a; | |
} | |
} | |
} | |
close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment