結果

問題 No.7 プライムナンバーゲーム
ユーザー r6ever6eve
提出日時 2017-08-18 14:27:28
言語 OCaml
(5.1.0)
結果
AC  
実行時間 16 ms / 5,000 ms
コード長 780 bytes
コンパイル時間 241 ms
コンパイル使用メモリ 18,288 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-07-24 21:00:02
合計ジャッジ時間 1,177 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 16 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 6 ms
4,376 KB
testcase_07 AC 5 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 8 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 5 ms
4,376 KB
testcase_12 AC 13 ms
4,376 KB
testcase_13 AC 14 ms
4,380 KB
testcase_14 AC 16 ms
4,376 KB
testcase_15 AC 15 ms
4,380 KB
testcase_16 AC 14 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

let eratos n =
  let a = Array.make (n + 1) true in
  a.(0) <- false; a.(1) <- false;
  let rec aux i j =
    if j > n then ()
    else begin
      a.(j) <- false;
      aux i (i + j)
    end in
  let rec doit i lst =
    if i > n then List.rev lst
    else if not a.(i) then doit (i + 1) lst
    else begin
      aux i (i + i);
      doit (i + 1) (i :: lst)
    end in
  doit 2 []

let solve n =
  let ps = eratos 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