@@ -673,7 +673,7 @@ pub trait ModIntBase:
673
673
#[ inline]
674
674
fn pow ( self , mut n : u64 ) -> Self {
675
675
let mut x = self ;
676
- let mut r = Self :: raw ( 1 ) ;
676
+ let mut r = Self :: raw ( ( Self :: modulus ( ) > 1 ) as u32 ) ;
677
677
while n > 0 {
678
678
if n & 1 == 1 {
679
679
r *= x;
@@ -1043,9 +1043,9 @@ macro_rules! impl_folding {
1043
1043
1044
1044
impl_folding ! {
1045
1045
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( ( Self :: modulus ( ) > 1 ) as u32 ) , Mul :: mul) } }
1047
1047
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( ( Self :: modulus ( ) > 1 ) as u32 ) , Mul :: mul) } }
1049
1049
}
1050
1050
1051
1051
#[ cfg( test) ]
@@ -1160,7 +1160,21 @@ mod tests {
1160
1160
assert_eq ! ( expected, c) ;
1161
1161
}
1162
1162
1163
- // test `2^31 < modulus < 2^32` case.
1163
+ // Corner cases of "modint" when mod = 1
1164
+ // https://github.com/rust-lang-ja/ac-library-rs/issues/110
1165
+ #[ test]
1166
+ fn mod1_corner_case ( ) {
1167
+ ModInt :: set_modulus ( 1 ) ; // !!
1168
+
1169
+ let x: ModInt = std:: iter:: empty :: < ModInt > ( ) . product ( ) ;
1170
+ assert_eq ! ( x. val( ) , 0 ) ;
1171
+
1172
+ let y = ModInt :: new ( 123 ) . pow ( 0 ) ;
1173
+ assert_eq ! ( y. val( ) , 0 ) ;
1174
+ }
1175
+
1176
+ // test `2^31 < modulus < 2^32` case
1177
+ // https://github.com/rust-lang-ja/ac-library-rs/issues/111
1164
1178
#[ test]
1165
1179
fn dynamic_modint_m32 ( ) {
1166
1180
let m = 3221225471 ;
0 commit comments