結果

問題 No.1730 GCD on Blackboard in yukicoder
ユーザー ks2mks2m
提出日時 2021-11-05 22:07:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,753 ms / 2,000 ms
コード長 2,386 bytes
コンパイル時間 3,139 ms
コンパイル使用メモリ 74,996 KB
実行使用メモリ 144,884 KB
最終ジャッジ日時 2023-08-07 23:08:45
合計ジャッジ時間 18,614 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 978 ms
76,296 KB
testcase_01 AC 79 ms
57,516 KB
testcase_02 AC 85 ms
57,420 KB
testcase_03 AC 88 ms
58,116 KB
testcase_04 AC 84 ms
58,192 KB
testcase_05 AC 87 ms
57,368 KB
testcase_06 AC 88 ms
58,276 KB
testcase_07 AC 89 ms
58,168 KB
testcase_08 AC 85 ms
57,388 KB
testcase_09 AC 88 ms
57,516 KB
testcase_10 AC 106 ms
58,388 KB
testcase_11 AC 78 ms
57,572 KB
testcase_12 AC 80 ms
57,336 KB
testcase_13 AC 1,482 ms
135,772 KB
testcase_14 AC 1,539 ms
133,988 KB
testcase_15 AC 1,573 ms
141,072 KB
testcase_16 AC 1,512 ms
142,316 KB
testcase_17 AC 1,649 ms
142,176 KB
testcase_18 AC 1,238 ms
141,156 KB
testcase_19 AC 1,753 ms
144,884 KB
testcase_20 AC 653 ms
81,388 KB
testcase_21 AC 667 ms
81,596 KB
testcase_22 AC 994 ms
77,620 KB
testcase_23 AC 535 ms
81,084 KB
testcase_24 AC 465 ms
80,800 KB
testcase_25 AC 1,427 ms
144,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
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();

		Eratosthenes era = new Eratosthenes(1000000);
		int[] cnt = new int[1000001];
		Map<Integer, List<Integer>> map = new HashMap<>();
		for (int i = 0; i < n; i++) {
			if (era.isSosuu(a[i])) {
				cnt[1]++;
				cnt[a[i]]++;
			} else {
				List<Integer> list = map.get(a[i]);
				if (list == null) {
					list = divList(a[i]);
					map.put(a[i], list);
				}
				for (int e : list) {
					cnt[e]++;
				}
			}
		}

		int[] ans = new int[n];
		for (int i = 1000000; i > 0; i--) {
			if (cnt[i] > 0) {
				int x = n - cnt[i];
				if (ans[x] == 0) {
					ans[x] = i;
				}
			}
		}
		for (int i = 1; i < n; i++) {
			ans[i] = Math.max(ans[i], ans[i - 1]);
		}

		PrintWriter pw = new PrintWriter(System.out);
		for (int i : ans) {
			pw.println(i);
		}
		pw.flush();
	}

	static List<Integer> divList(int n) {
		List<Integer> list = new ArrayList<>();
		int end = (int) Math.sqrt(n);
		for (int i = 1; i <= end; i++) {
			if (n % i == 0) {
				list.add(i);
			}
		}
		int i = end * end == n ? list.size() - 2 : list.size() - 1;
		for ( ; i >= 0; i--) {
			list.add(n / list.get(i));
		}
		return list;
	}

	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