結果

問題 No.144 エラトステネスのざる
ユーザー Kaito UdagawaKaito Udagawa
提出日時 2015-02-07 12:06:43
言語 Haskell
(9.8.2)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 909 bytes
コンパイル時間 9,515 ms
コンパイル使用メモリ 165,252 KB
実行使用メモリ 31,548 KB
最終ジャッジ日時 2023-09-05 14:59:21
合計ジャッジ時間 4,901 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,752 KB
testcase_01 AC 2 ms
7,668 KB
testcase_02 AC 3 ms
7,592 KB
testcase_03 AC 2 ms
7,580 KB
testcase_04 AC 2 ms
7,572 KB
testcase_05 AC 2 ms
7,592 KB
testcase_06 AC 2 ms
7,588 KB
testcase_07 AC 3 ms
7,800 KB
testcase_08 AC 3 ms
7,732 KB
testcase_09 AC 3 ms
7,708 KB
testcase_10 AC 3 ms
7,624 KB
testcase_11 AC 3 ms
7,988 KB
testcase_12 AC 3 ms
7,692 KB
testcase_13 AC 92 ms
31,200 KB
testcase_14 AC 93 ms
31,416 KB
testcase_15 AC 92 ms
31,444 KB
testcase_16 AC 92 ms
31,500 KB
testcase_17 AC 93 ms
31,356 KB
testcase_18 AC 92 ms
31,548 KB
testcase_19 AC 92 ms
31,416 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

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