Skip to content

Commit c9838af

Browse files
authored
Merge pull request #88 from rsk0315/improve/floor_sum
Improve floor_sum
2 parents d77536e + a596e83 commit c9838af

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

atcoder/math.hpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,17 @@ std::pair<long long, long long> crt(const std::vector<long long>& r,
8282
long long floor_sum(long long n, long long m, long long a, long long b) {
8383
long long ans = 0;
8484
if (a >= m) {
85-
ans += (n - 1) * n * (a / m) / 2;
85+
ans += (n - 1) * n / 2 * (a / m);
8686
a %= m;
8787
}
8888
if (b >= m) {
8989
ans += n * (b / m);
9090
b %= m;
9191
}
9292

93-
long long y_max = (a * n + b) / m, x_max = (y_max * m - b);
94-
if (y_max == 0) return ans;
95-
ans += (n - (x_max + a - 1) / a) * y_max;
96-
ans += floor_sum(y_max, a, m, (a - x_max % a) % a);
93+
long long y_max = a * n + b;
94+
if (y_max < m) return ans;
95+
ans += floor_sum(y_max / m, a, m, y_max % m);
9796
return ans;
9897
}
9998

0 commit comments

Comments
 (0)