結果

問題 No.458 異なる素数の和
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-12-11 16:45:23
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 897 bytes
コンパイル時間 591 ms
コンパイル使用メモリ 121,444 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-02 23:27:03
合計ジャッジ時間 2,453 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 39 ms
4,376 KB
testcase_02 AC 52 ms
4,376 KB
testcase_03 AC 9 ms
4,376 KB
testcase_04 AC 12 ms
4,376 KB
testcase_05 AC 112 ms
4,380 KB
testcase_06 AC 47 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 113 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 132 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 46 ms
4,376 KB
testcase_28 AC 130 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 26 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm.comparison;
import std.conv;
import std.stdio;
import std.string;


const int MAX_N = 20000;
const int UNDEF = -1;
immutable PRIMES = sieveOfEratosthenes(MAX_N);


pure @safe int[] sieveOfEratosthenes(const int end){
	assert(end > 1);
	auto isPrime = new bool[](end);
	isPrime[] = true;
	isPrime[0] = false;
	isPrime[1] = false;
	int[] primes;
	for (int i = 2; i < end; i++){
		if (isPrime[i]){
			primes ~= i;
			for (int j = 2 * i; j < end; j += i){
				isPrime[j] = false;
			}
		}
	}
	return primes;
}


pure @safe int compute(const int n){
	auto dp = new int[](n + 1);
	dp[] = UNDEF;
	dp[0] = 0;
	foreach (prime; PRIMES){
		if (prime > n) {
			break;
		}
		for (int i = n - 1; i >= 0; i--) {
			if (dp[i] != UNDEF && i + prime < n + 1){
				dp[i + prime] = max(dp[i + prime], dp[i] + 1);
			}
		}
	}
	return dp[n];
}


void main(){
	readln.chomp.to!int.compute.writeln;
}
0