結果

問題 No.2756 GCD Teleporter
ユーザー ks2mks2m
提出日時 2024-05-10 23:23:45
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,632 bytes
コンパイル時間 2,215 ms
コンパイル使用メモリ 79,172 KB
実行使用メモリ 107,580 KB
最終ジャッジ日時 2024-05-10 23:24:09
合計ジャッジ時間 23,364 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
56,760 KB
testcase_01 AC 100 ms
56,744 KB
testcase_02 AC 109 ms
56,640 KB
testcase_03 AC 101 ms
56,696 KB
testcase_04 AC 207 ms
63,672 KB
testcase_05 AC 404 ms
68,940 KB
testcase_06 AC 636 ms
78,704 KB
testcase_07 AC 584 ms
78,060 KB
testcase_08 AC 556 ms
77,300 KB
testcase_09 AC 535 ms
74,340 KB
testcase_10 AC 559 ms
79,932 KB
testcase_11 AC 710 ms
82,252 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 674 ms
77,652 KB
testcase_21 AC 416 ms
72,024 KB
testcase_22 AC 719 ms
81,084 KB
testcase_23 AC 224 ms
63,352 KB
testcase_24 AC 535 ms
79,820 KB
testcase_25 AC 616 ms
80,520 KB
testcase_26 AC 633 ms
86,364 KB
testcase_27 AC 696 ms
84,968 KB
testcase_28 AC 445 ms
71,392 KB
testcase_29 AC 578 ms
75,320 KB
testcase_30 AC 660 ms
82,684 KB
testcase_31 AC 446 ms
68,972 KB
testcase_32 AC 563 ms
76,084 KB
testcase_33 AC 346 ms
65,348 KB
testcase_34 AC 208 ms
65,724 KB
testcase_35 AC 563 ms
88,648 KB
testcase_36 AC 554 ms
85,800 KB
testcase_37 WA -
testcase_38 AC 636 ms
89,464 KB
testcase_39 AC 762 ms
107,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] sa = br.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]);
		}
		br.close();

		int m = 200000;
		Eratosthenes era = new Eratosthenes(m);
		List<List<Integer>> list = new ArrayList<>(m + 1);
		for (int i = 0; i <= m; i++) {
			list.add(new ArrayList<>());
		}
		for (int i = 0; i < n; i++) {
			Map<Integer, Integer> map = era.bunkai(a[i]);
			for (int k : map.keySet()) {
				list.get(k).add(i);
			}
		}

		long min = 0;
		UnionFind uf = new UnionFind(n);
		for (int i = 2; i <= m; i++) {
			List<Integer> wk = list.get(i);
			if (min == 0 && !wk.isEmpty()) {
				min = i;
			}
			for (int j = 1; j < wk.size(); j++) {
				uf.union(wk.get(0), wk.get(j));
			}
		}

		System.out.println(min * (uf.num - 1));
	}

	static class UnionFind {
		int[] parent, size;
		int num = 0; // 連結成分の数

		UnionFind(int n) {
			parent = new int[n];
			size = new int[n];
			num = n;
			for (int i = 0; i < n; i++) {
				parent[i] = i;
				size[i] = 1;
			}
		}

		void union(int x, int y) {
			int px = find(x);
			int py = find(y);
			if (px != py) {
				parent[px] = py;
				size[py] += size[px];
				num--;
			}
		}

		int find(int x) {
			if (parent[x] == x) {
				return x;
			}
			parent[x] = find(parent[x]);
			return parent[x];
		}

		/**
		 * xとyが同一連結成分か
		 */
		boolean same(int x, int y) {
			return find(x) == find(y);
		}

		/**
		 * xを含む連結成分のサイズ
		 */
		int size(int x) {
			return size[find(x)];
		}
	}

	static class Eratosthenes {
		int[] div;

		public Eratosthenes(int n) {
			div = new int[n + 1];
			if (n < 2) return;
			div[0] = -1;
			div[1] = -1;
			int end = (int) Math.sqrt(n) + 1;
			for (int i = 2; i <= end; i++) {
				if (div[i] == 0) {
					div[i] = i;
					for (int j = i * i; j <= n; j+=i) {
						if (div[j] == 0) div[j] = i;
					}
				}
			}
			for (int i = end + 1; i <= n; i++) {
				if (div[i] == 0) div[i] = i;
			}
		}

		public Map<Integer, Integer> bunkai(int x) {
			Map<Integer, Integer> soinsu = new HashMap<>();
			while (x > 1) {
				Integer d = div[x];
				soinsu.put(d, soinsu.getOrDefault(d, 0) + 1);
				x /= d;
			}
			return soinsu;
		}

		public boolean isSosuu(int x) {
			return div[x] == x;
		}
	}
}
0