Skip to content

Commit

Permalink
Improve floor_sum
Browse files Browse the repository at this point in the history
  • Loading branch information
mizar committed Jan 21, 2023
1 parent d2b35ac commit ab0ed85
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,21 +188,19 @@ pub fn crt(r: &[i64], m: &[i64]) -> (i64, i64) {
pub fn floor_sum(n: i64, m: i64, mut a: i64, mut b: i64) -> i64 {
let mut ans = 0;
if a >= m {
ans += (n - 1) * n * (a / m) / 2;
ans += (n - 1) * n / 2 * (a / m);
a %= m;
}
if b >= m {
ans += n * (b / m);
b %= m;
}

let y_max = (a * n + b) / m;
let x_max = y_max * m - b;
if y_max == 0 {
let y_max = a * n + b;
if y_max < m {
return ans;
}
ans += (n - (x_max + a - 1) / a) * y_max;
ans += floor_sum(y_max, a, m, (a - x_max % a) % a);
ans += floor_sum(y_max / m, a, m, y_max % m);
ans
}

Expand Down

0 comments on commit ab0ed85

Please sign in to comment.