結果
| 問題 | No.6 使いものにならないハッシュ | 
| コンテスト | |
| ユーザー |  poapoa | 
| 提出日時 | 2020-08-30 21:57:54 | 
| 言語 | Haskell (9.10.1) | 
| 結果 | 
                                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 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 32 | 
コンパイルメッセージ
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
            
            ソースコード
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
            
            
            
        