type Judge = Perfect | Great | Good | Miss type Notes = { A: int; B: int; C: int; D: int } type Status = { Chain: int; Score: int; Notes: Notes } type Result = Score of int | Failed type State = Playing of Status | Finished of Result let simulateBest a b c d = let calc (chain: int) (judge: Judge): int = match judge with | Perfect -> 100 * pown 2 (chain / 100) | Great -> 50 * pown 2 (chain / 100) | _ -> 0 let update (status: Status): State = match status.Notes with | { D = 10 } -> Finished Failed // 死 | { C = 0; D = 0 } as n -> match n with | { A = 0; B = 0 } -> Finished <| Score status.Score | n when n.B > 0 -> let newChain = status.Chain + 1 let newScore = status.Score + (calc status.Chain Judge.Great) let newNotes = { n with B = n.B - 1 } Playing { Chain = newChain; Score = newScore; Notes = newNotes } | n -> let newChain = status.Chain + 1 let newScore = status.Score + (calc status.Chain Judge.Perfect) let newNotes = { n with A = n.A - 1 } Playing { Chain = newChain; Score = newScore; Notes = newNotes } | n -> Playing { status with Notes = { n with C = 0; D = 0 } } // C, Dを消費しきる let rec f (state: State): string = match state with | Finished x -> match x with | Score x -> sprintf "Possible\n%i" x | Failed -> "Impossible" | Playing s -> f <| update s f <| Playing { Chain = 0; Score = 0; Notes = { A = a; B = b; C = c; D = d } } let A, B, C, D = let t = stdin.ReadLine().Split() |> Array.map int in t.[0], t.[1], t.[2], t.[3] simulateBest A B C D |> stdout.WriteLine