結果

問題 No.106 素数が嫌い!2
ユーザー はむ吉🐹はむ吉🐹
提出日時 2015-10-29 22:37:19
言語 Haskell
(9.8.2)
結果
AC  
実行時間 224 ms / 5,000 ms
コード長 881 bytes
コンパイル時間 7,580 ms
コンパイル使用メモリ 163,228 KB
実行使用メモリ 32,896 KB
最終ジャッジ日時 2023-10-11 05:24:59
合計ジャッジ時間 7,587 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
21,604 KB
testcase_01 AC 3 ms
7,016 KB
testcase_02 AC 2 ms
7,036 KB
testcase_03 AC 3 ms
7,024 KB
testcase_04 AC 83 ms
21,612 KB
testcase_05 AC 224 ms
32,836 KB
testcase_06 AC 222 ms
32,804 KB
testcase_07 AC 222 ms
32,896 KB
testcase_08 AC 12 ms
11,108 KB
testcase_09 AC 22 ms
11,412 KB
testcase_10 AC 222 ms
32,764 KB
testcase_11 AC 73 ms
21,076 KB
testcase_12 AC 222 ms
32,600 KB
testcase_13 AC 3 ms
6,960 KB
testcase_14 AC 2 ms
6,916 KB
testcase_15 AC 3 ms
7,124 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 #

import Data.Array.Unboxed (UArray, accumArray, elems)
import Control.Applicative ((<$>))

-- | Generates the sequence of prime numbers.
-- Based on <http://qiita.com/little_Haskeller/items/614a3ae20a517c19bb1f this article> .
primes2 :: Integral a => [a]
primes2 = map fromIntegral primes2'
    where
        primes2' = [2, 3, 5] ++ sieve2 5 7 (drop 2 primes2')
        sieve2 m s (p : ps) = [n | n <- ns, gcd m n == 1] ++ sieve2 (m * p) (p * p) ps
            where ns = [x + y | x <- [s, s + 6 .. p * p - 2], y <- [0, 4]]

primesTo :: Int -> [Int]
primesTo n = takeWhile (<= n) primes2

count :: Int -> Int -> Int
count n k = length $ filter (>= k) $ elems tb
    where
        tb = accumArray (+) 0 (2, n) [(p * i, 1) |
            p <- primesTo n, i <- [1 .. div n p]] :: UArray Int Int

main :: IO ()
main = do
    [n, k] <- map read . words <$> getLine
    print $ count n k
0