結果
| 問題 |
No.14 最小公倍数ソート
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-06-11 14:52:03 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 596 ms / 5,000 ms |
| コード長 | 2,907 bytes |
| コンパイル時間 | 4,167 ms |
| コンパイル使用メモリ | 80,088 KB |
| 実行使用メモリ | 62,468 KB |
| 最終ジャッジ日時 | 2024-10-09 12:52:29 |
| 合計ジャッジ時間 | 14,002 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 20 |
ソースコード
import java.io.*;
import java.util.*;
public class Main_yukicoder14 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Printer pr = new Printer(System.out);
int n = sc.nextInt();
int[] cnt = new int[10000 + 1];
PriorityQueue<Integer> pq = new PriorityQueue<>();
List<Integer> ret = new ArrayList<>();
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
if (i > 0) {
cnt[a]++;
pq.add(a);
} else {
ret.add(a);
}
}
out:
while (!pq.isEmpty()) {
int e = pq.peek();
if (cnt[e] <= 0) {
pq.remove();
continue;
}
int prev = ret.get(ret.size() - 1);
SortedSet<Integer> ts = new TreeSet<>();
for (int i = 1; i * i <= prev; i++) {
if (prev % i == 0) {
ts.add(i);
ts.add(prev / i);
}
}
int lcm = lcm(prev, e);
for (int j = 1; prev * j <= lcm; j++) {
for (int d : ts) {
if (d * j > 10000) {
break;
}
if (cnt[d * j] > 0 && lcm(prev, d * j) == prev * j) {
ret.add(d * j);
cnt[d * j]--;
continue out;
}
}
}
}
// pr.println(ret);
// pr.flush();
for (int i = 0; i < n; i++) {
pr.print(ret.get(i));
if (i < n - 1) {
pr.print(" ");
} else {
pr.println();
}
}
pr.close();
sc.close();
}
private static int lcm(int n, int m) {
return n / gcd(n, m) * m;
}
private static int gcd(int n, int m) {
if (m == 0) {
return n;
} else {
return gcd(m, n % m);
}
}
@SuppressWarnings("unused")
private static class Scanner {
BufferedReader br;
Iterator<String> it;
Scanner (InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
}
String next() throws RuntimeException {
try {
if (it == null || !it.hasNext()) {
it = Arrays.asList(br.readLine().split(" ")).iterator();
}
return it.next();
} catch (IOException e) {
throw new IllegalStateException();
}
}
int nextInt() throws RuntimeException {
return Integer.parseInt(next());
}
long nextLong() throws RuntimeException {
return Long.parseLong(next());
}
float nextFloat() throws RuntimeException {
return Float.parseFloat(next());
}
double nextDouble() throws RuntimeException {
return Double.parseDouble(next());
}
void close() {
try {
br.close();
} catch (IOException e) {
// throw new IllegalStateException();
}
}
}
private static class Printer extends PrintWriter {
Printer(PrintStream out) {
super(out);
}
}
}