結果

問題 No.7 プライムナンバーゲーム
ユーザー te-shte-sh
提出日時 2017-01-11 18:19:30
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 21 ms / 5,000 ms
コード長 825 bytes
コンパイル時間 759 ms
コンパイル使用メモリ 100,460 KB
実行使用メモリ 7,748 KB
最終ジャッジ日時 2023-09-03 00:29:38
合計ジャッジ時間 2,056 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 21 ms
7,736 KB
testcase_03 AC 3 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 10 ms
7,740 KB
testcase_07 AC 8 ms
7,728 KB
testcase_08 AC 4 ms
7,252 KB
testcase_09 AC 12 ms
7,740 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 7 ms
7,740 KB
testcase_12 AC 15 ms
7,716 KB
testcase_13 AC 15 ms
7,720 KB
testcase_14 AC 19 ms
7,748 KB
testcase_15 AC 19 ms
7,736 KB
testcase_16 AC 18 ms
7,748 KB
権限があれば一括ダウンロードができます

ソースコード

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

  foreach (int i; iota(4, n + 1)) {
    auto ai = new bool[](i);
    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