結果
| 問題 |
No.7 プライムナンバーゲーム
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-03-29 12:34:12 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 10 ms / 5,000 ms |
| コード長 | 882 bytes |
| コンパイル時間 | 1,506 ms |
| コンパイル使用メモリ | 136,704 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-12 03:16:19 |
| 合計ジャッジ時間 | 2,281 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 17 |
ソースコード
import std.stdio;
import std.algorithm;
import std.array;
uint[] p;
bool[] dp;
bool[] examined;
void main() {
uint n; readf("%s\n", &n);
p = primeNumbers(n); reverse(p);
dp = [ true, true ]; dp.length = n + 1;
examined = [ true, true ]; examined.length = n + 1;
writeln(blackWins(n) ? "Win" : "Lose");
}
uint[] primeNumbers(uint n) {
uint[] p = [];
foreach (i; 2..(n+1)) {
bool isPrime = true;
foreach (p_; p) {
if (0 == i % p_) {
isPrime = false;
break;
}
}
if (isPrime) { p ~= i; }
}
return p;
}
bool blackWins(uint n) {
if (examined[n]) { return dp[n]; }
bool whiteLoses = false;
foreach (i; p.filter!(a => a <= n)) {
if (whiteLoses |= !blackWins(n-i)) { break; }
}
examined[n] = true;
return dp[n] = whiteLoses;
}