結果

問題 No.458 異なる素数の和
ユーザー tentententen
提出日時 2020-09-10 15:52:18
言語 Java21
(openjdk 21)
結果
AC  
実行時間 173 ms / 2,000 ms
コード長 1,035 bytes
コンパイル時間 2,178 ms
コンパイル使用メモリ 76,464 KB
実行使用メモリ 56,336 KB
最終ジャッジ日時 2023-08-24 23:32:43
合計ジャッジ時間 7,899 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,864 KB
testcase_01 AC 145 ms
55,660 KB
testcase_02 AC 151 ms
56,132 KB
testcase_03 AC 140 ms
55,852 KB
testcase_04 AC 140 ms
55,820 KB
testcase_05 AC 165 ms
55,828 KB
testcase_06 AC 151 ms
56,300 KB
testcase_07 AC 126 ms
55,644 KB
testcase_08 AC 166 ms
55,776 KB
testcase_09 AC 137 ms
55,688 KB
testcase_10 AC 122 ms
55,512 KB
testcase_11 AC 173 ms
55,804 KB
testcase_12 AC 124 ms
56,032 KB
testcase_13 AC 125 ms
56,028 KB
testcase_14 AC 123 ms
56,076 KB
testcase_15 AC 124 ms
56,336 KB
testcase_16 AC 138 ms
55,748 KB
testcase_17 AC 121 ms
55,812 KB
testcase_18 AC 123 ms
56,128 KB
testcase_19 AC 122 ms
55,924 KB
testcase_20 AC 123 ms
56,252 KB
testcase_21 AC 122 ms
55,972 KB
testcase_22 AC 123 ms
55,860 KB
testcase_23 AC 123 ms
56,072 KB
testcase_24 AC 122 ms
55,904 KB
testcase_25 AC 122 ms
56,084 KB
testcase_26 AC 124 ms
55,760 KB
testcase_27 AC 147 ms
55,884 KB
testcase_28 AC 173 ms
56,084 KB
testcase_29 AC 136 ms
53,992 KB
testcase_30 AC 143 ms
56,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	ArrayList<Integer> primes = new ArrayList<>();
    	int[] counts = new int[n + 1];
    	Arrays.fill(counts, -1);
    	counts[0] = 0;
    	for (int i = 2; i <= n; i++) {
    	    if (isPrime(i, primes)) {
    	        primes.add(i);
    	        for (int j = n - i; j >= 0; j--) {
    	            if (counts[j] < 0) {
    	                continue;
    	            }
    	            counts[j + i] = Math.max(counts[j + i], counts[j] + 1);
    	        }
    	    }
    	}
    	if (counts[n] > 0) {
    	    System.out.println(counts[n]);
    	} else {
    	    System.out.println(-1);
    	}
    }
    
    static boolean isPrime(int x, ArrayList<Integer> primes) {
        for (int y : primes) {
            if (y * y > x) {
                break;
            }
            if (x % y == 0) {
                return false;
            }
        }
        return true;
    }
}
0