Submission #43753179


Source Code Expand

#!/usr/bin/env stack
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeApplications #-}

#ifdef DEBUG
import Debug.Trace (traceShow)
#endif

import Control.Monad (forM_, when)
import Data.Array.IO (
  IOArray,
  Ix,
  MArray (newArray),
  readArray,
  writeArray,
 )
import qualified Data.ByteString.Char8 as BS
import Data.Char (isSpace)
import Data.List (unfoldr)
import Debug.Trace (traceShow)

solve :: Int -> IO IntMod
solve k
  | k `mod` 9 == 0 = do
    -- 桁和が K になる、0 を含まない整数の通り数
    dp <- newArray @IOArray (0, k) 0

    writeArray dp 0 1

    forM_ [0 .. k - 1] $ \i -> do
      x <- readArray dp i

      forM_ [1 .. 9] $ \j -> do
        when (i + j <= k) $ do
          updateWith (+) dp (i + j) x

    readArray dp k
  | otherwise = return 0

main :: IO ()
main = do
  k <- readLn @Int

  (IntMod x) <- solve k

  print x

{-- Library --}

modulus :: Int
modulus = 1000000007 -- 10^9 + 7

newtype IntMod = IntMod Int deriving (Eq, Show)

instance Num IntMod where
  IntMod x + IntMod y = IntMod ((x + y) `mod` modulus)
  IntMod x - IntMod y = IntMod ((x - y) `mod` modulus)
  IntMod x * IntMod y = IntMod ((x * y) `mod` modulus)
  fromInteger n = IntMod (fromInteger (n `mod` fromIntegral modulus))
  abs = abs
  signum = signum

