結果

問題 No.7 プライムナンバーゲーム
ユーザー te-shte-sh
提出日時 2020-01-14 22:02:35
言語 Nim
(2.0.2)
結果
RE  
実行時間 -
コード長 1,041 bytes
コンパイル時間 5,183 ms
コンパイル使用メモリ 68,440 KB
実行使用メモリ 7,704 KB
最終ジャッジ日時 2023-08-27 05:05:30
合計ジャッジ時間 5,726 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 3 ms
4,376 KB
testcase_06 RE -
testcase_07 RE -
testcase_08 AC 7 ms
5,152 KB
testcase_09 AC 29 ms
7,704 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

# URL: https://yukicoder.me/problems/no/7

import algorithm, bitops, sequtils, strutils, sugar

proc bsearch[T](min, max: T, cmp: proc(x: T): bool): T =
  var
    tmin = min
    tmax = max
  while tmax-tmin > 1:
    var mid = (tmin+tmax) div 2
    if cmp(mid):
      tmin = mid
    else:
      tmax = mid
  if cmp(tmax): tmax elif cmp(tmin): tmin else: tmin-1

proc nsqrt[T](n: T): T =
  if n <= 1: return n
  var m = T(1) shl (n.fastLog2 div 2 + 1)
  bsearch(T(1), m, (x) => x*x <= n)

proc primes(n: int): seq[int] =
  var sieve = newSeqWith((n+1) div 2, true)
  for p in 1..((n.nsqrt-1) div 2):
    if sieve[p]:
      for q in countup(p*3+1, (n+1) div 2, p*2+1):
        sieve[q] = false
  result = newSeq[int](0)
  for p, b in sieve:
    if b: result.add(p*2+1)
  result[0] = 2

var
  n = stdin.readline.parseInt
  grundy = newSeq[int](n+1)
  p = primes(n)

for i in 4..n:
  var m = newSeq[bool](n+1)
  for pi in p:
    if i-pi < 2: break
    m[grundy[i-pi]] = true
  grundy[i] = m.find(false)

echo if grundy[n] > 0: "Win" else: "Lose"
0