結果

問題 No.7 プライムナンバーゲーム
ユーザー keibkeib
提出日時 2019-03-06 19:54:05
言語 Swift
(5.10.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 649 bytes
コンパイル時間 3,206 ms
コンパイル使用メモリ 181,328 KB
実行使用メモリ 16,000 KB
最終ジャッジ日時 2024-04-09 04:35:49
合計ジャッジ時間 4,242 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
15,232 KB
testcase_01 AC 11 ms
15,616 KB
testcase_02 AC 13 ms
15,616 KB
testcase_03 AC 11 ms
15,744 KB
testcase_04 AC 11 ms
15,616 KB
testcase_05 AC 11 ms
16,000 KB
testcase_06 AC 12 ms
15,744 KB
testcase_07 AC 11 ms
15,744 KB
testcase_08 AC 11 ms
15,616 KB
testcase_09 AC 12 ms
15,488 KB
testcase_10 AC 11 ms
15,616 KB
testcase_11 AC 12 ms
15,616 KB
testcase_12 AC 12 ms
15,744 KB
testcase_13 AC 13 ms
15,488 KB
testcase_14 AC 14 ms
15,616 KB
testcase_15 AC 13 ms
15,488 KB
testcase_16 AC 13 ms
15,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import Foundation


let n = Int(readLine()!)!

let primes =  { () -> [Int] in
  var multiples = Set<Int>()
  return (2 ..< n).filter { i in
    if i == 2 {
      return true
    }
    if i % 2 == 0 {
      return false
    }
    if multiples.contains(i) {
      return false
    }
    var m = 3
    while i * m < n {
      multiples.insert(i * m)
      m += 2
    }
    return true
  }
}()

var dp: [Bool] = [true, true]
for i in 2 ... n {
  var found = false
  for p in primes {
    if p > i {
      break
    }
    if !dp[i - p] {
      found = true
      break
    }
  }
  dp.append(found)
} 
if dp[n] {
  print("Win")
} else {
  print("Lose")
}
0