結果

問題 No.6 使いものにならないハッシュ
ユーザー poapoapoapoa
提出日時 2020-08-30 21:57:54
言語 Haskell
(9.8.2)
結果
AC  
実行時間 13 ms / 5,000 ms
コード長 1,389 bytes
コンパイル時間 7,319 ms
コンパイル使用メモリ 192,512 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2024-09-16 16:58:41
合計ジャッジ時間 8,544 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 13 ms
7,808 KB
testcase_03 AC 4 ms
6,528 KB
testcase_04 AC 5 ms
7,680 KB
testcase_05 AC 6 ms
7,808 KB
testcase_06 AC 8 ms
7,936 KB
testcase_07 AC 7 ms
7,936 KB
testcase_08 AC 8 ms
7,936 KB
testcase_09 AC 6 ms
7,296 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 5 ms
7,424 KB
testcase_12 AC 10 ms
7,936 KB
testcase_13 AC 4 ms
6,272 KB
testcase_14 AC 5 ms
7,168 KB
testcase_15 AC 9 ms
7,936 KB
testcase_16 AC 8 ms
7,808 KB
testcase_17 AC 8 ms
7,808 KB
testcase_18 AC 10 ms
7,808 KB
testcase_19 AC 8 ms
7,936 KB
testcase_20 AC 8 ms
7,808 KB
testcase_21 AC 3 ms
5,760 KB
testcase_22 AC 9 ms
7,936 KB
testcase_23 AC 9 ms
7,808 KB
testcase_24 AC 8 ms
7,936 KB
testcase_25 AC 7 ms
7,936 KB
testcase_26 AC 10 ms
7,808 KB
testcase_27 AC 8 ms
7,936 KB
testcase_28 AC 6 ms
7,936 KB
testcase_29 AC 9 ms
7,936 KB
testcase_30 AC 9 ms
7,680 KB
testcase_31 AC 8 ms
8,064 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 )

Main.hs:31:16: warning: [GHC-63394] [-Wx-partial]
    In the use of ‘head’
    (imported from Prelude, but defined in GHC.List):
    "This is a partial function, it throws an error on empty lists. Use pattern matching or Data.List.uncons instead. Consider refactoring to use Data.List.NonEmpty."
   |
31 | func1 acc [] = head acc
   |                ^^^^

Main.hs:32:51: warning: [GHC-63394] [-Wx-partial]
    In the use of ‘tail’
    (imported from Prelude, but defined in GHC.List):
    "This is a partial function, it throws an error on empty lists. Replace it with drop 1, or use pattern matching or Data.List.uncons instead. Consider refactoring to use Data.List.NonEmpty."
   |
32 | func1 acc hps = func1 (func4 acc (func3 [] hps)) (tail hps)
   |                                                   ^^^^
[2 of 2] Linking a.out

ソースコード

diff #

import Data.Char
import Data.Maybe
import Control.Monad
import Control.Monad.ST
import Data.Array.ST
import Data.Array.Unboxed

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

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

main :: IO ()
main = do
  k <- readLn :: IO Int
  n <- readLn :: IO Int
  let ps = dropWhile (<k) $ primesToUA n
  print (func1 [] (map func2 ps))

func1 :: [Int] -> [(Int, Int)] -> Int
func1 acc [] = head acc
func1 acc hps = func1 (func4 acc (func3 [] hps)) (tail hps)

func2 :: Int -> (Int, Int)
func2 p = (until (<10) (sum . map digitToInt . show) p, p) 

func3 :: [(Int, Int)] -> [(Int, Int)] -> [Int]
func3 acc [] = map snd (reverse acc)
func3 acc ((h,p):hps)
  | isJust $ lookup h acc = map snd (reverse acc)
  | otherwise = func3 ((h,p):acc) hps

func4 :: [Int] -> [Int] -> [Int]
func4 hps1 hps2
  | length hps1 > length hps2 = hps1
  | length hps1 < length hps2 = hps2
  | otherwise = max hps1 hps2
0