@@ -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;
@@ -1044,9 +1044,9 @@ macro_rules! impl_folding {
10441044
10451045impl_folding ! {
10461046 impl <M : Modulus > Sum <_> for StaticModInt <M > { fn sum( _) -> _ { _( Self :: raw( 0 ) , Add :: add) } }
1047- impl <M : Modulus > Product <_> for StaticModInt <M > { fn product( _) -> _ { _( Self :: raw( 1 ) , Mul :: mul) } }
1047+ impl <M : Modulus > Product <_> for StaticModInt <M > { fn product( _) -> _ { _( Self :: raw( u32 :: from ( Self :: modulus ( ) > 1 ) ) , Mul :: mul) } }
10481048 impl <I : Id > Sum <_> for DynamicModInt <I > { fn sum( _) -> _ { _( Self :: raw( 0 ) , Add :: add) } }
1049- impl <I : Id > Product <_> for DynamicModInt <I > { fn product( _) -> _ { _( Self :: raw( 1 ) , Mul :: mul) } }
1049+ impl <I : Id > Product <_> for DynamicModInt <I > { fn product( _) -> _ { _( Self :: raw( u32 :: from ( Self :: modulus ( ) > 1 ) ) , Mul :: mul) } }
10501050}
10511051
10521052#[ cfg( test) ]
@@ -1161,6 +1161,19 @@ mod tests {
11611161 assert_eq ! ( expected, c) ;
11621162 }
11631163
1164+ // Corner cases of "modint" when mod = 1
1165+ // https://github.com/rust-lang-ja/ac-library-rs/issues/110
1166+ #[ test]
1167+ fn mod1_corner_case ( ) {
1168+ ModInt :: set_modulus ( 1 ) ; // !!
1169+
1170+ let x: ModInt = std:: iter:: empty :: < ModInt > ( ) . product ( ) ;
1171+ assert_eq ! ( x. val( ) , 0 ) ;
1172+
1173+ let y = ModInt :: new ( 123 ) . pow ( 0 ) ;
1174+ assert_eq ! ( y. val( ) , 0 ) ;
1175+ }
1176+
11641177 // test `2^31 < modulus < 2^32` case
11651178 // https://github.com/rust-lang-ja/ac-library-rs/issues/111
11661179 #[ test]
0 commit comments