結果

問題 No.58 イカサマなサイコロ
ユーザー HaarHaar
提出日時 2016-07-06 23:54:29
言語 Haskell
(9.8.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,287 bytes
コンパイル時間 4,396 ms
コンパイル使用メモリ 161,152 KB
最終ジャッジ日時 2024-04-21 00:38:22
合計ジャッジ時間 4,951 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、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
  |          ^^^^^^^^^^^^^^

ソースコード

diff #

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)
0