結果

問題 No.7 プライムナンバーゲーム
ユーザー tshigitshigi
提出日時 2016-08-01 16:43:07
言語 Ruby
(3.3.0)
結果
AC  
実行時間 206 ms / 5,000 ms
コード長 558 bytes
コンパイル時間 108 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 21,632 KB
最終ジャッジ日時 2024-04-09 04:01:39
合計ジャッジ時間 3,207 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
12,416 KB
testcase_01 AC 104 ms
12,416 KB
testcase_02 AC 107 ms
12,544 KB
testcase_03 AC 102 ms
12,544 KB
testcase_04 AC 104 ms
12,416 KB
testcase_05 AC 105 ms
12,416 KB
testcase_06 AC 105 ms
12,672 KB
testcase_07 AC 110 ms
12,416 KB
testcase_08 AC 107 ms
12,416 KB
testcase_09 AC 137 ms
15,488 KB
testcase_10 AC 109 ms
12,288 KB
testcase_11 AC 105 ms
12,544 KB
testcase_12 AC 197 ms
20,352 KB
testcase_13 AC 106 ms
12,800 KB
testcase_14 AC 166 ms
19,072 KB
testcase_15 AC 161 ms
18,560 KB
testcase_16 AC 206 ms
21,632 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

require 'prime'

inputs = STDIN.readlines.map(&:chomp)
N = inputs[0].to_i

class Calc
  def initialize
    @primes = Prime.instance.each(N).to_a
    @memos = Array.new(N)
  end

  def dp(n, turn)
    if n == 0 || n == 1
      winner_is_me = true
    elsif !@memos[n].nil?
      winner_is_me = @memos[n]
    else
      ps = @primes.take_while { |p| p < n }.reverse
      winner_is_me = ps.any? { |p|
        dp(n - p, !turn) == turn
      }
    end
    @memos[n] = winner_is_me
    !(turn ^ winner_is_me)
  end
end

puts Calc.new.dp(N, true) ? 'Win' : 'Lose'
0