結果

問題 No.816 Beautiful tuples
ユーザー こまるこまる
提出日時 2020-10-27 19:21:07
言語 Haskell
(9.6.2)
結果
AC  
実行時間 3 ms / 1,500 ms
コード長 2,263 bytes
コンパイル時間 4,027 ms
コンパイル使用メモリ 186,604 KB
実行使用メモリ 7,300 KB
最終ジャッジ日時 2023-09-29 03:21:37
合計ジャッジ時間 3,448 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,960 KB
testcase_01 AC 2 ms
7,012 KB
testcase_02 AC 3 ms
7,212 KB
testcase_03 AC 3 ms
7,152 KB
testcase_04 AC 3 ms
7,184 KB
testcase_05 AC 3 ms
7,252 KB
testcase_06 AC 3 ms
7,148 KB
testcase_07 AC 2 ms
7,188 KB
testcase_08 AC 2 ms
7,152 KB
testcase_09 AC 3 ms
7,300 KB
testcase_10 AC 3 ms
7,192 KB
testcase_11 AC 3 ms
7,192 KB
testcase_12 AC 3 ms
7,192 KB
testcase_13 AC 3 ms
7,152 KB
testcase_14 AC 2 ms
7,004 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 #

{-# LANGUAGE BangPatterns      #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TupleSections     #-}

import           Control.Monad
import           Control.Monad.Fix
import           Control.Monad.State
import           Control.Monad.Cont
import           Data.IORef
import qualified Data.Vector.Fusion.Stream.Monadic as VFSM
import qualified Data.Vector.Unboxed               as VU
import qualified Data.Vector.Unboxed.Mutable       as VUM

main :: IO ()
main = do
  [a, b] <- map (read :: String -> Int) . words <$> getLine
  let cs = divisor (a + b)
  ans   <- newIORef ((-1) :: Int)
  withBreak $ \break -> rep' (VU.length cs) $ \i -> do
    let c = cs VU.! i
    when (a /= c && b /= c && (b + c) `mod` a == 0 && (a + c) `mod` b == 0) $ do
      liftIO (writeIORef ans c)
      break()
  ret <- readIORef ans
  print ret 
  
divisor :: Int -> VU.Vector Int
divisor n = VU.filter (/= 0) $ VU.create $ do
  ret <- VUM.unsafeNew 6720 -- <= 10^12
  rep n $ \i -> do
    when (mod n i == 0) $ do
      flip fix 0 $ \loop !index -> do
        item <- VUM.unsafeRead ret index
        if item == 0
          then VUM.unsafeWrite ret index i
          else loop (index + 1)
      when (i * i /= n) $ do
        flip fix 1 $ \loop !index -> do
          item <-VUM.unsafeRead ret index
          if item == 0
            then VUM.unsafeWrite ret index (div n i)
            else loop (index + 1)
  return ret        

stream :: Monad m => Int -> Int -> VFSM.Stream m Int
stream !l !r = VFSM.Stream step l
  where
    step x
      | 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 1 n)
{-# INLINE rep #-}

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' #-}

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