結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 91 ms
12,536 KB
testcase_14 AC 99 ms
12,536 KB
testcase_15 AC 99 ms
12,412 KB
testcase_16 AC 99 ms
12,280 KB
testcase_17 AC 100 ms
12,408 KB
testcase_18 AC 99 ms
12,408 KB
testcase_19 AC 84 ms
12,408 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