結果

問題 No.12 限定された素数
ユーザー aimyaimy
提出日時 2017-06-12 15:48:09
言語 Haskell
(9.8.2)
結果
AC  
実行時間 2,714 ms / 5,000 ms
コード長 1,142 bytes
コンパイル時間 14,691 ms
コンパイル使用メモリ 181,324 KB
実行使用メモリ 107,904 KB
最終ジャッジ日時 2023-08-16 00:45:40
合計ジャッジ時間 77,625 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,362 ms
63,876 KB
testcase_01 AC 2,473 ms
87,752 KB
testcase_02 AC 2,352 ms
60,724 KB
testcase_03 AC 2,714 ms
107,904 KB
testcase_04 AC 2,424 ms
88,004 KB
testcase_05 AC 2,483 ms
87,620 KB
testcase_06 AC 2,504 ms
88,644 KB
testcase_07 AC 2,524 ms
88,648 KB
testcase_08 AC 2,432 ms
66,056 KB
testcase_09 AC 2,453 ms
87,996 KB
testcase_10 AC 2,474 ms
85,340 KB
testcase_11 AC 2,573 ms
88,680 KB
testcase_12 AC 2,524 ms
87,644 KB
testcase_13 AC 2,484 ms
88,640 KB
testcase_14 AC 2,422 ms
68,168 KB
testcase_15 AC 2,473 ms
88,612 KB
testcase_16 AC 2,554 ms
89,664 KB
testcase_17 AC 2,332 ms
60,896 KB
testcase_18 AC 2,394 ms
74,200 KB
testcase_19 AC 2,394 ms
74,304 KB
testcase_20 AC 2,443 ms
87,832 KB
testcase_21 AC 2,454 ms
87,016 KB
testcase_22 AC 2,393 ms
74,200 KB
testcase_23 AC 2,394 ms
74,348 KB
testcase_24 AC 2,361 ms
63,868 KB
testcase_25 AC 2,474 ms
81,448 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.List
import Control.Monad
import qualified Data.Set as S

primes :: [Int]
primes = 2 : 3 : [x | i<-[1..], j<-[-1,1], let x = 6*i+j, isPrime x]
 where isPrime n = null [i | i <- takeWhile (\x -> x^2 <= n) primes, mod n i == 0]

main = do
 getLine
 as <- map read . words <$> getLine
 putStrLn $ maybe "-1" show (rprime as)

rprime :: [Int] -> Maybe Int
rprime as = guard ((not.null) ds) >> maximum ds
 where
  sas = S.fromList (concatMap show as)
  ps = takeWhile (<= 5000000) primes
  sps = S.fromList ps
  ips = zip [0..] ps
  pcands = filter (S.null . flip S.difference sas . S.fromList . show . snd) ips
  punits = filter ((== sas) . S.fromList . concat . map show) (groupBySeq pcands)
  ds = map (diffs sps) punits

groupBySeq :: [(Int, Int)] -> [[Int]]
groupBySeq [] = []
groupBySeq xs@((i,p):ips) = g : groupBySeq (drop (length g) xs)
 where g = map (snd.snd) $ takeWhile (\(i,(j,_)) -> i==j) (zip [i..] xs)

diffs :: S.Set Int -> [Int] -> Maybe Int
diffs sps ps = do
  plb <- (succ <$> S.lookupLT (minimum ps) sps) `mplus` (Just 1)
  phb <- (pred <$> S.lookupGT (maximum ps) sps) `mplus` (Just 5000000)
  return (phb - plb)
0