結果

問題 No.7 プライムナンバーゲーム
ユーザー d_nishiyama85d_nishiyama85
提出日時 2016-07-08 00:03:01
言語 Ruby
(3.3.0)
結果
AC  
実行時間 2,156 ms / 5,000 ms
コード長 608 bytes
コンパイル時間 60 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 19,584 KB
最終ジャッジ日時 2024-04-09 04:02:30
合計ジャッジ時間 16,235 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
12,544 KB
testcase_01 AC 98 ms
12,416 KB
testcase_02 AC 2,156 ms
19,584 KB
testcase_03 AC 223 ms
12,672 KB
testcase_04 AC 128 ms
12,672 KB
testcase_05 AC 128 ms
12,544 KB
testcase_06 AC 684 ms
14,592 KB
testcase_07 AC 498 ms
13,952 KB
testcase_08 AC 271 ms
12,928 KB
testcase_09 AC 1,000 ms
16,128 KB
testcase_10 AC 96 ms
12,416 KB
testcase_11 AC 501 ms
13,952 KB
testcase_12 AC 1,578 ms
17,664 KB
testcase_13 AC 1,668 ms
17,792 KB
testcase_14 AC 2,146 ms
19,456 KB
testcase_15 AC 2,032 ms
19,072 KB
testcase_16 AC 1,893 ms
18,560 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

require 'pp'

def main
  n = gets.to_i
  @primes = sieve n
  puts (solve(n) === 1) ? "Win" : "Lose"
end

def solve(n)
  dp = {}
  (2..n).each{|i|
    @primes.each {|p|
      if dp.key?(i - p) && dp[i-p] === 0
        dp[i] = 1
      end
    }
    if !dp.key?(i)
      dp[i] = 0
    end
  }
  dp[n]
end

# n 以下の素数をすべて出す
def sieve(n)
  search = *(2..n)
  primes = []
  i = 2
  while i * i <= n && !search.empty?
    p = search.shift
    primes << p
    _next = []
    search.each do |m|
      if m % p > 0
          _next << m
      end
    end
    search = _next
  end
  primes
end

main
0