結果

問題 No.407 鴨等素数間隔列の数え上げ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-07-09 17:21:38
言語 Haskell
(9.8.2)
結果
TLE  
実行時間 -
コード長 1,643 bytes
コンパイル時間 8,724 ms
コンパイル使用メモリ 176,384 KB
実行使用メモリ 48,256 KB
最終ジャッジ日時 2024-04-21 11:52:05
合計ジャッジ時間 7,659 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
10,624 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 39 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 146 ms
5,376 KB
testcase_06 AC 72 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 129 ms
7,936 KB
testcase_20 AC 490 ms
16,000 KB
testcase_21 AC 7 ms
5,376 KB
testcase_22 AC 9 ms
5,376 KB
testcase_23 AC 33 ms
5,376 KB
testcase_24 AC 35 ms
5,376 KB
testcase_25 AC 937 ms
23,680 KB
testcase_26 AC 64 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 27 ms
5,376 KB
testcase_29 AC 75 ms
5,376 KB
testcase_30 AC 7 ms
5,376 KB
testcase_31 AC 66 ms
5,376 KB
testcase_32 AC 420 ms
13,696 KB
testcase_33 TLE -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 Data.Int

-- テストケース検証用のassertを外した場合

-- 素数の無限リスト
-- 参考: http://qiita.com/little_Haskeller/items/614a3ae20a517c19bb1f
primes :: Integral a => [a]
primes = [2, 3, 5] ++ sieve 5 7 (drop 2 primes)
    where
        sieve m s (p : ps) = [n | n <- ns, gcd m n == 1] ++ sieve (m * p) (p * p) ps
            where ns = [x + y | x <- [s, s + 6 .. p * p - 2], y <- [0, 4]]
        sieve _ _ [] = error "Unexpected"

-- 上記の素数リストをもとに、与えられた数の素数判定を行う
-- 参考: https://wiki.haskell.org/Testing_primality
isPrime :: Integral a => a -> Bool
isPrime n = n > 1 && foldr (\p r -> p * p > n || (mod n p /= 0 && r)) True primes

-- 素数計数関数の表をつくる
-- 添え字と内容をInt64に決め打ちしている
pcfTable :: Int64 -> UArray Int64 Int64
pcfTable n = runSTUArray $ do
    t <- newArray_ (0, n)
    writeArray t 0 0
    forM_ [1 .. n] $ \i -> do
        pre <- readArray t $ i - 1
        writeArray t i $ if isPrime i then pre + 1 else pre
    return t

-- 鴨等素数間隔列の数を数える
countSeqs :: Int64 -> Int64 -> Int64
countSeqs n l
    | x0Max < 0 = 0
    | otherwise = sum [pcf ! dMax x0 | x0 <- [0 .. x0Max]]
    where
        -- x0がとれる最大値
        x0Max = l - n + 1
        -- 各x0に対して、dの最大値が決まる
        dMax x0 = (l - x0) `div` (n - 1)
        pcf = pcfTable $ dMax 0

main :: IO ()
main = do
    [n, l] <- fmap read . words <$> getLine
    print $ countSeqs n l
0