結果
問題 | No.2756 GCD Teleporter |
ユーザー | tenten |
提出日時 | 2024-05-13 16:02:02 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,251 ms / 2,000 ms |
コード長 | 3,001 bytes |
コンパイル時間 | 2,487 ms |
コンパイル使用メモリ | 80,940 KB |
実行使用メモリ | 144,468 KB |
最終ジャッジ日時 | 2024-05-13 16:02:37 |
合計ジャッジ時間 | 33,807 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
50,344 KB |
testcase_01 | AC | 52 ms
50,060 KB |
testcase_02 | AC | 52 ms
49,984 KB |
testcase_03 | AC | 52 ms
50,060 KB |
testcase_04 | AC | 241 ms
60,712 KB |
testcase_05 | AC | 490 ms
74,008 KB |
testcase_06 | AC | 968 ms
109,236 KB |
testcase_07 | AC | 852 ms
104,772 KB |
testcase_08 | AC | 740 ms
94,192 KB |
testcase_09 | AC | 755 ms
86,544 KB |
testcase_10 | AC | 850 ms
109,200 KB |
testcase_11 | AC | 867 ms
112,768 KB |
testcase_12 | AC | 399 ms
67,648 KB |
testcase_13 | AC | 1,057 ms
122,996 KB |
testcase_14 | AC | 1,044 ms
118,684 KB |
testcase_15 | AC | 400 ms
67,628 KB |
testcase_16 | AC | 728 ms
85,560 KB |
testcase_17 | AC | 827 ms
102,980 KB |
testcase_18 | AC | 385 ms
67,460 KB |
testcase_19 | AC | 1,030 ms
123,516 KB |
testcase_20 | AC | 1,251 ms
119,020 KB |
testcase_21 | AC | 817 ms
84,320 KB |
testcase_22 | AC | 1,161 ms
119,040 KB |
testcase_23 | AC | 328 ms
65,440 KB |
testcase_24 | AC | 1,035 ms
106,856 KB |
testcase_25 | AC | 1,077 ms
110,460 KB |
testcase_26 | AC | 1,028 ms
118,036 KB |
testcase_27 | AC | 1,023 ms
119,280 KB |
testcase_28 | AC | 701 ms
84,644 KB |
testcase_29 | AC | 816 ms
105,780 KB |
testcase_30 | AC | 1,080 ms
120,404 KB |
testcase_31 | AC | 648 ms
84,404 KB |
testcase_32 | AC | 952 ms
113,128 KB |
testcase_33 | AC | 530 ms
72,408 KB |
testcase_34 | AC | 264 ms
61,020 KB |
testcase_35 | AC | 929 ms
114,160 KB |
testcase_36 | AC | 765 ms
105,520 KB |
testcase_37 | AC | 54 ms
50,484 KB |
testcase_38 | AC | 818 ms
118,560 KB |
testcase_39 | AC | 921 ms
144,468 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); TreeMap<Integer, Integer> added = new TreeMap<>(); List<Set<Integer>> values = new ArrayList<>(); for (int i = 0; i < n; i++) { int x = sc.nextInt(); Set<Integer> current = new HashSet<>(); for (int j = 2; j <= Math.sqrt(x); j++) { while (x % j == 0) { x /= j; current.add(j); added.put(j, null); } } if (x > 1) { current.add(x); added.put(x, null); } values.add(current); } int idx = n; for (int x : added.keySet()) { added.put(x, idx++); } UnionFindTree uft = new UnionFindTree(idx); for (int i = 0; i < n; i++) { for (int x : values.get(i)) { uft.unite(i, added.get(x)); } } if (added.size() == 0) { System.out.println(uft.size * 2); } else { System.out.println(Math.min(uft.size * 2, (uft.size - 1L) * added.firstKey())); } } static class UnionFindTree { int[] parents; int size; public UnionFindTree(int size) { this.size = size; parents = new int[size]; for (int i = 0; i < size; i++) { parents[i] = i; } } public int find(int x) { if (x == parents[x]) { return x; } else { return parents[x] = find(parents[x]); } } public boolean same(int x, int y) { return find(x) == find(y); } public void unite(int x, int y) { if (!same(x, y)) { size--; parents[find(x)] = find(y); } } } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }