結果

問題 No.390 最長の数列
ユーザー tenten
提出日時 2020-09-29 13:54:41
言語 Java
(openjdk 23)
結果
AC  
実行時間 2,271 ms / 5,000 ms
コード長 1,064 bytes
コンパイル時間 2,326 ms
コンパイル使用メモリ 79,704 KB
実行使用メモリ 104,428 KB
最終ジャッジ日時 2024-07-03 20:22:30
合計ジャッジ時間 19,447 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static HashSet<Integer> exists = new HashSet<>();
    static TreeMap<Integer, Integer> dp = new TreeMap<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            exists.add(sc.nextInt());
        }
        int max = 0;
        for (int x : exists) {
            max = Math.max(max, dfw(x));
        }
        System.out.println(max);
    }
    
    static int dfw(int x) {
        if (dp.containsKey(x)) {
            return dp.get(x);
        }
        if (!exists.contains(x)) {
            dp.put(x, 0);
            return 0;
        }
        if (x == 1) {
            dp.put(x, 1);
            return 1;
        }
        int max = dfw(1);
        for (int i = 2; i <= Math.sqrt(x); i++) {
            if (x % i == 0) {
                max = Math.max(max, dfw(i));
                max = Math.max(max, dfw(x / i));
            }
        }
        dp.put(x, max + 1);
        return max + 1;
    }
}
0