結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,684 KB
testcase_01 AC 2 ms
7,540 KB
testcase_02 AC 3 ms
7,624 KB
testcase_03 AC 3 ms
7,480 KB
testcase_04 AC 3 ms
7,484 KB
testcase_05 AC 3 ms
7,440 KB
testcase_06 AC 3 ms
7,544 KB
testcase_07 AC 2 ms
7,784 KB
testcase_08 AC 3 ms
7,840 KB
testcase_09 AC 2 ms
7,860 KB
testcase_10 AC 3 ms
7,660 KB
testcase_11 AC 3 ms
7,900 KB
testcase_12 AC 3 ms
7,712 KB
testcase_13 AC 92 ms
31,260 KB
testcase_14 AC 92 ms
31,528 KB
testcase_15 AC 92 ms
31,504 KB
testcase_16 AC 93 ms
31,376 KB
testcase_17 AC 92 ms
31,332 KB
testcase_18 AC 102 ms
31,696 KB
testcase_19 AC 92 ms
31,340 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.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 :: 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