-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp113.hs
46 lines (38 loc) · 1.33 KB
/
p113.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import Control.Monad.State
import qualified Data.Map as M
maxDigs :: Int
maxDigs = 100
numWaysUnderN :: Int -> Int -> State (M.Map (Int, Int) Integer) Integer
numWaysUnderN n 0 = return 1
numWaysUnderN n len = do
m <- get
if M.member (n, len) m then return $ m M.! (n, len)
else do
res <- sequence $ map (\x -> numWaysUnderN x (len - 1)) [0..n]
let tot = 1 + sum res
modify (M.insert (n, len) tot)
return tot
numWaysOverN :: Int -> Int -> State (M.Map (Int, Int) Integer) Integer
numWaysOverN n 0 = return 1
numWaysOverN n len = do
m <- get
if M.member (n, len) m then return $ m M.! (n, len)
else do
res <- sequence $ map (\x -> numWaysOverN x (len - 1)) [n..9]
let tot = 1 + sum res
modify (M.insert (n, len) tot)
return tot
numWaysUnder :: State (M.Map (Int, Int) Integer) Integer
numWaysUnder = do
res <- sequence $ map (\x -> numWaysUnderN x (maxDigs - 1)) [1..9]
return $ sum res
numWaysOver :: State (M.Map (Int, Int) Integer) Integer
numWaysOver = do
res <- sequence $ map (\x -> numWaysOverN x (maxDigs - 1)) [1..9]
return $ sum res
numConstant :: Int
numConstant = 9 * maxDigs
numNonBouncy :: Integer
numNonBouncy = (evalState numWaysOver M.empty) +
(evalState numWaysUnder M.empty) -
fromIntegral numConstant