結果
| 問題 | 
                            No.28 末尾最適化
                             | 
                    
| コンテスト | |
| ユーザー | 
                             tottoripaper
                         | 
                    
| 提出日時 | 2014-11-16 22:17:55 | 
| 言語 | Haskell  (9.10.1)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 2,182 ms / 5,000 ms | 
| コード長 | 1,054 bytes | 
| コンパイル時間 | 5,319 ms | 
| コンパイル使用メモリ | 178,680 KB | 
| 実行使用メモリ | 13,048 KB | 
| 最終ジャッジ日時 | 2024-12-31 21:18:23 | 
| 合計ジャッジ時間 | 7,877 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 2 | 
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default [1 of 2] Compiling Main ( Main.hs, Main.o ) [2 of 2] Linking a.out
ソースコード
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
            
            
            
        
            
tottoripaper