Skip to content

Commit 663c8fb

Browse files
authored
Merge pull request #142 from mizar/modint1_fix
fix corner cases of "modint" when mod = 1
2 parents cfc44a3 + d815ff0 commit 663c8fb

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/modint.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ pub trait ModIntBase:
673673
#[inline]
674674
fn pow(self, mut n: u64) -> Self {
675675
let mut x = self;
676-
let mut r = Self::raw(1);
676+
let mut r = Self::raw(u32::from(Self::modulus() > 1));
677677
while n > 0 {
678678
if n & 1 == 1 {
679679
r *= x;
@@ -1043,13 +1043,14 @@ macro_rules! impl_folding {
10431043

10441044
impl_folding! {
10451045
impl<M: Modulus> Sum<_> for StaticModInt<M> { fn sum(_) -> _ { _(Self::raw(0), Add::add) } }
1046-
impl<M: Modulus> Product<_> for StaticModInt<M> { fn product(_) -> _ { _(Self::raw(1), Mul::mul) } }
1046+
impl<M: Modulus> Product<_> for StaticModInt<M> { fn product(_) -> _ { _(Self::raw(u32::from(Self::modulus() > 1)), Mul::mul) } }
10471047
impl<I: Id > Sum<_> for DynamicModInt<I> { fn sum(_) -> _ { _(Self::raw(0), Add::add) } }
1048-
impl<I: Id > Product<_> for DynamicModInt<I> { fn product(_) -> _ { _(Self::raw(1), Mul::mul) } }
1048+
impl<I: Id > Product<_> for DynamicModInt<I> { fn product(_) -> _ { _(Self::raw(u32::from(Self::modulus() > 1)), Mul::mul) } }
10491049
}
10501050

10511051
#[cfg(test)]
10521052
mod tests {
1053+
use crate::modint::ModInt;
10531054
use crate::modint::ModInt1000000007;
10541055

10551056
#[test]
@@ -1157,4 +1158,17 @@ mod tests {
11571158
c /= b;
11581159
assert_eq!(expected, c);
11591160
}
1161+
1162+
// Corner cases of "modint" when mod = 1
1163+
// https://github.com/rust-lang-ja/ac-library-rs/issues/110
1164+
#[test]
1165+
fn mod1_corner_case() {
1166+
ModInt::set_modulus(1); // !!
1167+
1168+
let x: ModInt = std::iter::empty::<ModInt>().product();
1169+
assert_eq!(x.val(), 0);
1170+
1171+
let y = ModInt::new(123).pow(0);
1172+
assert_eq!(y.val(), 0);
1173+
}
11601174
}

0 commit comments

Comments
 (0)