結果

問題 No.1138 No Bingo!
ユーザー こまる
提出日時 2020-10-14 17:39:28
言語 Haskell
(9.10.1)
結果
AC  
実行時間 15 ms / 3,000 ms
コード長 1,806 bytes
コンパイル時間 4,087 ms
コンパイル使用メモリ 176,256 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2024-07-20 19:14:23
合計ジャッジ時間 4,294 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

{-# LANGUAGE BangPatterns               #-}

import           Control.Monad
import           Control.Monad.ST
import           Data.STRef
import qualified Data.Vector.Fusion.Stream.Monadic as VFSM
import qualified Data.Vector.Unboxed.Mutable       as VUM
import           GHC.Exts

stream :: Monad m => Int -> Int -> VFSM.Stream m Int
stream !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] stream #-}

rep1 :: Monad m => Int -> (Int -> m ()) -> m ()
rep1 n = flip VFSM.mapM_ (stream 1 (n + 1))

rep2 :: Monad m => Int -> (Int -> m ()) -> m ()
rep2 n = flip VFSM.mapM_ (stream 2 (n + 1))

main :: IO ()
main = readLn >>= print . solver

solver :: Int -> Int
solver n = runST $ do
  as <- VUM.unsafeNew 202020
  bs <- VUM.unsafeNew 202020
  ss <- newSTRef (1 :: Int)
  rep1 n $ \i -> modifySTRef' ss (\s -> s * i `mod` 998244353)
  VUM.unsafeWrite as 0 (1 :: Int)
  VUM.unsafeWrite bs 0 (1 :: Int)
  rep2 n $ \i -> do
    itemA1 <- VUM.unsafeRead as (i - 1)
    itemA2 <- VUM.unsafeRead as (i - 2)
    VUM.unsafeWrite as i ((i - 1) * (itemA1 + itemA2) `mod` 998244353)
    when (i >= 4) $ do
      if even i
        then do
          itemb1 <- VUM.unsafeRead bs (i - 1)
          itemb4 <- VUM.unsafeRead bs (i - 4)
          VUM.unsafeWrite bs i (((i - 1) * itemb1 + 2 * (i - 2) * itemb4) `mod` 998244353)
        else do
          itemb1 <- VUM.unsafeRead bs (i - 1)
          itemb2 <- VUM.unsafeRead bs (i - 2)
          VUM.unsafeWrite bs i (((i - 1) * itemb1 + 2 * (i - 1) * itemb2) `mod` 998244353)
  item0 <- readSTRef ss
  item1 <- VUM.unsafeRead as n
  item2 <- VUM.unsafeRead bs n
  return $ flip mod 998244353 $ item0 - item1 - item1 + item2 + 998244353 + 998244353
0