結果

問題 No.7 プライムナンバーゲーム
ユーザー こまるこまる
提出日時 2020-11-20 11:48:06
言語 Haskell
(9.6.2)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 4,329 bytes
コンパイル時間 6,176 ms
コンパイル使用メモリ 182,056 KB
実行使用メモリ 7,156 KB
最終ジャッジ日時 2023-07-24 21:42:12
合計ジャッジ時間 6,686 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,076 KB
testcase_01 AC 2 ms
6,972 KB
testcase_02 AC 4 ms
7,012 KB
testcase_03 AC 2 ms
7,052 KB
testcase_04 AC 3 ms
7,044 KB
testcase_05 AC 3 ms
7,132 KB
testcase_06 AC 3 ms
7,044 KB
testcase_07 AC 3 ms
7,152 KB
testcase_08 AC 3 ms
7,072 KB
testcase_09 AC 3 ms
7,064 KB
testcase_10 AC 3 ms
7,020 KB
testcase_11 AC 3 ms
7,120 KB
testcase_12 AC 4 ms
7,156 KB
testcase_13 AC 3 ms
7,136 KB
testcase_14 AC 4 ms
7,140 KB
testcase_15 AC 4 ms
7,108 KB
testcase_16 AC 4 ms
7,064 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          #-}
{-# LANGUAGE BinaryLiterals        #-}
{-# LANGUAGE CPP                   #-}
{-# LANGUAGE DerivingVia           #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE TypeFamilies          #-}

import           Control.Monad
import           Control.Monad.Cont
import           Control.Monad.ST
import           Data.IORef
import qualified Data.Vector.Fusion.Stream.Monadic as VFSM
import qualified Data.Vector.Unboxed.Mutable       as VUM

main :: IO ()
main = do
  n     <- readLn :: IO Int
  prime <- VUM.unsafeNew 10010 :: IO (VUM.IOVector Bool)
  dp    <- VUM.unsafeNew 10010 :: IO (VUM.IOVector Bool)
  p     <- VUM.unsafeNew 100000 :: IO (VUM.IOVector Int)
  ptr   <- newIORef (0 :: Int)
  range 2 n $ \i -> VUM.unsafeWrite prime i True
  forP n $ \i -> do
    pi <- VUM.unsafeRead prime i
    when pi $ forG 2 n (*) i (+) 1 $ \j -> do
      VUM.unsafeWrite prime (i * j) False
  range 2 n $ \i -> do
    pi <- VUM.unsafeRead prime i
    when pi $ do
      pointer <- readIORef ptr
      modifyIORef' ptr succ
      VUM.unsafeWrite p pointer i
  VUM.unsafeWrite dp 0 True
  VUM.unsafeWrite dp 1 True
  range 2 n $ \i -> do
    dpi <- VUM.unsafeRead dp i
    unless dpi $ do
      pSize <- readIORef ptr
      withBreakIO $ \break -> rep pSize $ \j -> do
        pj <- VUM.unsafeRead p j
        when (i + pj > n) $ break ()
        VUM.unsafeWrite dp (i + pj) True
  dpn <- VUM.unsafeRead dp n
  if dpn
    then putStrLn "Win"
    else putStrLn "Lose"

rep :: Monad m => Int -> (Int -> m ()) -> m ()
rep n = flip VFSM.mapM_ (streamG 0 (n - 1) const 0 (+) 1)
{-# INLINE rep #-}

rep' :: Monad m => Int -> (Int -> m ()) -> m ()
rep' n = flip VFSM.mapM_ (streamG 0 n const 0 (+) 1)
{-# INLINE rep' #-}

rep1 :: Monad m => Int -> (Int -> m ()) -> m ()
rep1 n = flip VFSM.mapM_ (streamG 1 (n - 1) const 0 (+) 1)
{-# INLINE rep1 #-}

rep1' :: Monad m => Int -> (Int -> m ()) -> m ()
rep1' n = flip VFSM.mapM_ (streamG 1 n const 0 (+) 1)
{-# INLINE rep1' #-}

rev :: Monad m => Int -> (Int -> m ()) -> m ()
rev n = flip VFSM.mapM_ (streamRG (n - 1) 0 const 0 (-) 1)
{-# INLINE rev #-}

rev' :: Monad m => Int -> (Int -> m ()) -> m ()
rev' n = flip VFSM.mapM_ (streamRG n 0 const 0 (-) 1)
{-# INLINE rev' #-}

rev1 :: Monad m => Int -> (Int -> m ()) -> m ()
rev1 n = flip VFSM.mapM_ (streamRG (n - 1) 1 const 0 (-) 1)
{-# INLINE rev1 #-}

rev1' :: Monad m => Int -> (Int -> m ()) -> m ()
rev1' n = flip VFSM.mapM_ (streamRG n 1 const 0 (-) 1)
{-# INLINE rev1' #-}

range :: Monad m => Int -> Int -> (Int -> m ()) -> m ()
range l r = flip VFSM.mapM_ (streamG l r const 0 (+) 1)
{-# INLINE range #-}

rangeR :: Monad m => Int -> Int -> (Int -> m ()) -> m ()
rangeR r l = flip VFSM.mapM_ (streamRG r l const 0 (-) 1)
{-# INLINE rangeR #-}

forP :: Monad m => Int -> (Int -> m ()) -> m ()
forP p = flip VFSM.mapM_ (streamG 2 p (^) 2 (+) 1)
{-# INLINE forP #-}

forG :: Monad m => Int -> Int -> (Int -> Int -> Int) -> Int -> (Int -> Int -> Int) -> Int -> (Int -> m ()) -> m ()
forG l r f p g d = flip VFSM.mapM_ (streamG l r f p g d)
{-# INLINE forG #-}

forRG :: Monad m => Int -> Int -> (Int -> Int -> Int) -> Int -> (Int -> Int -> Int) -> Int -> (Int -> m ()) -> m ()
forRG r l f p g d = flip VFSM.mapM_ (streamRG r l f p g d)
{-# INLINE forRG #-}

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

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

withBreakIO :: ((r -> ContT r IO b) -> ContT r IO r) -> IO r
withBreakIO = flip runContT pure . callCC
{-# INLINE withBreakIO #-}

withBreakST :: ((r -> ContT r (ST s) b) -> ContT r (ST s) r) -> (ST s) r
withBreakST = flip runContT pure . callCC
{-# INLINE withBreakST #-}
0