結果

問題 No.144 エラトステネスのざる
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-04-17 16:06:27
言語 Haskell
(9.8.2)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 745 bytes
コンパイル時間 2,741 ms
コンパイル使用メモリ 174,848 KB
実行使用メモリ 12,160 KB
最終ジャッジ日時 2024-10-04 10:25:39
合計ジャッジ時間 3,428 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 1 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 1 ms
5,248 KB
testcase_07 AC 1 ms
5,248 KB
testcase_08 AC 1 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 93 ms
11,904 KB
testcase_14 AC 99 ms
12,032 KB
testcase_15 AC 95 ms
12,032 KB
testcase_16 AC 98 ms
11,904 KB
testcase_17 AC 91 ms
11,904 KB
testcase_18 AC 91 ms
12,160 KB
testcase_19 AC 78 ms
12,032 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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
import Data.Array.ST
import Data.Array.Unboxed
import Text.Printf

-- Reference: http://sucrose.hatenablog.com/entry/2014/10/10/235805
numDivisorsTable :: Int -> UArray Int Int
numDivisorsTable n = runSTUArray $ do
    t <- newArray (1, n) 0
    forM_ [1 .. n] $ \i -> do
        forM_ [i, 2 * i .. n] $ \j -> do
            writeArray t j . (+ 1) =<< readArray t j
    return t

computeProbability :: Double -> Int -> Double
computeProbability p n = sum $ map prob [2 .. n]
    where
        t = numDivisorsTable n
        prob i = (1 - p) ** (fromIntegral $ subtract 2 $ t ! i)

main :: IO ()
main = do
    [n, p] <- words <$> getLine
    printf "%.12f\n" $ computeProbability (read p) (read n)
0