結果
| 問題 |
No.58 イカサマなサイコロ
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-07-06 23:54:29 |
| 言語 | Haskell (9.10.1) |
| 結果 |
CE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 1,287 bytes |
| コンパイル時間 | 714 ms |
| コンパイル使用メモリ | 161,408 KB |
| 最終ジャッジ日時 | 2024-11-14 19:46:32 |
| 合計ジャッジ時間 | 1,075 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main ( Main.hs, Main.o )
Main.hs:9:10: error: [GHC-39999]
• No instance for ‘Applicative Probably’
arising from the superclasses of an instance declaration
• In the instance declaration for ‘Monad Probably’
|
9 | instance Monad Probably where
| ^^^^^^^^^^^^^^
ソースコード
import Data.Ratio
import Data.List
data Probably a = Prob{getProb :: [(a, Rational)]} deriving Show
instance Functor Probably where
fmap f (Prob xs) = Prob $ map (\(x,p) -> (f x, p)) xs
instance Monad Probably where
return x = Prob [(x, 1)]
m >>= f = flatten (fmap f m)
flatten :: Probably (Probably a) -> Probably a
flatten (Prob xs) = Prob $ concat $ map multAll xs
where multAll (Prob xs', p) = map (\(x, r) -> (x, r * p)) xs'
normalDice :: Probably Int
normalDice = Prob [(i, 1%6) | i <- [1..6]]
cheatingDice :: Probably Int
cheatingDice = Prob [(i, 1%6) | i <- concat(replicate 2 [4..6])]
integrate :: Probably Int -> Probably Int -> Probably Int
integrate a b = Prob $ fmap head $ group $ sort $ map (\(x, p) -> (x, sum $ fmap snd (filter (\(x', _) -> x == x') ps))) ps
where
Prob ps = do
a' <- a
b' <- b
return (a' + b')
throwDice :: [Probably Int] -> Probably Int
throwDice ps = foldl1 integrate ps
main :: IO()
main = do
[n, k] <- return . map (read::String->Int) . lines =<< getContents
let jiro = throwDice (replicate n normalDice)
let taro = throwDice $ (replicate (n-k) normalDice) ++ (replicate k cheatingDice)
print $ fromRational $ sum $ fmap (\(x, p) -> (* p) $ sum $ fmap snd $ filter (\(y, p') -> x > y) (getProb jiro)) (getProb taro)