@@ -16,35 +16,36 @@ namespace cp_algo::math {
1616 template <typename modint, typename _Int>
1717 struct modint_base {
1818 using Int = _Int;
19- using Uint = std::make_unsigned_t <Int>;
19+ using UInt = std::make_unsigned_t <Int>;
2020 static constexpr size_t bits = sizeof (Int) * 8 ;
21- using Int2 = std::conditional_t <bits <= 32 , uint64_t , __uint128_t >;
21+ using Int2 = std::conditional_t <bits <= 32 , int64_t , __int128_t >;
22+ using UInt2 = std::conditional_t <bits <= 32 , uint64_t , __uint128_t >;
2223 static Int mod () {
2324 return modint::mod ();
2425 }
25- static Uint imod () {
26+ static UInt imod () {
2627 return modint::imod ();
2728 }
28- static Int2 pw128 () {
29+ static UInt2 pw128 () {
2930 return modint::pw128 ();
3031 }
31- static Uint m_reduce (Int2 ab) {
32+ static UInt m_reduce (UInt2 ab) {
3233 if (mod () % 2 == 0 ) [[unlikely]] {
3334 return ab % mod ();
3435 } else {
35- Uint m = ab * imod ();
36- return (ab + (Int2 )m * mod ()) >> bits;
36+ UInt m = ab * imod ();
37+ return (ab + (UInt2 )m * mod ()) >> bits;
3738 }
3839 }
39- static Uint m_transform (Uint a) {
40+ static UInt m_transform (UInt a) {
4041 if (mod () % 2 == 0 ) [[unlikely]] {
4142 return a;
4243 } else {
4344 return m_reduce (a * pw128 ());
4445 }
4546 }
4647 modint_base (): r(0 ) {}
47- modint_base (Int rr): r(rr % mod()) {
48+ modint_base (Int2 rr): r(rr % mod()) {
4849 r = std::min (r, r + mod ());
4950 r = m_transform (r);
5051 }
@@ -60,7 +61,7 @@ namespace cp_algo::math {
6061 return to_modint () *= t.inv ();
6162 }
6263 modint& operator *= (const modint &t) {
63- r = m_reduce ((Int2 )r * t.r );
64+ r = m_reduce ((UInt2 )r * t.r );
6465 return to_modint ();
6566 }
6667 modint& operator += (const modint &t) {
@@ -83,37 +84,37 @@ namespace cp_algo::math {
8384 auto operator < (const modint_base &t) const {return getr () < t.getr ();}
8485 auto operator > (const modint_base &t) const {return getr () > t.getr ();}
8586 Int rem () const {
86- Uint R = getr ();
87- return 2 * R > (Uint )mod () ? R - mod () : R;
87+ UInt R = getr ();
88+ return 2 * R > (UInt )mod () ? R - mod () : R;
8889 }
8990
9091 // Only use if you really know what you're doing!
91- Uint modmod () const {return (Uint )8 * mod () * mod ();};
92- void add_unsafe (Uint t) {r += t;}
92+ UInt modmod () const {return (UInt )8 * mod () * mod ();};
93+ void add_unsafe (UInt t) {r += t;}
9394 void pseudonormalize () {r = std::min (r, r - modmod ());}
9495 modint const & normalize () {
95- if (r >= (Uint )mod ()) {
96+ if (r >= (UInt )mod ()) {
9697 r %= mod ();
9798 }
9899 return to_modint ();
99100 }
100- void setr (Uint rr) {r = m_transform (rr);}
101- Uint getr () const {
102- Uint res = m_reduce (r);
101+ void setr (UInt rr) {r = m_transform (rr);}
102+ UInt getr () const {
103+ UInt res = m_reduce (r);
103104 return std::min (res, res - mod ());
104105 }
105- void setr_direct (Uint rr) {r = rr;}
106- Uint getr_direct () const {return r;}
106+ void setr_direct (UInt rr) {r = rr;}
107+ UInt getr_direct () const {return r;}
107108 private:
108- Uint r;
109+ UInt r;
109110 modint& to_modint () {return static_cast <modint&>(*this );}
110111 modint const & to_modint () const {return static_cast <modint const &>(*this );}
111112 };
112113 template <typename modint>
113114 concept modint_type = std::is_base_of_v<modint_base<modint, typename modint::Int>, modint>;
114115 template <modint_type modint>
115116 std::istream& operator >> (std::istream &in, modint &x) {
116- typename modint::Uint r;
117+ typename modint::UInt r;
117118 auto &res = in >> r;
118119 x.setr (r);
119120 return res;
@@ -127,24 +128,24 @@ namespace cp_algo::math {
127128 struct modint : modint_base<modint<m>, decltype (m)> {
128129 using Base = modint_base<modint<m>, decltype (m)>;
129130 using Base::Base;
130- static constexpr Base::Uint im = m % 2 ? inv2(-m) : 0 ;
131- static constexpr Base::Uint r2 = (typename Base::Int2 )(-1 ) % m + 1 ;
131+ static constexpr Base::UInt im = m % 2 ? inv2(-m) : 0 ;
132+ static constexpr Base::UInt r2 = (typename Base::UInt2 )(-1 ) % m + 1 ;
132133 static constexpr Base::Int mod () {return m;}
133- static constexpr Base::Uint imod () {return im;}
134- static constexpr Base::Int2 pw128 () {return r2;}
134+ static constexpr Base::UInt imod () {return im;}
135+ static constexpr Base::UInt2 pw128 () {return r2;}
135136 };
136137
137138 template <typename Int = int64_t >
138139 struct dynamic_modint : modint_base<dynamic_modint<Int>, Int> {
139140 using Base = modint_base<dynamic_modint<Int>, Int>;
140141 using Base::Base;
141142 static Int mod () {return m;}
142- static Base::Uint imod () {return im;}
143- static Base::Int2 pw128 () {return r2;}
143+ static Base::UInt imod () {return im;}
144+ static Base::UInt2 pw128 () {return r2;}
144145 static void switch_mod (Int nm) {
145146 m = nm;
146147 im = m % 2 ? inv2 (-m) : 0 ;
147- r2 = (typename Base::Int2 )(-1 ) % m + 1 ;
148+ r2 = (typename Base::UInt2 )(-1 ) % m + 1 ;
148149 }
149150
150151 // Wrapper for temp switching
@@ -158,13 +159,13 @@ namespace cp_algo::math {
158159 }
159160 private:
160161 static Int m;
161- static Base::Uint im, r1, r2;
162+ static Base::UInt im, r1, r2;
162163 };
163164 template <typename Int>
164165 Int dynamic_modint<Int>::m = 1 ;
165166 template <typename Int>
166- dynamic_modint<Int>::Base::Uint dynamic_modint<Int>::im = -1 ;
167+ dynamic_modint<Int>::Base::UInt dynamic_modint<Int>::im = -1 ;
167168 template <typename Int>
168- dynamic_modint<Int>::Base::Uint dynamic_modint<Int>::r2 = 0 ;
169+ dynamic_modint<Int>::Base::UInt dynamic_modint<Int>::r2 = 0 ;
169170}
170171#endif // CP_ALGO_MATH_MODINT_HPP
0 commit comments