updateWith :: (MArray a e m, Ix i) => (e -> e -> e) -> a i e -> i -> e -> m ()
updateWith f arr ix x = do
  v <- readArray arr ix
  writeArray arr ix $! f v x
{-# INLINE updateWith #-}

getInts :: IO [Int]
getInts = unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine

getTuple :: IO (Int, Int)
getTuple = do
  [a, b] <- getInts
  return (a, b)

#ifdef DEBUG
dbg :: Show a => a -> ()
dbg !x = let !_ = traceShow x () in ()
#else
dbg :: Show a => a -> ()
dbg _ = ()
#endif

Submission Info

Submission Time
Task 042 - Multiple of 9(★4)
User toyboot4e
Language Haskell (GHC 8.8.3)
Score 4
Code Size 1895 Byte
Status AC
Exec Time 31 ms
Memory 7300 KiB

Compile Error

Loaded package environment from /home/contestant/.ghc/x86_64-linux-8.8.3/environments/default

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 4 / 4
Status
AC × 2
AC × 62
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 10_small_00.txt, 10_small_01.txt, 10_small_02.txt, 10_small_03.txt, 10_small_04.txt, 10_small_05.txt, 10_small_06.txt, 10_small_07.txt, 10_small_08.txt, 10_small_09.txt, 10_small_10.txt, 10_small_11.txt, 10_small_12.txt, 10_small_13.txt, 10_small_14.txt, 10_small_15.txt, 10_small_16.txt, 10_small_17.txt, 10_small_18.txt, 10_small_19.txt, 11_large_00.txt, 11_large_01.txt, 11_large_02.txt, 11_large_03.txt, 11_large_04.txt, 11_large_05.txt, 11_large_06.txt, 11_large_07.txt, 11_large_08.txt, 11_large_09.txt, 11_large_10.txt, 11_large_11.txt, 11_large_12.txt, 11_large_13.txt, 11_large_14.txt, 11_large_15.txt, 11_large_16.txt, 11_large_17.txt, 11_large_18.txt, 11_large_19.txt, 99_hand_00.txt, 99_hand_01.txt, 99_hand_02.txt, 99_hand_03.txt, 99_hand_04.txt, 99_hand_05.txt, 99_hand_06.txt, 99_hand_07.txt, 99_hand_08.txt, 99_hand_09.txt, 99_hand_10.txt, 99_hand_11.txt, 99_hand_12.txt, 99_hand_13.txt, 99_hand_14.txt, 99_hand_15.txt, 99_hand_16.txt, 99_hand_17.txt, 99_hand_18.txt, 99_hand_19.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 7 ms 3820 KiB
00_sample_01.txt AC 2 ms 3856 KiB
10_small_00.txt AC 2 ms 3876 KiB
10_small_01.txt AC 2 ms 3644 KiB
10_small_02.txt AC 2 ms 3808 KiB
10_small_03.txt AC 2 ms 3816 KiB
10_small_04.txt AC 2 ms 3732 KiB
10_small_05.txt AC 2 ms 3716 KiB
10_small_06.txt AC 2 ms 3848 KiB
10_small_07.txt AC 2 ms 3800 KiB
10_small_08.txt AC 2 ms 3944 KiB
10_small_09.txt AC 2 ms 3828 KiB
10_small_10.txt AC 2 ms 3844 KiB
10_small_11.txt AC 3 ms 3720 KiB
10_small_12.txt AC 2 ms 3664 KiB
10_small_13.txt AC 2 ms 3700 KiB
10_small_14.txt AC 4 ms 3796 KiB
10_small_15.txt AC 2 ms 3828 KiB
10_small_16.txt AC 2 ms 3736 KiB
10_small_17.txt AC 2 ms 3832 KiB
10_small_18.txt AC 2 ms 3876 KiB
10_small_19.txt AC 2 ms 3672 KiB
11_large_00.txt AC 31 ms 7048 KiB
11_large_01.txt AC 26 ms 7052 KiB
11_large_02.txt AC 23 ms 6932 KiB
11_large_03.txt AC 24 ms 6932 KiB
11_large_04.txt AC 25 ms 7052 KiB
11_large_05.txt AC 31 ms 7008 KiB
11_large_06.txt AC 23 ms 7044 KiB
11_large_07.txt AC 25 ms 7008 KiB
11_large_08.txt AC 24 ms 7024 KiB
11_large_09.txt AC 28 ms 7144 KiB
11_large_10.txt AC 28 ms 6940 KiB
11_large_11.txt AC 27 ms 7016 KiB
11_large_12.txt AC 23 ms 7024 KiB
11_large_13.txt AC 24 ms 7040 KiB
11_large_14.txt AC 23 ms 7020 KiB
11_large_15.txt AC 24 ms 7044 KiB
11_large_16.txt AC 27 ms 6940 KiB
11_large_17.txt AC 24 ms 7080 KiB
11_large_18.txt AC 28 ms 7068 KiB
11_large_19.txt AC 30 ms 7036 KiB
99_hand_00.txt AC 2 ms 3724 KiB
99_hand_01.txt AC 8 ms 3808 KiB
99_hand_02.txt AC 10 ms 4912 KiB
99_hand_03.txt AC 29 ms 7300 KiB
99_hand_04.txt AC 2 ms 3812 KiB
99_hand_05.txt AC 2 ms 3816 KiB
99_hand_06.txt AC 3 ms 3928 KiB
99_hand_07.txt AC 2 ms 3640 KiB
99_hand_08.txt AC 2 ms 3712 KiB
99_hand_09.txt AC 2 ms 3784 KiB
99_hand_10.txt AC 2 ms 3704 KiB
99_hand_11.txt AC 2 ms 3852 KiB
99_hand_12.txt AC 2 ms 3848 KiB
99_hand_13.txt AC 2 ms 3812 KiB
99_hand_14.txt AC 2 ms 3784 KiB
99_hand_15.txt AC 2 ms 3784 KiB
99_hand_16.txt AC 2 ms 3704 KiB
99_hand_17.txt AC 2 ms 3852 KiB
99_hand_18.txt AC 2 ms 3936 KiB
99_hand_19.txt AC 2 ms 3820 KiB