結果

問題 No.106 素数が嫌い!2
ユーザー ducktail
提出日時 2018-07-23 13:50:09
言語 Haskell
(9.10.1)
結果
AC  
実行時間 72 ms / 5,000 ms
コード長 774 bytes
コンパイル時間 9,432 ms
コンパイル使用メモリ 195,712 KB
実行使用メモリ 23,552 KB
最終ジャッジ日時 2024-12-26 15:01:47
合計ジャッジ時間 8,288 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 13
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 #

import Control.Applicative ((<$>))
import Control.Monad (when, forM_, foldM)
import Data.Vector.Unboxed.Mutable (IOVector, STVector)
import qualified Data.Vector.Unboxed.Mutable as VM

main :: IO ()
main = solve >>= print

solve :: IO Int
solve = do
  [n, k] <- map read <$> words <$> getLine :: IO [Int]
  pt <- VM.replicate (n+1) 0 :: IO (IOVector Int)
  forM_ [2 .. n] $ \i -> do
    x <- VM.read pt i
    when (x == 0) $ do
      VM.write pt i 1
      forM_ [2*i, 3*i .. n] $ \j -> do
        modify pt j (+1)
  foldM (f k pt) 0 [2 .. n]
  where f k pt c i = do
          x <- VM.read pt i
          if x >= k then return (c+1)
            else return c
  
modify :: IOVector Int -> Int -> (Int -> Int) -> IO ()
modify v i f = do
  x <- VM.read v i
  VM.write v i (f x)
0