結果

問題 No.458 異なる素数の和
ユーザー ぴろずぴろず
提出日時 2016-12-09 00:28:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 179 ms / 2,000 ms
コード長 1,100 bytes
コンパイル時間 2,260 ms
コンパイル使用メモリ 81,384 KB
実行使用メモリ 56,216 KB
最終ジャッジ日時 2023-09-18 14:53:40
合計ジャッジ時間 7,961 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
55,732 KB
testcase_01 AC 154 ms
55,524 KB
testcase_02 AC 157 ms
55,728 KB
testcase_03 AC 143 ms
55,796 KB
testcase_04 AC 144 ms
56,080 KB
testcase_05 AC 171 ms
55,808 KB
testcase_06 AC 156 ms
55,772 KB
testcase_07 AC 136 ms
55,764 KB
testcase_08 AC 172 ms
55,800 KB
testcase_09 AC 144 ms
55,828 KB
testcase_10 AC 131 ms
55,716 KB
testcase_11 AC 179 ms
55,884 KB
testcase_12 AC 131 ms
55,696 KB
testcase_13 AC 132 ms
55,656 KB
testcase_14 AC 131 ms
55,708 KB
testcase_15 AC 130 ms
55,540 KB
testcase_16 AC 141 ms
55,828 KB
testcase_17 AC 130 ms
55,836 KB
testcase_18 AC 131 ms
55,976 KB
testcase_19 AC 132 ms
55,736 KB
testcase_20 AC 130 ms
55,816 KB
testcase_21 AC 131 ms
56,016 KB
testcase_22 AC 132 ms
55,936 KB
testcase_23 AC 130 ms
55,824 KB
testcase_24 AC 131 ms
55,584 KB
testcase_25 AC 131 ms
55,692 KB
testcase_26 AC 132 ms
55,884 KB
testcase_27 AC 154 ms
55,672 KB
testcase_28 AC 178 ms
55,876 KB
testcase_29 AC 142 ms
56,216 KB
testcase_30 AC 150 ms
55,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no458;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static int INF = 1 << 29;
	public static void main(String[] args) {
		int n = new Scanner(System.in).nextInt();
		ArrayList<Integer> primes = Prime.primeList(20000);
		int[] dp = new int[n+1];
		Arrays.fill(dp, -INF);
		dp[0] = 0;
		for(int p:primes) {
			for(int i=n;i>=p;i--) {
				dp[i] = Math.max(dp[i], dp[i-p] + 1);
			}
		}
		System.out.println(dp[n] < 0 ? -1 : dp[n]);
	}

}
class Prime {
	public static boolean[] isPrimeArray(int max) {
		boolean[] isPrime = new boolean[max+1];
		Arrays.fill(isPrime, true);
		isPrime[0] = isPrime[1] = false;
		for(int i=2;i*i<=max;i++) {
			if (isPrime[i]) {
				int j = i * 2;
				while(j<=max) {
					isPrime[j] = false;
					j += i;
				}
			}
		}
		return isPrime;
	}
	public static ArrayList<Integer> primeList(int max) {
		boolean[] isPrime = isPrimeArray(max);
		ArrayList<Integer> primeList = new ArrayList<Integer>();
		for(int i=2;i<=max;i++) {
			if (isPrime[i]) {
				primeList.add(i);
			}
		}
		return primeList;
	}
}
0