結果

問題 No.1252 数字根D
コンテスト
ユーザー こまる
提出日時 2020-10-20 12:05:18
言語 Haskell
(9.14.1)
コンパイル:
ghc -rtsopts -with-rtsopts=-K1G -o a.out -O2 _filename_
実行:
./a.out
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,055 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 11,732 ms
コンパイル使用メモリ 232,244 KB
実行使用メモリ 10,496 KB
最終ジャッジ日時 2026-03-04 16:18:03
合計ジャッジ時間 12,110 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 14
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.14.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #
raw source code

{-# 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