結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
12,416 KB
testcase_01 AC 83 ms
12,032 KB
testcase_02 AC 155 ms
12,160 KB
testcase_03 AC 91 ms
12,160 KB
testcase_04 AC 88 ms
12,160 KB
testcase_05 AC 88 ms
12,416 KB
testcase_06 AC 108 ms
12,416 KB
testcase_07 AC 100 ms
12,288 KB
testcase_08 AC 91 ms
12,416 KB
testcase_09 AC 187 ms
17,792 KB
testcase_10 AC 83 ms
12,032 KB
testcase_11 AC 100 ms
12,160 KB
testcase_12 AC 352 ms
27,136 KB
testcase_13 AC 138 ms
12,416 KB
testcase_14 AC 280 ms
23,552 KB
testcase_15 AC 274 ms
22,912 KB
testcase_16 AC 386 ms
29,952 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

n = gets.to_i

def prime_numbers(n)
  p = []
  2.upto(n) do |i|
    is_prime = true
    p.each do |p_|
      if 0 == i % p_
        is_prime = false
        break
      end
    end
    p << i if is_prime
  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