結果

問題 No.7 プライムナンバーゲーム
ユーザー te-sh
提出日時 2017-01-11 18:21:19
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 13 ms / 5,000 ms
コード長 841 bytes
コンパイル時間 870 ms
コンパイル使用メモリ 111,956 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-12 06:20:17
合計ジャッジ時間 1,409 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.math;      // math functions

void main()
{
  auto n = readln.chomp.to!int;

  auto pi = primes(n);
  auto gi = new int[](n + 1);
  auto ai = new bool[](n);

  foreach (int i; iota(4, n + 1)) {
    ai[] = false;
    foreach (p; pi) {
      if (i - p < 2) break;
      ai[gi[i - p]] = true;
    }
    gi[i] = ai.countUntil!"!a".to!int;
  }

  writeln(gi[n] == 0 ? "Lose" : "Win");
}

int[] primes(int n)
{
  auto sieve = new bool[]((n + 1) / 2);
  sieve[] = true;

  foreach (p; iota(1, (n.to!real.sqrt.to!int - 1) / 2 + 1))
    if (sieve[p])
      foreach (q; iota(p * 3 + 1, (n + 1) / 2, p * 2 + 1))
        sieve[q] = false;

  auto r = sieve.enumerate
    .filter!("a[1]")
    .map!("a[0]").map!("a * 2 + 1")
    .map!(to!int).array;
  r[0] = 2;

  return r;
}
0