結果

問題 No.28 末尾最適化
ユーザー tottoripapertottoripaper
提出日時 2014-11-16 22:17:55
言語 Haskell
(9.8.2)
結果
AC  
実行時間 2,132 ms / 5,000 ms
コード長 1,054 bytes
コンパイル時間 864 ms
コンパイル使用メモリ 161,800 KB
実行使用メモリ 15,468 KB
最終ジャッジ日時 2023-08-30 07:25:23
合計ジャッジ時間 3,730 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,004 KB
testcase_01 AC 2,132 ms
15,468 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 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)
      | otherwise = 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