145 static IntType add(IntType x, IntType c)
149 else if (c <= traits::const_max - m)
150 return add_small(x, c);
155 static IntType mult(IntType a, IntType x)
159 else if (m <= traits::const_max / a)
160 return mult_small(a, x);
161 else if (traits::is_signed && (m % a < m / a))
162 return mult_schrage(a, x);
172 static IntType mult_add(IntType a, IntType x, IntType c)
174 if (m <= (traits::const_max - c) / a)
175 return (a * x + c) % m;
177 return add(mult(a, x), c);
180 static IntType invert(IntType x)
182 return x == 0 ? 0 : invert_euclidian(x);
191 static IntType add_small(IntType x, IntType c)
199 static IntType mult_small(IntType a, IntType x)
204 static IntType mult_schrage(IntType a, IntType value)
206 const IntType q = m / a;
207 const IntType r = m % a;
209 value = a * (value % q) - r * (value / q);
216 static IntType invert_euclidian(IntType c)
227 return (l2 < 1 ? l2 + m : l2);
232 return (l1 < 1 ? l1 + m : l1);
244 class const_mod<unsigned int, 0>
246 typedef unsigned int IntType;
250 static IntType add(IntType x, IntType c) {
return x + c; }
251 static IntType mult(IntType a, IntType x) {
return a * x; }
252 static IntType mult_add(IntType a, IntType x, IntType c) {
return a * x + c; }
264 class const_mod<unsigned long, 0>
266 typedef unsigned long IntType;
270 static IntType add(IntType x, IntType c) {
return x + c; }
271 static IntType mult(IntType a, IntType x) {
return a * x; }
272 static IntType mult_add(IntType a, IntType x, IntType c) {
return a * x + c; }