結果
| 問題 |
No.7 プライムナンバーゲーム
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-03-29 13:40:18 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 8 ms / 5,000 ms |
| コード長 | 876 bytes |
| コンパイル時間 | 1,372 ms |
| コンパイル使用メモリ | 141,784 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-12 03:16:30 |
| 合計ジャッジ時間 | 2,009 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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 = [];
bool[] notPrime = []; notPrime.length = n + 1;
foreach (i; 2..(n+1)) {
if (notPrime[i]) { continue; }
p ~= i;
for (uint j = i; j <= n; j += i) {
notPrime[j] = true;
}
}
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;
}