@@ -37,6 +37,9 @@ namespace cp_algo::math {
3737 return UInt ((ab + m * mod ()) >> bits);
3838 }
3939 }
40+ static UInt m_reduce (Int2 ab) {
41+ return m_reduce (UInt2 (ab + UInt2 (ab < 0 ) * mod () * mod ()));
42+ }
4043 static UInt m_transform (UInt a) {
4144 if (mod () % 2 == 0 ) [[unlikely]] {
4245 return a;
@@ -89,7 +92,7 @@ namespace cp_algo::math {
8992 }
9093
9194 // Only use if you really know what you're doing!
92- UInt modmod () const {return (UInt)8 * mod () * mod ();};
95+ static UInt modmod () {return (UInt)8 * mod () * mod ();};
9396 void add_unsafe (UInt t) {r += t;}
9497 void pseudonormalize () {r = std::min (r, r - modmod ());}
9598 modint const & normalize () {
@@ -100,11 +103,15 @@ namespace cp_algo::math {
100103 }
101104 void setr (UInt rr) {r = m_transform (rr);}
102105 UInt getr () const {
103- UInt res = m_reduce (r );
106+ UInt res = m_reduce (UInt2 (r) );
104107 return std::min (res, res - mod ());
105108 }
106109 void setr_direct (UInt rr) {r = rr;}
107110 UInt getr_direct () const {return r;}
111+ Int rem_direct () const {
112+ UInt R = std::min (r, r - mod ());
113+ return 2 * R > (UInt)mod () ? R - mod () : R;
114+ }
108115 private:
109116 UInt r;
110117 modint& to_modint () {return static_cast <modint&>(*this );}
0 commit comments