結果

問題 No.2756 GCD Teleporter
ユーザー ks2mks2m
提出日時 2024-05-10 23:44:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 696 ms / 2,000 ms
コード長 2,758 bytes
コンパイル時間 2,436 ms
コンパイル使用メモリ 78,696 KB
実行使用メモリ 105,860 KB
最終ジャッジ日時 2024-05-10 23:45:24
合計ジャッジ時間 21,478 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
56,888 KB
testcase_01 AC 103 ms
56,692 KB
testcase_02 AC 104 ms
56,504 KB
testcase_03 AC 99 ms
56,904 KB
testcase_04 AC 192 ms
63,588 KB
testcase_05 AC 352 ms
71,760 KB
testcase_06 AC 621 ms
80,380 KB
testcase_07 AC 517 ms
76,592 KB
testcase_08 AC 499 ms
77,220 KB
testcase_09 AC 490 ms
74,812 KB
testcase_10 AC 526 ms
77,472 KB
testcase_11 AC 694 ms
82,188 KB
testcase_12 AC 273 ms
66,364 KB
testcase_13 AC 673 ms
83,052 KB
testcase_14 AC 687 ms
78,908 KB
testcase_15 AC 290 ms
66,264 KB
testcase_16 AC 445 ms
71,992 KB
testcase_17 AC 503 ms
72,592 KB
testcase_18 AC 269 ms
65,016 KB
testcase_19 AC 665 ms
82,620 KB
testcase_20 AC 656 ms
78,416 KB
testcase_21 AC 395 ms
72,328 KB
testcase_22 AC 637 ms
81,100 KB
testcase_23 AC 211 ms
64,300 KB
testcase_24 AC 544 ms
79,816 KB
testcase_25 AC 510 ms
77,760 KB
testcase_26 AC 540 ms
83,132 KB
testcase_27 AC 599 ms
85,196 KB
testcase_28 AC 417 ms
71,044 KB
testcase_29 AC 486 ms
75,644 KB
testcase_30 AC 598 ms
81,240 KB
testcase_31 AC 372 ms
69,556 KB
testcase_32 AC 508 ms
76,104 KB
testcase_33 AC 310 ms
65,284 KB
testcase_34 AC 196 ms
66,524 KB
testcase_35 AC 484 ms
88,872 KB
testcase_36 AC 562 ms
88,404 KB
testcase_37 AC 112 ms
56,520 KB
testcase_38 AC 596 ms
89,372 KB
testcase_39 AC 696 ms
105,860 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 = 1;
		UnionFind uf = new UnionFind(n);
		for (int i = 2; i <= m; i++) {
			List<Integer> wk = list.get(i);
			if (min == 1 && !wk.isEmpty()) {
				min = i;
			}
			for (int j = 1; j < wk.size(); j++) {
				uf.union(wk.get(0), wk.get(j));
			}
		}

		long num = uf.num - 1;
		if (min % 2 == 1 && num != 0) {
			num++;
		}
		long ans = num * 2;
		if (min == 3 && num == 2) {
			ans = 3;
		}
		System.out.println(ans);
	}

	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