結果

問題 No.7 プライムナンバーゲーム
ユーザー taktak
提出日時 2019-04-28 13:41:09
言語 F#
(F# 4.0)
結果
WA  
実行時間 -
コード長 1,520 bytes
コンパイル時間 6,442 ms
コンパイル使用メモリ 165,904 KB
実行使用メモリ 31,880 KB
最終ジャッジ日時 2023-08-22 07:13:10
合計ジャッジ時間 9,400 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
22,252 KB
testcase_01 AC 69 ms
22,284 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 74 ms
20,276 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 113 ms
28,196 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 123 ms
30,024 KB
testcase_14 AC 139 ms
29,184 KB
testcase_15 AC 132 ms
28,880 KB
testcase_16 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) F# Compiler version 11.0.0.0 for F# 5.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

ソースコード

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