結果

問題 No.7 プライムナンバーゲーム
ユーザー te-sh
提出日時 2017-04-14 12:32:37
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 828 bytes
コンパイル時間 877 ms
コンパイル使用メモリ 114,812 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-12 18:42:53
合計ジャッジ時間 1,402 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.bitmanip;

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; 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");
}

pure int[] primes(int n)
{
  import std.math, std.bitmanip;

  auto sieve = BitArray();
  sieve.length((n + 1) / 2);
  sieve = ~sieve;

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

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

  return r;
}
0