結果
問題 | No.458 異なる素数の和 |
ユーザー | はむ吉🐹 |
提出日時 | 2016-12-11 13:56:03 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 160 ms / 2,000 ms |
コード長 | 835 bytes |
コンパイル時間 | 924 ms |
コンパイル使用メモリ | 94,208 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-12 05:30:04 |
合計ジャッジ時間 | 2,999 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
import std.algorithm.comparison; import std.conv; import std.stdio; import std.string; const int UNDEF = -1; 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 primes = sieveOfEratosthenes(n + 1); auto dp = new int[](n + 1); dp[] = UNDEF; dp[0] = 0; foreach (prime; primes){ 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; }