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: Time Limit Exceeded)

-- https://github.com/minoki/my-atcoder-solutions
{-# LANGUAGE TypeApplications #-}
import           Control.Monad
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

floorSum :: Int64 -> Int64 -> Int64 -> Int64 -> Int64
floorSum n m 0 b = n * floor (b % m)
floorSum 0 m a b = 0
floorSum n 1 a b = a * (n * (n - 1) `quot` 2) + n * b
floorSum n m a b
  | m < 0 = error "m must be positive"
  | a >= m || a < 0 = case a `divMod` m of
                        (q, a') -> q * (n * (n - 1) `quot` 2) + floorSum n m a' b
  | b >= m || b < 0 = case b `divMod` m of
                        (q, b') -> q * n + floorSum n m a b'
  | n > m = case n `quotRem` m of
              (q, n') -> (q * n - q * (q + 1) `quot` 2 * m) * a + q * floorSum m m a b + floorSum n' m a b
  | otherwise = -- 0 < a < m, 0 <= b < m, 0 < n <= m
      let t = floor ((toInteger a * (toInteger n - 1) + toInteger b) % toInteger m)
      in n * t + floorSum t a (- m) (b - m)

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] ]

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

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