結果

問題 No.144 エラトステネスのざる
ユーザー Kaito UdagawaKaito Udagawa
提出日時 2015-02-07 12:42:21
言語 Haskell
(9.8.2)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 948 bytes
コンパイル時間 2,596 ms
コンパイル使用メモリ 161,720 KB
実行使用メモリ 31,532 KB
最終ジャッジ日時 2023-09-05 15:01:50
合計ジャッジ時間 4,406 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,576 KB
testcase_01 AC 2 ms
7,512 KB
testcase_02 AC 3 ms
7,604 KB
testcase_03 AC 3 ms
7,468 KB
testcase_04 AC 3 ms
7,444 KB
testcase_05 AC 3 ms
7,444 KB
testcase_06 AC 3 ms
7,680 KB
testcase_07 AC 3 ms
7,908 KB
testcase_08 AC 3 ms
7,728 KB
testcase_09 AC 3 ms
7,768 KB
testcase_10 AC 3 ms
7,740 KB
testcase_11 AC 3 ms
7,840 KB
testcase_12 AC 3 ms
7,612 KB
testcase_13 AC 93 ms
31,084 KB
testcase_14 AC 102 ms
31,424 KB
testcase_15 AC 92 ms
31,296 KB
testcase_16 AC 92 ms
31,400 KB
testcase_17 AC 92 ms
31,464 KB
testcase_18 AC 102 ms
31,532 KB
testcase_19 AC 93 ms
31,316 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 ScopedTypeVariables #-}

import Control.Monad
import Control.Monad.ST
import Data.Array.ST
import Data.Array.Unboxed
import Data.Functor
import Control.Applicative

primes :: Int -> Double -> UArray Int Double
primes n p = runSTUArray (newArray (2, n) 1.0 >>= sieve 2)
  where
    invp = 1 - p :: Double
    sup = n `div` 2 :: Int
    sieve :: forall s. Int -> STUArray s Int Double -> ST s (STUArray s Int Double)
    sieve i arr
      | i > sup = return arr
      | otherwise = erase (i + i) >>= sieve (i + 1)
      where
        erase :: Int -> ST s (STUArray s Int Double)
        erase j
          | j > n = return arr
          | otherwise =
             readArray arr j >>=
               writeArray arr j . (*invp) >>
                 erase (j + i)

main :: IO ()
main =
  words <$> getLine >>= \ (f0:f1:_) ->
    let n = read f0 :: Int
        p = read f1 :: Double
    in (putStrLn . show) (foldr (+) 0 (elems (primes n p)))
0