結果

問題 No.816 Beautiful tuples
ユーザー こまる
提出日時 2020-10-27 19:04:21
言語 Haskell
(9.10.1)
結果
WA  
実行時間 -
コード長 1,639 bytes
コンパイル時間 2,061 ms
コンパイル使用メモリ 183,132 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-21 21:58:24
合計ジャッジ時間 2,676 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 WA * 8
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 FlexibleInstances #-}
{-# LANGUAGE TupleSections     #-}

import           Control.Monad
import           Control.Monad.Fix
import           Data.Bits
import qualified Data.Foldable                     as F
import           Data.Word
import           Unsafe.Coerce
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
    c  = 3 * a
    cs = divisor (a + b)
  case VU.elemIndex c cs of
    Nothing -> putStrLn "-1"
    Just i  -> print i
  
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 #-}
0