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 22, 2023
1 parent 6600bef commit 6428bd4
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 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(u32::from(Self::modulus() > 1));
while n > 0 {
if n & 1 == 1 {
r *= x;
Expand Down Expand Up @@ -1046,9 +1046,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(u32::from(Self::modulus() > 1)), 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(u32::from(Self::modulus() > 1)), Mul::mul) } }
}

#[cfg(test)]
Expand Down Expand Up @@ -1163,6 +1163,19 @@ mod tests {
assert_eq!(expected, c);
}

// 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]
Expand Down

0 comments on commit 6428bd4

Please sign in to comment.