{-# OPTIONS_GHC -O2 -funbox-strict-fields #-} import Control.Applicative import Control.Monad.Reader import qualified Data.List as L data Exp = Con Int | Var String | Mult Exp Exp | Plus Exp Exp | Bibun Exp deriving (Show) parse :: String -> Exp parse s = case expression s of (a, "") -> a (a, s ) -> error ("remain: " ++ s) expression :: String -> (Exp, String) expression s = case r of '+' : t -> (Plus h k, a) where (k,a) = expression t _ -> (h, r) where (h,r) = term s term :: String -> (Exp, String) term s = case r of '*' : t -> (Mult h k, a) where (k,a) = (term t) _ -> (h, r) where (h,r) = factor s factor :: String -> (Exp, String) factor ('d' : r) = (Bibun h, a) where (h,k) = expression (tail r) -- remove '{' a = tail k -- remove '}' factor ('x' : r) = (Var "x", r) factor s = (Con x, r) where [(x,r)] = reads s -- list size is constant. -- eval :: Exp -> [Int] -- eval (Con a) = a : [0,0 ..] -- eval (Var _) = 0 : 1 : [0,0..] -- eval (Mult x (Var _)) = 0 : (eval x) -- eval (Mult x (Con a)) = map (* a) $ eval x -- eval (Mult (Var _) x) = 0 : (eval x) -- eval (Mult (Con a) x) = map (* a) $ eval x -- eval (Mult _ _) = error "mult" -- eval (Plus l r) = zipWith (+) (eval l) (eval r) -- eval (Bibun r) = zipWith (*) (tail $ eval r) [1..] -- solve :: Int -> String -> [Int] -- solve max_degree = (take (max_degree+1)) . eval . parse -- eval :: Int -> Exp -> [Int] -- eval m (Con a) = take m (a : [0,0 ..]) -- eval m (Var _) = take m (0 : 1 : [0,0..]) -- eval m (Mult x (Var _)) = 0 : (init (eval m x)) -- eval m (Mult (Var _) x) = 0 : (init (eval m x)) -- eval m (Mult x (Con a)) = map (* a) $ eval m x -- eval m (Mult (Con a) x) = map (* a) $ eval m x -- eval m (Mult _ _) = error "mult" -- eval m (Plus l r) = zipWith (+) (eval m l) (eval m r) -- eval m (Bibun r) = zipWith (*) ((tail $ eval m r) ++ [0]) [1..] -- solve :: Int -> String -> [Int] -- solve max_degree = (eval (max_degree+1)) . parse eval :: Exp -> Reader Int [Int] eval (Con a) = do {m <- ask; return $ take m (a : [0,0 ..])} eval (Var _) = do {m <- ask; return $ take m (0 : 1 : [0,0..])} eval (Mult x (Var _)) = do {r <- eval x; return $ 0 : (init r)} eval (Mult (Var _) x) = do {r <- eval x; return $ 0 : (init r)} eval (Mult x (Con a)) = do {r <- eval x; return $ map (* a) r} eval (Mult (Con a) x) = do {r <- eval x; return $ map (* a) r} eval (Mult _ _) = error "mult" eval (Plus l r) = do {a <- eval l; b <- eval r; return $ zipWith (+) a b} eval (Bibun r) = do {k <- eval r; return $ zipWith (*) ((tail k) ++ [0]) [1..]} solve :: Int -> String -> [Int] solve m s = runReader (eval (parse s)) (m+1) main :: IO () main = do n <- read <$> getLine :: IO Int d <- read <$> getLine :: IO Int s <- getLine putStrLn $ foldl (++) "" (L.intersperse " " (map show (solve d s)))