結果

問題 No.7 プライムナンバーゲーム
ユーザー r6ever6eve
提出日時 2017-08-18 14:20:08
言語 OCaml
(5.1.0)
結果
AC  
実行時間 41 ms / 5,000 ms
コード長 807 bytes
コンパイル時間 221 ms
コンパイル使用メモリ 19,692 KB
実行使用メモリ 6,696 KB
最終ジャッジ日時 2024-04-09 04:22:36
合計ジャッジ時間 1,218 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,692 KB
testcase_01 AC 2 ms
6,696 KB
testcase_02 AC 40 ms
6,692 KB
testcase_03 AC 3 ms
6,688 KB
testcase_04 AC 2 ms
6,692 KB
testcase_05 AC 2 ms
6,692 KB
testcase_06 AC 12 ms
6,692 KB
testcase_07 AC 8 ms
6,696 KB
testcase_08 AC 4 ms
6,692 KB
testcase_09 AC 18 ms
6,692 KB
testcase_10 AC 1 ms
6,692 KB
testcase_11 AC 8 ms
6,692 KB
testcase_12 AC 29 ms
6,688 KB
testcase_13 AC 31 ms
6,688 KB
testcase_14 AC 41 ms
6,692 KB
testcase_15 AC 38 ms
6,692 KB
testcase_16 AC 36 ms
6,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

let eratos n =
  let a = Array.make (n + 1) true in
  a.(0) <- false; a.(1) <- false;
  let rec doit i j =
    if j > n then ()
    else begin
      a.(j) <- false;
      doit i (i + j)
    end in
  let doit i = doit i (i + i) in
  let m = float n |> sqrt |> truncate in
  for i = 2 to m do if a.(i) then doit i done;
  a

let solve n =
  let _, ps = Array.fold_right (fun e (i, lst) -> (i - 1, if e then i :: lst else lst)) (eratos n) (n, []) in
  let t = Array.make (n + 1) false in
  t.(0) <- true; t.(1) <- true;
  for i = 2 to n do
    let rec doit = function
      | [] -> ()
      | p :: _ when p > i -> ()
      | p :: ps ->
        t.(i) <- t.(i) || not t.(i - p);
        doit ps in
    doit ps
  done;
  t.(n)

let () =
  let n = read_int () in
  print_endline (if solve n then "Win" else "Lose")
0