結果

問題 No.7 プライムナンバーゲーム
ユーザー kou_kkkkou_kkk
提出日時 2023-08-04 13:33:38
言語 Nim
(2.0.2)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 714 bytes
コンパイル時間 4,883 ms
コンパイル使用メモリ 66,560 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 11:12:07
合計ジャッジ時間 5,731 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import math, sequtils, strutils, sugar

let n = parseInt readLine stdin

var isPrimes = true.repeat n.succ
for i in [0, 1]:
  isPrimes[i] = false

let rn = toInt ceil sqrt toFloat n
for i in 2 .. rn:
  if not isPrimes[i]:
    continue
  
  for i2 in countup(i ^ 2, n, i):
    isPrimes[i2] = false

let primes = collect(newSeq):
  for i, v in isPrimes:
    if v: i

const
  win = 1
  lose = 0

var dp = repeat(-1, n.succ)

proc fn(v: int): int =
  result = lose
  
  for p in primes:
    if p > v:
      break
    
    let v2 = v - p
    if v2 in [0, 1]:
      continue
    
    if dp[v2] == -1:
      dp[v2] = fn v2
    
    if dp[v2] == lose:
      return win
    
if n.fn == win:
  echo "Win"
else:
  echo "Lose"
0