結果

問題 No.7 プライムナンバーゲーム
ユーザー taktak
提出日時 2019-04-28 13:41:09
言語 F#
(F# 4.0)
結果
WA  
実行時間 -
コード長 1,520 bytes
コンパイル時間 15,519 ms
コンパイル使用メモリ 186,392 KB
実行使用メモリ 42,380 KB
最終ジャッジ日時 2024-05-09 13:05:53
合計ジャッジ時間 17,341 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
29,952 KB
testcase_01 AC 64 ms
30,208 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 64 ms
30,952 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 80 ms
37,120 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 82 ms
38,756 KB
testcase_14 AC 87 ms
40,448 KB
testcase_15 AC 86 ms
40,064 KB
testcase_16 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.fsproj を復元しました (428 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

#nowarn "40"

open System

module Prime =
    let isPrimes max =
        let isPrime = Array.init (max + 1) (fun _ -> true)
        isPrime.[0] <- false
        isPrime.[1] <- false
        for i in 2 .. max do
            if isPrime.[i] then
                for j in 2 * i .. i .. max do 
                    isPrime.[j] <- false
            else
                ()            
        isPrime

    let primeNums max =
        let isPrimes = isPrimes max
        [ for i in 0 .. max do if isPrimes.[i] then yield i ]        

let memoize f =
    let memo = new Collections.Generic.Dictionary<_,_>()
    (fun x ->
        match memo.TryGetValue x with
        | true, v -> v
        | _ -> 
            memo.[x] <- f x
            memo.[x])

let isWinable n =
    let primes = Prime.primeNums n
    let rec dfs =
        let inter = memoize (fun (num, isMyTurn) ->
            match num, isMyTurn with
            | x, _ when x < 0 -> failwith "Error"
            | 0, true | 1, true -> true
            | 0, false | 1, false -> false
            | x, turn when x = n ->  
                primes 
                |> Seq.where(fun p -> x - p >= 0)
                |> Seq.exists (fun y -> dfs (x - y, not turn)) 
            | x, turn ->
                primes 
                |> Seq.where(fun p -> x - p >= 0)
                |> Seq.forall (fun y -> dfs (x - y, not turn)))
        inter
    dfs (n, true)
    
let N = Console.ReadLine() |> int

isWinable N
|> function
| true -> "Win"
| _ -> "Lose"
|> Console.WriteLine
0