結果
問題 | No.2756 GCD Teleporter |
ユーザー | tenten |
提出日時 | 2024-05-13 15:59:44 |
言語 | Java21 (openjdk 21) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,891 bytes |
コンパイル時間 | 2,430 ms |
コンパイル使用メモリ | 80,372 KB |
実行使用メモリ | 144,452 KB |
最終ジャッジ日時 | 2024-05-13 16:00:23 |
合計ジャッジ時間 | 32,159 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 49 ms
50,464 KB |
testcase_01 | AC | 52 ms
50,172 KB |
testcase_02 | AC | 54 ms
50,436 KB |
testcase_03 | AC | 51 ms
50,440 KB |
testcase_04 | AC | 228 ms
60,416 KB |
testcase_05 | AC | 461 ms
73,912 KB |
testcase_06 | AC | 852 ms
109,220 KB |
testcase_07 | AC | 737 ms
104,336 KB |
testcase_08 | AC | 692 ms
104,316 KB |
testcase_09 | AC | 730 ms
86,544 KB |
testcase_10 | AC | 765 ms
109,168 KB |
testcase_11 | AC | 825 ms
112,792 KB |
testcase_12 | AC | 452 ms
67,596 KB |
testcase_13 | AC | 998 ms
122,092 KB |
testcase_14 | AC | 1,001 ms
117,736 KB |
testcase_15 | AC | 443 ms
63,172 KB |
testcase_16 | AC | 650 ms
85,776 KB |
testcase_17 | AC | 744 ms
91,772 KB |
testcase_18 | AC | 354 ms
67,504 KB |
testcase_19 | AC | 995 ms
122,844 KB |
testcase_20 | AC | 1,189 ms
119,444 KB |
testcase_21 | AC | 642 ms
84,272 KB |
testcase_22 | AC | 988 ms
118,768 KB |
testcase_23 | AC | 337 ms
64,876 KB |
testcase_24 | AC | 997 ms
106,680 KB |
testcase_25 | AC | 1,065 ms
109,792 KB |
testcase_26 | AC | 1,100 ms
117,856 KB |
testcase_27 | AC | 1,010 ms
119,276 KB |
testcase_28 | AC | 710 ms
84,908 KB |
testcase_29 | AC | 790 ms
105,652 KB |
testcase_30 | AC | 1,136 ms
119,492 KB |
testcase_31 | AC | 593 ms
84,280 KB |
testcase_32 | AC | 933 ms
108,612 KB |
testcase_33 | AC | 527 ms
72,460 KB |
testcase_34 | AC | 270 ms
64,900 KB |
testcase_35 | AC | 905 ms
113,868 KB |
testcase_36 | AC | 732 ms
105,656 KB |
testcase_37 | RE | - |
testcase_38 | AC | 766 ms
118,408 KB |
testcase_39 | AC | 861 ms
144,452 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)); } } 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(); } } }