結果

問題 No.1252 数字根D
ユーザー こまるこまる
提出日時 2020-10-20 12:05:18
言語 Haskell
(9.10.1)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 1,055 bytes
コンパイル時間 5,506 ms
コンパイル使用メモリ 210,176 KB
実行使用メモリ 8,192 KB
最終ジャッジ日時 2024-07-21 08:22:08
合計ジャッジ時間 5,244 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 14
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 #-}
import           Control.Monad.ST
import           Control.Monad.State
import           Data.Char
import           Data.Coerce
import qualified Data.ByteString.Char8             as BSC8
import qualified Data.Vector.Unboxed               as VU

type Parser a = StateT BSC8.ByteString Maybe a
runParser :: Parser a -> BSC8.ByteString -> Maybe (a, BSC8.ByteString)
runParser = runStateT
{-# INLINE runParser #-}
int :: Parser Int
int = coerce $ BSC8.readInt . BSC8.dropWhile isSpace
{-# INLINE int #-}

main :: IO ()
main = do
  q  <- readLn :: IO Int
  ts <- VU.unfoldrN q (runParser $ (,,) <$> int <*> int <*> int) <$> BSC8.getContents
  putStr . unlines . map show . VU.toList $ solver ts
solver :: VU.Vector (Int, Int, Int) -> VU.Vector Int
solver ts = runST $ do
  VU.forM ts $ \(d, a, b) -> do
    if a == 0
      then return $ func b d - func 0 d 
      else return $ func b d - func (a - 1) d
func :: Int -> Int -> Int
func x d =
  let
    (q, r) = divMod x (d - 1)
  in
    d * (d - 1) `div` 2 * q + r * (r + 1) `div` 2
0