結果

問題 No.7 プライムナンバーゲーム
ユーザー shi-moshi-mo
提出日時 2016-03-29 13:36:48
言語 Ruby
(3.3.0)
結果
AC  
実行時間 326 ms / 5,000 ms
コード長 537 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 24,064 KB
最終ジャッジ日時 2024-04-09 03:57:37
合計ジャッジ時間 3,207 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
12,288 KB
testcase_01 AC 82 ms
12,288 KB
testcase_02 AC 88 ms
12,416 KB
testcase_03 AC 87 ms
12,288 KB
testcase_04 AC 84 ms
12,288 KB
testcase_05 AC 91 ms
12,416 KB
testcase_06 AC 86 ms
12,416 KB
testcase_07 AC 85 ms
12,160 KB
testcase_08 AC 89 ms
12,160 KB
testcase_09 AC 147 ms
16,640 KB
testcase_10 AC 84 ms
12,416 KB
testcase_11 AC 85 ms
12,288 KB
testcase_12 AC 303 ms
22,912 KB
testcase_13 AC 85 ms
12,160 KB
testcase_14 AC 212 ms
19,584 KB
testcase_15 AC 212 ms
19,072 KB
testcase_16 AC 326 ms
24,064 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

n = gets.to_i

def prime_numbers(n)
  p = []
  not_prime = []

  2.upto(n) do |i|
    next if not_prime[i]

    p << i
    (i*2).step(n, i) do |j|
      not_prime[j] = true
    end
  end
  p
end

$p = prime_numbers(n).reverse!
$dp = [ true, true ] # $dp[i] means you win or don't when i is given to you.
def black_wins?(n)
  return $dp[n] unless $dp[n].nil?

  white_loses = false
  $p.select{|k| k <= n }.each do |i|
    break if (white_loses |= !black_wins?(n-i))
  end
  $dp[n] = white_loses
end

puts black_wins?(n) ? 'Win' : 'Lose'
0