結果

問題 No.265 数学のテスト
ユーザー ともきともき
提出日時 2015-08-08 03:27:53
言語 Haskell
(9.8.2)
結果
TLE  
実行時間 -
コード長 2,354 bytes
コンパイル時間 1,221 ms
コンパイル使用メモリ 175,064 KB
実行使用メモリ 17,152 KB
最終ジャッジ日時 2023-09-25 06:44:48
合計ジャッジ時間 4,527 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
16,060 KB
testcase_01 AC 12 ms
13,580 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative
import Control.Monad.Reader
import qualified Data.List as L

data Exp = Con   Int
         | Var
         | 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, 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 :: 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 (Con a) x)   = do {r <- eval x; return $ map (* a) r}
eval e@(Mult Var _)     = eval e
eval e@(Mult _ (Con _)) = eval e
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 $ foldr (++) ""  (L.intersperse " " (map show (solve d s)))
0