結果
問題 | No.1730 GCD on Blackboard in yukicoder |
ユーザー |
![]() |
提出日時 | 2021-11-05 22:07:45 |
言語 | Java (openjdk 23) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 2,386 bytes |
コンパイル時間 | 2,510 ms |
コンパイル使用メモリ | 78,444 KB |
実行使用メモリ | 139,212 KB |
最終ジャッジ日時 | 2024-11-08 04:39:40 |
合計ジャッジ時間 | 25,485 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 23 TLE * 1 |
ソースコード
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;}}}