結果

問題 No.1273 はじめのζ関数
ユーザー こまるこまる
提出日時 2020-10-31 09:33:46
言語 Haskell
(9.8.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,342 bytes
コンパイル時間 564 ms
コンパイル使用メモリ 151,132 KB
最終ジャッジ日時 2023-09-29 10:10:44
合計ジャッジ時間 966 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )

Main.hs:14:7: error: [GHC-88464]
    Variable not in scope: liftIO :: IO () -> ContT () IO a1
    Suggested fix:
      Perhaps use one of these:
        ‘liftM’ (imported from Control.Monad),
        ‘liftA2’ (imported from Prelude),
        ‘liftM2’ (imported from Control.Monad)
   |
14 |       liftIO $ modifyIORef res (* a)
   |       ^^^^^^

Main.hs:15:14: error: [GHC-88464]
    Variable not in scope: liftIO :: IO Int -> ContT () IO a0
    Suggested fix:
      Perhaps use one of these:
        ‘liftM’ (imported from Control.Monad),
        ‘liftA2’ (imported from Prelude),
        ‘liftM2’ (imported from Control.Monad)
   |
15 |       ret <- liftIO $ readIORef res
   |              ^^^^^^

ソースコード

diff #

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

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

zeta :: Int -> Int -> IO Double
zeta a x = do
  res <- newIORef (a - 1)
  withBreak $ \break -> do
    rep (x - 1) $ \_ -> do
      liftIO $ modifyIORef res (* a)
      ret <- liftIO $ readIORef res
      when (ret > 1_000_000_000_000) $ break ()
  d <- readIORef 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)

withBreak :: ((r -> ContT r IO b) -> ContT r IO r) -> IO r
withBreak = flip runContT pure . callCC
{-# INLINE withBreak #-}

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