結果

問題 No.7 プライムナンバーゲーム
ユーザー tshigtshig
提出日時 2016-08-04 15:21:49
言語 Ruby
(3.3.0)
結果
AC  
実行時間 210 ms / 5,000 ms
コード長 625 bytes
コンパイル時間 119 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 20,736 KB
最終ジャッジ日時 2024-04-09 04:01:43
合計ジャッジ時間 3,354 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
12,288 KB
testcase_01 AC 102 ms
12,288 KB
testcase_02 AC 103 ms
12,672 KB
testcase_03 AC 107 ms
12,416 KB
testcase_04 AC 101 ms
12,416 KB
testcase_05 AC 103 ms
12,288 KB
testcase_06 AC 104 ms
12,416 KB
testcase_07 AC 102 ms
12,416 KB
testcase_08 AC 103 ms
12,288 KB
testcase_09 AC 132 ms
15,360 KB
testcase_10 AC 101 ms
12,288 KB
testcase_11 AC 101 ms
12,416 KB
testcase_12 AC 194 ms
19,456 KB
testcase_13 AC 101 ms
12,672 KB
testcase_14 AC 161 ms
18,944 KB
testcase_15 AC 163 ms
18,560 KB
testcase_16 AC 210 ms
20,736 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

require 'prime'

class Calc0007
  def initialize(args)
    args = args.map { |l| l.chomp.split(/\s+/) }
    @n = args.shift.first.to_i
    @primes = Prime.instance.each(@n).to_a
    @memo = Array.new(@n + 1)
  end

  def calc
    dp(@n, true)
  end

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

  def run
    calc ? 'Win' : 'Lose'
  end
end

puts Calc0007.new(STDIN.readlines).run if __FILE__ == $0
0