結果

問題 No.36 素数が嫌い!
ユーザー poapoapoapoa
提出日時 2020-08-30 23:23:09
言語 Haskell
(9.8.2)
結果
WA  
実行時間 -
コード長 1,057 bytes
コンパイル時間 1,786 ms
コンパイル使用メモリ 177,920 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-04-27 13:43:11
合計ジャッジ時間 4,908 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 57 ms
11,264 KB
testcase_02 AC 74 ms
11,520 KB
testcase_03 AC 72 ms
11,264 KB
testcase_04 AC 69 ms
11,264 KB
testcase_05 AC 71 ms
11,392 KB
testcase_06 AC 71 ms
11,264 KB
testcase_07 AC 75 ms
11,520 KB
testcase_08 AC 32 ms
6,940 KB
testcase_09 WA -
testcase_10 AC 68 ms
11,264 KB
testcase_11 AC 69 ms
11,392 KB
testcase_12 AC 71 ms
11,392 KB
testcase_13 AC 76 ms
11,520 KB
testcase_14 WA -
testcase_15 AC 34 ms
6,944 KB
testcase_16 AC 73 ms
11,520 KB
testcase_17 AC 74 ms
11,264 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 45 ms
11,136 KB
testcase_25 WA -
testcase_26 AC 72 ms
11,264 KB
testcase_27 AC 76 ms
11,264 KB
testcase_28 AC 77 ms
11,264 KB
testcase_29 AC 70 ms
11,392 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.Monad
import           Control.Monad.ST
import           Data.Bool
import qualified Data.Array.ST                 as ArrST
import qualified Data.Array.Unboxed            as ArrU

sieveUA :: Int -> ArrU.UArray Int Bool
sieveUA top = ArrST.runSTUArray $ do
    let m = (top-1) `div` 2
        r = floor . sqrt $ fromIntegral top + 1
    sieve <- ArrST.newArray (1,m) True
    forM_ [1..r `div` 2] $ \i -> do
      isPrime <- ArrST.readArray sieve i
      when isPrime $ do
        forM_ [2*i*(i+1), 2*i*(i+2)+1..m] $ \j -> do
          ArrST.writeArray sieve j False
    return sieve

primesToUA :: Int -> [Int]
primesToUA top = 2 : [i*2+1 | (i,True) <- ArrU.assocs $ sieveUA top]

main :: IO ()
main = readLn >>= putStrLn . solver


solver :: Int -> String
solver n = bool "NO" "YES" $ func1 n

func1 :: Int -> Bool
func1 n = iter n 0 ps
  where
    ps = primesToUA 10000000
    iter _ p []     = p >= 3
    iter i j (l:ls)
      | j >= 3         = True
      | i `mod` l == 0 = iter i (j + 1) ls
      | otherwise      = iter i j ls
0