結果

問題 No.658 テトラナッチ数列 Hard
ユーザー こまるこまる
提出日時 2020-10-24 20:28:46
言語 Haskell
(9.8.2)
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 2,060 bytes
コンパイル時間 7,176 ms
コンパイル使用メモリ 198,908 KB
実行使用メモリ 12,212 KB
最終ジャッジ日時 2023-09-28 20:47:59
合計ジャッジ時間 8,250 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,452 KB
testcase_01 AC 4 ms
7,380 KB
testcase_02 AC 3 ms
7,396 KB
testcase_03 AC 3 ms
7,620 KB
testcase_04 AC 13 ms
11,956 KB
testcase_05 AC 12 ms
11,912 KB
testcase_06 AC 13 ms
11,960 KB
testcase_07 AC 12 ms
12,032 KB
testcase_08 AC 12 ms
12,088 KB
testcase_09 AC 12 ms
12,164 KB
testcase_10 AC 12 ms
12,212 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 #

{-# LANGUAGE BangPatterns #-}

import           Control.Monad.State
import           Data.Char
import           Data.Coerce
import qualified Data.ByteString.Char8             as BSC8
import qualified Data.Vector.Fusion.Stream.Monadic as VFSM
import qualified Data.Vector.Unboxed               as VU
import qualified Data.Vector.Unboxed.Mutable       as VUM

main :: IO ()
main = do
  t <- VUM.unsafeNew 10001
  VUM.unsafeWrite t 0 (0 :: Int)
  VUM.unsafeWrite t 1 (0 :: Int)
  VUM.unsafeWrite t 2 (0 :: Int)
  VUM.unsafeWrite t 3 (1 :: Int)
  rep1 $ \i -> do
    item1 <- VUM.unsafeRead t (i - 1)
    item2 <- VUM.unsafeRead t (i - 2)
    item3 <- VUM.unsafeRead t (i - 3)
    item4 <- VUM.unsafeRead t (i - 4)
    VUM.unsafeWrite t i ((item1 + item2 + item3 + item4) `mod` 17)
  q  <- readLn :: IO Int
  qs <- parseN1 q
  rep2 q $ \i -> print =<< VUM.unsafeRead t (mod (qs VU.! i) 4912)

stream1 :: Monad m => Int -> Int -> VFSM.Stream m Int
stream1 !l !r = VFSM.Stream step l
  where
    step x
      | x <= r    = return $ VFSM.Yield x (x + 1)
      | otherwise = return $ VFSM.Done
    {-# INLINE [0] step #-}
{-# INLINE [1] stream1 #-}

rep1 :: Monad m => (Int -> m ()) -> m ()
rep1 = flip VFSM.mapM_ (stream1 4 10000)
{-# INLINE rep1 #-}

stream2 :: Monad m => Int -> Int -> VFSM.Stream m Int
stream2 !l !r = VFSM.Stream step l
  where
    step x
      | x < r     = return $ VFSM.Yield x (x + 1)
      | otherwise = return $ VFSM.Done
    {-# INLINE [0] step #-}
{-# INLINE [1] stream2 #-}

rep2 :: Monad m => Int -> (Int -> m ()) -> m ()
rep2 n = flip VFSM.mapM_ (stream2 0 n)
{-# INLINE rep2 #-}

type CParser a = StateT BSC8.ByteString Maybe a
runCParser :: CParser a -> BSC8.ByteString -> Maybe (a, BSC8.ByteString)
runCParser = runStateT
{-# INLINE runCParser #-}
int :: CParser Int
int = coerce $ BSC8.readInt . BSC8.dropWhile isSpace
{-# INLINE int #-}
int1 :: CParser Int
int1 = fmap (subtract 1) int
{-# INLINE int1 #-}
parseN1 :: Int -> IO (VU.Vector Int)
parseN1 n = VU.unfoldrN n (runCParser int1) <$> BSC8.getContents
{-# INLINE parseN1 #-}
0