@@ -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 ( u32 :: from ( Self :: modulus ( ) > 1 ) ) ;
677
677
while n > 0 {
678
678
if n & 1 == 1 {
679
679
r *= x;
@@ -1046,9 +1046,9 @@ macro_rules! impl_folding {
1046
1046
1047
1047
impl_folding ! {
1048
1048
impl <M : Modulus > Sum <_> for StaticModInt <M > { fn sum( _) -> _ { _( Self :: raw( 0 ) , Add :: add) } }
1049
- impl <M : Modulus > Product <_> for StaticModInt <M > { fn product( _) -> _ { _( Self :: raw( 1 ) , Mul :: mul) } }
1049
+ impl <M : Modulus > Product <_> for StaticModInt <M > { fn product( _) -> _ { _( Self :: raw( u32 :: from ( Self :: modulus ( ) > 1 ) ) , Mul :: mul) } }
1050
1050
impl <I : Id > Sum <_> for DynamicModInt <I > { fn sum( _) -> _ { _( Self :: raw( 0 ) , Add :: add) } }
1051
- impl <I : Id > Product <_> for DynamicModInt <I > { fn product( _) -> _ { _( Self :: raw( 1 ) , Mul :: mul) } }
1051
+ impl <I : Id > Product <_> for DynamicModInt <I > { fn product( _) -> _ { _( Self :: raw( u32 :: from ( Self :: modulus ( ) > 1 ) ) , Mul :: mul) } }
1052
1052
}
1053
1053
1054
1054
#[ cfg( test) ]
@@ -1163,6 +1163,19 @@ mod tests {
1163
1163
assert_eq ! ( expected, c) ;
1164
1164
}
1165
1165
1166
+ // Corner cases of "modint" when mod = 1
1167
+ // https://github.com/rust-lang-ja/ac-library-rs/issues/110
1168
+ #[ test]
1169
+ fn mod1_corner_case ( ) {
1170
+ ModInt :: set_modulus ( 1 ) ; // !!
1171
+
1172
+ let x: ModInt = std:: iter:: empty :: < ModInt > ( ) . product ( ) ;
1173
+ assert_eq ! ( x. val( ) , 0 ) ;
1174
+
1175
+ let y = ModInt :: new ( 123 ) . pow ( 0 ) ;
1176
+ assert_eq ! ( y. val( ) , 0 ) ;
1177
+ }
1178
+
1166
1179
// test `2^31 < modulus < 2^32` case
1167
1180
// https://github.com/rust-lang-ja/ac-library-rs/issues/111
1168
1181
#[ test]
0 commit comments