Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a4c93cd

Browse files
committed
ACL Practice Contest C - clean up
1 parent 66ba519 commit a4c93cd

File tree

1 file changed

+13
-50
lines changed

1 file changed

+13
-50
lines changed

‎practice2-c/Main.hs‎

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ import Control.Exception (assert)
1616
comb2 :: (Integral a, Bits a) => a -> a
1717
comb2 n = (n `shiftR` 1) * ((n - 1) .|. 1)
1818

19-
prop_comb2 :: Integer -> QC.Property
20-
prop_comb2 n = comb2 n QC.=== n * (n - 1) `quot` 2
21-
22-
prop_floorSum_negate_a :: QC.NonNegative (QC.Small Int64) -> QC.Positive Int64 -> Int64 -> Int64 -> QC.Property
23-
prop_floorSum_negate_a (QC.NonNegative (QC.Small n)) (QC.Positive m) a b =
24-
let does_not_overflow = (\t -> toInteger (minBound :: Int64) <= t && t <= toInteger (maxBound :: Int64)) (toInteger b + toInteger a * (toInteger n - 1))
25-
in does_not_overflow QC.==> floorSum n m (- a) (b + a * (n - 1)) QC.=== floorSum n m a b
26-
2719
-- floorSum n m a b
2820
-- n: non-negative, m: positive
2921
floorSum :: Int64 -> Int64 -> Int64 -> Int64 -> Int64
@@ -33,53 +25,34 @@ floorSum 0 m a b = 0
3325
floorSum n 1 a b = a * comb2 n + n * b
3426
floorSum n m a b
3527
| a < 0 = floorSum n m (- a) (b + a * (n - 1))
36-
{-
28+
{-
3729
| a >= m || a < 0 = case a `divMod` m of
3830
(q, a') -> q * comb2 n + floorSum n m a' b
3931
-}
4032
| let m2 = m `quot` 2
4133
, abs a > m2 = case (a + m2) `divMod` m of
42-
(q, a') ->
43-
q * comb2 n + floorSum n m (a' - m2) b
34+
(q, a') -> q * comb2 n + floorSum n m (a' - m2) b
4435
| b >= m || b < 0 = case b `divMod` m of
4536
(q, b') -> q * n + floorSum n m a b'
4637
| n > m = case n `quotRem` m of
4738
(q, n') -> (q * n - comb2 (q + 1) * m) * a + q * floorSum m m a b + floorSum n' m a b
48-
-- | n < 100 = fromInteger $ floorSum_naive n m a b
49-
-- in -- fromInteger $ floorSum_naive n m a b
50-
-- - n * t - floorSum t (- a) (- m) (- b - m) + floorSum t (- a) (- m) (b - m)
51-
| otherwise = -- 0 < a < m, 0 <= b < m, 0 < n <= m
52-
-- 0 < a < m
53-
-- sum [ fromIntegral $ length [ i | i <- [0..n-1], floor ((toInteger a * toInteger i + toInteger b) % toInteger m) >= k ] | k <- [1..(floor $ (toInteger a * (toInteger n - 1) + toInteger b) % toInteger m)] ]
54-
-- sum [ fromIntegral $ length [ i | i <- [0..n-1], i >= - floor ((- toInteger m * toInteger k + toInteger b) % toInteger a) ] | k <- [1..(floor $ (toInteger a * (toInteger n - 1) + toInteger b) % toInteger m)] ]
55-
-- sum [ n - max 0 (- floor ((- toInteger m * toInteger k + toInteger b - toInteger m) % toInteger a)) | k <- [0..(floor $ (toInteger a * (toInteger n - 1) + toInteger b) % toInteger m) - 1] ]
39+
| otherwise =
40+
-- 0 < a < m, 0 <= b < m, 0 < n <= m
5641
let t = floor ((toInteger a * (toInteger n - 1) + toInteger b) % toInteger m)
5742
in n * t + floorSum t a (- m) (b - m)
58-
-- ceilSum (ceiling $ (a * (n - 1) + b) % m) a m (m - b)
5943

60-
{-
61-
ceilSum :: Int64 -> Int64 -> Int64 -> Int64 -> Int64
62-
ceilSum n m 0 b = n * ceiling (b % m)
63-
ceilSum 0 m a b = 0
64-
ceilSum n 1 a b = a * (n * (n - 1) `quot` 2) + n * b
65-
ceilSum n m a b
66-
| a >= m = case a `quotRem` m of
67-
(q, a') -> q * (n * (n - 1) `quot` 2) + ceilSum n m a' b
68-
| b >= m || b < 0 = case b `divMod` m of
69-
(q, b') -> q * n + ceilSum n m a b'
70-
| n > m = case n `quotRem` m of
71-
(q, n') -> (q * n - q * (q + 1) `quot` 2 * m) * a + q * ceilSum m m a b + ceilSum n' m a b
72-
| n < 100 = fromInteger $ ceilSum_naive n m a b
73-
| otherwise = n * (n - 1) `quot` 2 - floorSum n m (m - a) (- b) -- 0 < a < m, 0 <= b < m, 0 < n <= m
74-
75-
-}
7644
floorSum_naive :: Int64 -> Int64 -> Int64 -> Int64 -> Integer
7745
floorSum_naive n m a b = sum [ floor ((fromIntegral a * fromIntegral i + fromIntegral b) % fromIntegral m) | i <- [0..n-1] ]
7846

79-
{-
80-
ceilSum_naive :: Int64 -> Int64 -> Int64 -> Int64 -> Integer
81-
ceilSum_naive n m a b = sum [ ceiling ((fromIntegral a * fromIntegral i + fromIntegral b) % fromIntegral m) | i <- [0..n-1] ]
82-
-}
47+
main = do
48+
t <- readLn @Int
49+
replicateM_ t $ do
50+
[n,m,a,b] <- map fromIntegral . unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
51+
print $ floorSum n m a b
52+
53+
prop_comb2 :: Integer -> QC.Property
54+
prop_comb2 n = comb2 n QC.=== n * (n - 1) `quot` 2
55+
8356
prop_floorSum :: QC.NonNegative (QC.Small Int64) -> QC.Positive Int64 -> Int64 -> Int64 -> QC.Property
8457
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
8558

@@ -90,13 +63,3 @@ prop_floorSum_r = QC.forAllShrink (QC.choose (1, 10^4)) QC.shrink $ \n -> n >= 1
9063
QC.forAllShrink (QC.choose (0, m - 1)) QC.shrink $ \b ->
9164
QC.within (100 * 1000) $ toInteger (floorSum n m a b) QC.=== floorSum_naive n m a b
9265

93-
{-
94-
prop_ceilSum :: QC.NonNegative (QC.Small Int64) -> QC.Positive Int64 -> Int64 -> Int64 -> QC.Property
95-
prop_ceilSum (QC.NonNegative (QC.Small n)) (QC.Positive m) a b = QC.within (100 * 1000) $ toInteger (ceilSum n m a b) QC.=== ceilSum_naive n m a b
96-
-}
97-
98-
main = do
99-
t <- readLn @Int
100-
replicateM_ t $ do
101-
[n,m,a,b] <- map fromIntegral . unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
102-
print $ floorSum n m a b

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /