Skip to content

Commit a83f313

Browse files
authored
Merge pull request #7 from AconCavy/patch/floor-sum
Update FloorSum
2 parents a3696a5 + 6de139c commit a83f313

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/AlgorithmSharp/Mathematics.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static long FloorSum(long n, long m, long a, long b)
4848
{
4949
if (a >= m)
5050
{
51-
ret += (n - 1) * n * (a / m) / 2;
51+
ret += (n - 1) * n / 2 * (a / m);
5252
a %= m;
5353
}
5454

@@ -58,11 +58,9 @@ public static long FloorSum(long n, long m, long a, long b)
5858
b %= m;
5959
}
6060

61-
var yMax = (a * n + b) / m;
62-
var xMax = yMax * m - b;
63-
if (yMax == 0) return ret;
64-
ret += (n - (xMax + a - 1) / a) * yMax;
65-
(n, m, a, b) = (yMax, a, m, (a - xMax % a) % a);
61+
var yMax = a * n + b;
62+
if (yMax < m) return ret;
63+
(n, m, a, b) = (yMax / m, a, m, yMax % m);
6664
}
6765
}
6866

0 commit comments

Comments
 (0)