Problem:
Score : 100 points

Problem Statement

In this problem, you should process T testcases.

For each testcase, you are given four integers N, M, A, B.

Calculate \sum_{i = 0}^{N - 1} floor((A \times i + B) / M).

Constraints

1 \leq T \leq 100,000

1 \leq N, M \leq 10^9

0 \leq A, B < M

Input

Input is given from Standard Input in the following format:

T
N_0 M_0 A_0 B_0
N_1 M_1 A_1 B_1
:
N_{T - 1} M_{T - 1} A_{T - 1} B_{T - 1}

Output

Print the answer for each testcase.

Sample Input 1

5
4 10 6 3
6 5 4 3
1 1 0 0
31415 92653 58979 32384
1000000000 1000000000 999999999 999999999

Sample Output 1

3
13
0
314095480
499999999500000000

Solution in Haskell (GHC 8.8.3) (judge verdict: Accepted)

-- https://github.com/minoki/my-atcoder-solutions
{-# LANGUAGE BangPatterns     #-}
{-# LANGUAGE TypeApplications #-}
import           Control.Exception     (assert)
import           Control.Monad
import           Data.Bits
import qualified Data.ByteString.Char8 as BS
import           Data.Char             (isSpace)
import           Data.Int              (Int64)
import           Data.List             (unfoldr)
import           Data.Ratio
import qualified Test.QuickCheck       as QC

-- comb2 n = n * (n - 1) `quot` 2 without undue overflow
-- n even: comb2 n = (n `quot` 2) * (n - 1)
-- n odd: comb2 n = n * (n `quot` 2)
comb2 :: (Integral a, Bits a) => a -> a
comb2 n = (n `shiftR` 1) * ((n - 1) .|. 1)

-- floorSum n m a b
-- Assumptions:
-- * n: non-negative, m: positive
floorSum :: Int64 -> Int64 -> Int64 -> Int64 -> Int64
floorSum !n !m !a !b
  | assert (n >= 0 && m > 0) False = undefined
  | a < 0 = floorSum n m (- a) (b + a * (n - 1))
  | m < n = case n `quotRem` m of
              (q, n') -> (q * n - comb2 (q + 1) * m) * a +
                         q * floorSum_positive 0 m m a b +
                         floorSum_positive 0 n' m a b
  | otherwise = floorSum_positive 0 n m a b
  where
    -- Invariants:
    -- * n: non-negative, m: positive, a: non-negative
    -- * 0 <= n <= m
    floorSum_positive :: Int64 -> Int64 -> Int64 -> Int64 -> Int64 -> Int64
    floorSum_positive !acc !n !m !a !b
      | a == 0 = acc + n * (b `div` m)
      | n == 0 = acc
      | m == 1 = acc + a * comb2 n + b * n
      | let m2 = m `quot` 2, a > m2 = case (a + m2) `quotRem` m of
              (q, a') -> let (a'',b'') = if a' < m2 then
                                           (m2 - a', b - (m2 - a') * (n - 1))
                                         else
                                           (a' - m2, b)
                         in floorSum_positive (acc + q * comb2 n) n m a'' b''
      | otherwise =
          let (q, b') = b `divMod` m
              t = (a * (n - 1) + b') `div` m
              -- t <= (m * (m - 1) + m) `div` m = m
          in floorSum_positive (acc + n * (q + t)) t a m (b' - m * t)

floorSum_naive :: Int64 -> Int64 -> Int64 -> Int64 -> Integer
floorSum_naive n m a b = sum [ floor ((fromIntegral a * fromIntegral i + fromIntegral b) % fromIntegral m) | i <- [0..n-1] ]

main = do
  t <- readLn @Int
  replicateM_ t $ do
    [n,m,a,b] <- map fromIntegral . unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
    print $ floorSum n m a b

prop_comb2 :: Integer -> QC.Property
prop_comb2 n = comb2 n QC.=== n * (n - 1) `quot` 2

prop_floorSum :: QC.NonNegative (QC.Small Int64) -> QC.Positive Int64 -> Int64 -> Int64 -> QC.Property
prop_floorSum (QC.NonNegative (QC.Small n)) (QC.Positive m) a b = QC.within (100 * 1000) $ toInteger (floorSum n m a b) QC.=== floorSum_naive n m a b

prop_floorSum_r :: QC.Property
prop_floorSum_r = QC.forAllShrink (QC.choose (1, 10^4)) QC.shrink $ \n -> n >= 1 QC.==>
  QC.forAllShrink (QC.choose (1, 10^9)) QC.shrink $ \m -> m >= 1 QC.==>
  QC.forAllShrink (QC.choose (0, m - 1)) QC.shrink $ \a ->
  QC.forAllShrink (QC.choose (0, m - 1)) QC.shrink $ \b ->
  QC.within (100 * 1000) $ toInteger (floorSum n m a b) QC.=== floorSum_naive n m a b