Skip to content

Commit

Permalink
Corner cases of "modint" when mod = 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mizar committed Jan 21, 2023
1 parent f16bad2 commit 3761917
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/modint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ pub trait ModIntBase:
#[inline]
fn pow(self, mut n: u64) -> Self {
let mut x = self;
let mut r = Self::raw(1);
let mut r = Self::raw((Self::modulus() > 1) as u32);
while n > 0 {
if n & 1 == 1 {
r *= x;
Expand Down Expand Up @@ -1043,9 +1043,9 @@ macro_rules! impl_folding {

impl_folding! {
impl<M: Modulus> Sum<_> for StaticModInt<M> { fn sum(_) -> _ { _(Self::raw(0), Add::add) } }
impl<M: Modulus> Product<_> for StaticModInt<M> { fn product(_) -> _ { _(Self::raw(1), Mul::mul) } }
impl<M: Modulus> Product<_> for StaticModInt<M> { fn product(_) -> _ { _(Self::raw((Self::modulus() > 1) as u32), Mul::mul) } }
impl<I: Id > Sum<_> for DynamicModInt<I> { fn sum(_) -> _ { _(Self::raw(0), Add::add) } }
impl<I: Id > Product<_> for DynamicModInt<I> { fn product(_) -> _ { _(Self::raw(1), Mul::mul) } }
impl<I: Id > Product<_> for DynamicModInt<I> { fn product(_) -> _ { _(Self::raw((Self::modulus() > 1) as u32), Mul::mul) } }
}

#[cfg(test)]
Expand Down Expand Up @@ -1160,7 +1160,21 @@ mod tests {
assert_eq!(expected, c);
}

// test `2^31 < modulus < 2^32` case.
// Corner cases of "modint" when mod = 1
// https://github.com/rust-lang-ja/ac-library-rs/issues/110
#[test]
fn mod1_corner_case() {
ModInt::set_modulus(1); // !!

let x: ModInt = std::iter::empty::<ModInt>().product();
assert_eq!(x.val(), 0);

let y = ModInt::new(123).pow(0);
assert_eq!(y.val(), 0);
}

// test `2^31 < modulus < 2^32` case
// https://github.com/rust-lang-ja/ac-library-rs/issues/111
#[test]
fn dynamic_modint_m32() {
let m = 3221225471;
Expand Down

0 comments on commit 3761917

Please sign in to comment.