結果

問題 No.1273 はじめのζ関数
ユーザー こまる
提出日時 2020-10-31 09:47:05
言語 Haskell
(9.10.1)
結果
TLE  
実行時間 -
コード長 1,164 bytes
コンパイル時間 2,605 ms
コンパイル使用メモリ 181,248 KB
実行使用メモリ 13,440 KB
最終ジャッジ日時 2024-07-22 04:44:31
合計ジャッジ時間 10,014 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 1
other AC * 21 TLE * 1 -- * 18
権限があれば一括ダウンロードができます
コンパイルメッセージ
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

ソースコード

diff #

{-# LANGUAGE BangPatterns       #-}
{-# LANGUAGE NumericUnderscores #-}

import           Control.Monad
import           Control.Monad.ST
import           Data.IORef
import           Data.STRef
import qualified Data.Vector.Fusion.Stream.Monadic as VFSM

main :: IO ()
main = do
  x <- readLn :: IO Int
  if x == 2 then putStrLn "1000000"
  else do
    res <- newIORef (0.0 :: Double)
    rep2 $ \a -> modifyIORef' res (+ zeta a x)
    print . floor . (* 1_000_000) =<< readIORef res

zeta :: Int -> Int -> Double
zeta a x = runST $ do
  res <- newSTRef (a - 1)
  rep (x - 1) $ \_ -> do
    ret <- readSTRef res
    when (ret <= 1_000_000_000_000) $ modifySTRef res (* a)
  d <- readSTRef res
  return (1.0 / fromIntegral d)

stream :: Monad m => Int -> Int -> VFSM.Stream m Int
stream !l !r = VFSM.Stream step l
  where
    step x
      | x < r     = return $ VFSM.Yield x (x + 1)
      | otherwise = return $ VFSM.Done
    {-# INLINE [0] step #-}
{-# INLINE [1] stream #-}

rep :: Monad m => Int -> (Int -> m ()) -> m ()
rep n = flip VFSM.mapM_ (stream 0 n)
{-# INLINE rep #-}

rep2 :: Monad m => (Int -> m ()) -> m ()
rep2 = flip VFSM.mapM_ (stream 2 1_000_000)
0