結果

問題 No.144 エラトステネスのざる
ユーザー Kaito UdagawaKaito Udagawa
提出日時 2015-02-07 11:19:56
言語 Haskell
(9.8.2)
結果
WA  
実行時間 -
コード長 681 bytes
コンパイル時間 1,225 ms
コンパイル使用メモリ 161,740 KB
実行使用メモリ 31,836 KB
最終ジャッジ日時 2023-09-05 14:56:25
合計ジャッジ時間 3,114 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 3 ms
7,536 KB
testcase_04 AC 3 ms
7,496 KB
testcase_05 AC 3 ms
7,720 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 #

import Data.Array.ST
import Data.Array.Unboxed
import Data.Functor
import Debug.Trace

primes :: Int -> Double -> UArray Int Double
primes n p = runSTUArray (newArray (2, n) 1.0 >>= sieve 2)
  where
    sieve i arr
      | i >= (n `div` 2) = return arr
      | otherwise = erase (i * 2 - 1) >>= sieve (i + 1)
      where
        erase j
          | j >= (n `div` 2) = return arr
          | otherwise =
            readArray arr j >>= \ b ->
              writeArray arr j (p * b) >>
                erase (j + i - 1)

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