結果

問題 No.7 プライムナンバーゲーム
ユーザー tak
提出日時 2019-04-28 13:41:09
言語 F#
(F# 4.0)
結果
WA  
実行時間 -
コード長 1,520 bytes
コンパイル時間 16,895 ms
コンパイル使用メモリ 185,624 KB
実行使用メモリ 40,448 KB
最終ジャッジ日時 2024-12-16 02:25:09
合計ジャッジ時間 15,562 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 WA * 10
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.fsproj を復元しました (636 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