結果

問題 No.28 末尾最適化
ユーザー tottoripapertottoripaper
提出日時 2014-11-16 22:13:29
言語 Haskell
(9.8.2)
結果
RE  
実行時間 -
コード長 1,018 bytes
コンパイル時間 7,847 ms
コンパイル使用メモリ 161,556 KB
実行使用メモリ 8,396 KB
最終ジャッジ日時 2023-08-30 07:24:39
合計ジャッジ時間 7,450 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,076 KB
testcase_01 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 Control.Applicative
import Control.Monad
import Data.Int
import Data.List (sort)

getInts :: IO [Int64]
getInts = map (fromIntegral . read) . words <$> getLine

factor :: Int64 -> [(Int64, Int64)]
factor x = helper x 2
  where
    helper x y
      | y * y > x = if x /= 1 then [(x, 1)] else []
      | x `mod` y == 0 = let (n, x') = f x y 0
                         in (y, n) : helper x' (y+1)
    f x y a
      | x `mod` y == 0 = f (x`div`y) y (a+1)
      | otherwise = (a, x)
  
main = do
  q <- readLn
  query <- replicateM q getInts
  let
    m = 100000009
    count n x t
      | n `mod` x == 0 = count (n`div`x) x (t+1)
      | otherwise = t
    f x 0 = [x]
    f x n = x : (f x' (n-1))
      where
        x' = 1+(x*x`mod`m + x*12345`mod`m) `mod` m
    g [seed,n,k,b] = minimum $ map h fs
      where
        xs = f seed n
        fs = factor b
        h (p,q) = (`div`q) . sum . take (fromIntegral k) . sort $ map (\x -> count x p 0) xs
   in mapM (print . g) query
0