-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: relax the constraints of floor_sum #99
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,25 +185,22 @@ pub fn crt(r: &[i64], m: &[i64]) -> (i64, i64) { | |
/// | ||
/// assert_eq!(math::floor_sum(6, 5, 4, 3), 13); | ||
/// ``` | ||
#[allow(clippy::many_single_char_names)] | ||
pub fn floor_sum(n: i64, m: i64, mut a: i64, mut b: i64) -> i64 { | ||
assert!((0..1i64 << 32).contains(&n)); | ||
assert!((1..1i64 << 32).contains(&m)); | ||
let mut ans = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since overflows occur in C++ without special handling, they use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's wrap the variables with |
||
if a >= m { | ||
ans += (n - 1) * n * (a / m) / 2; | ||
a %= m; | ||
} | ||
if b >= m { | ||
ans += n * (b / m); | ||
b %= m; | ||
if a < 0 { | ||
let a2 = internal_math::safe_mod(a, m); | ||
ans -= n * (n - 1) / 2 * ((a2 - a) / m); | ||
a = a2; | ||
} | ||
|
||
let y_max = (a * n + b) / m; | ||
let x_max = y_max * m - b; | ||
if y_max == 0 { | ||
return ans; | ||
if b < 0 { | ||
let b2 = internal_math::safe_mod(b, m); | ||
ans -= n * ((b2 - b) / m); | ||
b = b2; | ||
} | ||
ans += (n - (x_max + a - 1) / a) * y_max; | ||
ans += floor_sum(y_max, a, m, (a - x_max % a) % a); | ||
ans | ||
ans + internal_math::floor_sum_unsigned(n as u64, m as u64, a as u64, b as u64) as i64 | ||
} | ||
|
||
#[cfg(test)] | ||
|
@@ -306,5 +303,24 @@ mod tests { | |
499_999_999_500_000_000 | ||
); | ||
assert_eq!(floor_sum(332955, 5590132, 2231, 999423), 22014575); | ||
for n in 0..20 { | ||
for m in 1..20 { | ||
for a in -20..20 { | ||
for b in -20..20 { | ||
assert_eq!(floor_sum(n, m, a, b), floor_sum_naive(n, m, a, b)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[allow(clippy::many_single_char_names)] | ||
fn floor_sum_naive(n: i64, m: i64, a: i64, b: i64) -> i64 { | ||
let mut ans = 0; | ||
for i in 0..n { | ||
let z = a * i + b; | ||
ans += (z - internal_math::safe_mod(z, m)) / m; | ||
} | ||
ans | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let this function take
Wrapping<u64>
instead ofu64
to avoid complication; it's an internal function anyway.