-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodulo.cpp
More file actions
332 lines (268 loc) · 11.8 KB
/
modulo.cpp
File metadata and controls
332 lines (268 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <modulo.hpp>
#include <gcd.hpp>
#include <exponentiation.hpp>
#include <limits>
// Modular Binary Exponentiation
namespace nm {
template <typename T>
T mod_bin_exp(T x, T y, const T m) {
if (m <= 1) return 0;
if (y <= 0) return 1;
T z = mod_bin_exp<T>(x, y / 2, m);
return y % 2 ? (z * z % m) * x % m : z * z % m;
}
template <typename T>
T mod_bin_exp_iterative(T x, T y, const T m) {
if (y <= 0) return 1;
T res = 1;
while (y) {
if (y & 1) res = res * x % m;
x = x * x % m;
y >>= 1;
}
return res;
}
}
template int nm::mod_bin_exp<int>(int, int, int);
template long nm::mod_bin_exp<long>(long, long, long);
template long long nm::mod_bin_exp<long long>(long long, long long, long long);
template int nm::mod_bin_exp_iterative<int>(int, int, int);
template long nm::mod_bin_exp_iterative<long>(long, long, long);
template long long nm::mod_bin_exp_iterative<long long>(long long, long long, long long);
namespace nm {
// T is expected to be signed integer type.
// mod is a prime number.
template<typename T>
T modular_multiplicative_inverse(T num, const T mod) {
T x, y;
if (extended_gcd<T>(num, mod, x, y) != 1) return -1;
// (num * x + mod * y = 1) % mod
// num * x = 1 % mod
return (x % mod + mod) % mod;
}
// mod is a prime number.
template<typename T>
T prime_modular_multiplicative_inverse_by_bin_exp(T num, const T mod) {
return mod_bin_exp_iterative<T>(num, mod - 2, mod);
// mod is prime => phi-function(mod) = mod - 1
// phi-function counts number of coprimes to its argument
// that are less than the argument a.k.a. Euler Totient Function.
}
// num is non-negative integer.
// mod is a prime number.
template<typename T>
T prime_modular_multiplicative_inverse(T num, const T mod) {
num %= mod;
// mod is prime => gcd(num, mod) is 1
return num < 2 ? num : mod - (long long)(mod / num) *
prime_modular_multiplicative_inverse<T>(mod % num, mod) % mod;
}
} // modular inverse
template long long nm::modular_multiplicative_inverse<long long>(long long, const long long);
template long long nm::prime_modular_multiplicative_inverse_by_bin_exp<long long>(long long, const long long);
template long long nm::prime_modular_multiplicative_inverse<long long>(long long, const long long);
template int nm::modular_multiplicative_inverse<int>(int, const int);
template int nm::prime_modular_multiplicative_inverse_by_bin_exp<int>(int, const int);
template int nm::prime_modular_multiplicative_inverse<int>(int, const int);
// Modular Arithmetic //
namespace nm {
template<typename T>
Arithmetic<T>::Arithmetic(T mod_prime) : mod(mod_prime) {}
template<typename T>
T Arithmetic<T>::underflow(T x) {
if (x >= 0) return x % this->mod;
return (std::numeric_limits<T>::max() % this->mod +
(x - std::numeric_limits<T>::min() + 1)) % this->mod;
}
template<typename T>
T Arithmetic<T>::fix(T x) {
if (x >= 0) return x % this->mod;
return (this->mod + x) % this->mod;
}
// x and y are expected to be non negative integers.
template<typename T>
T Arithmetic<T>::add(T x, T y) {
return this->fix((x + y) % this->mod);
}
// x and y are expected to be non negative integers
template<typename T>
T Arithmetic<T>::subtract(T x, T y) {
return this->fix((x - y) % this->mod);
}
// multiply is log(y) operation
template<typename T>
T Arithmetic<T>::multiply(T x, T y) {
T z = 0;
while (y) {
if (y & 1) z = (z + x) % this->mod;
x = 2LL * x % this->mod;
y >>= 1;
}
return z;
}
// Raise requires exponentiation.
template<typename T>
T Arithmetic<T>::raise(T x, T y) {
return mod_bin_exp_iterative<T>(x, y, this->mod);
}
// Divide requires multiplicative_inverse.
template<typename T>
T Arithmetic<T>::divide(T x, T y) {
return this->multiply(x, prime_modular_multiplicative_inverse<T>(y, this->mod));
}
}
template class nm::Arithmetic<int>;
template class nm::Arithmetic<long long>;
namespace nm {
template<std::size_t M>
inline int64_t Int32_M<M>::get_value() const noexcept {
return this->value;
}
template<std::size_t M>
Int32_M<M> Int32_M<M>::inverse() const {
return Int32_M<M>(prime_modular_multiplicative_inverse<std::int64_t>(this->value, M));
}
template<std::size_t M>
template<typename T>
Int32_M<M>& Int32_M<M>::operator=(const T &x) {
this->value = std::int64_t(x) % M;
if (this->value < 0) this->value += M;
return *this;
}
template<std::size_t M>
Int32_M<M>& Int32_M<M>::operator=(const Int32_M<M> &x) {
this->value = x.get_value();
return *this;
}
template<std::size_t M>
Int32_M<M>& Int32_M<M>::operator+=(const Int32_M<M> &x) {
this->value += x.get_value();
if (this->value >= std::int64_t(M)) this->value -= M;
return *this;
}
template<std::size_t M>
Int32_M<M>& Int32_M<M>::operator-=(const Int32_M<M> &x) {
this->value -= x.get_value();
if (this->value < 0) this->value += M;
return *this;
}
template<std::size_t M>
Int32_M<M>& Int32_M<M>::operator*=(const Int32_M<M> &x) {
std::uint64_t y = x.get_value();
this->value = this->value * y % M;
return *this;
}
template<std::size_t M>
Int32_M<M>& Int32_M<M>::operator/=(const Int32_M<M> &x) {
return *this *= x.inverse();
}
template<std::size_t M>
Int32_M<M> &Int32_M<M>::operator++() {
return *this += 1;
}
template<std::size_t M>
Int32_M<M> &Int32_M<M>::operator--() {
return *this -= 1;
}
template<std::size_t M>
Int32_M<M> Int32_M<M>::operator++(int) {
Int32_M<M> y = *this;
*this += 1;
return y;
}
template<std::size_t M>
Int32_M<M> Int32_M<M>::operator--(int) {
Int32_M<M> y = *this;
*this -= 1;
return y;
}
template<std::size_t M>
std::strong_ordering Int32_M<M>::operator<=>(const Int32_M<M> &x) const noexcept {
return (this->value == x.get_value() ? std::strong_ordering::equivalent :
(this->value < x.get_value() ? std::strong_ordering::less :
std::strong_ordering::greater));
}
template<std::size_t M>
template<typename T>
std::strong_ordering Int32_M<M>::operator<=>(const T &x) const noexcept {
return (this->value == x ? std::strong_ordering::equivalent :
(this->value < x ? std::strong_ordering::less :
std::strong_ordering::greater));
}
// non-member functions //
template <std::uint64_t T, typename U>
Int32_M<T> raise(const Int32_M<T> &x, const U &y) {
return Int32_M<T>(mod_bin_exp<int64_t>(x.get_value(), y, T));
}
template <std::size_t T, typename U>
inline Int32_M<T> operator+(const Int32_M<T> &x, const U &y) {
return Int32_M<T>(x) += Int32_M<T>(y);
}
template<std::size_t T, typename U>
inline Int32_M<T> operator-(const Int32_M<T> &x, const U &y) {
return Int32_M<T>(x) -= Int32_M<T>(y);
}
template<std::size_t T, typename U>
inline Int32_M<T> operator*(const Int32_M<T> &x, const U &y) {
return Int32_M<T>(x) *= Int32_M<T>(y);
}
template<std::size_t T, typename U>
inline Int32_M<T> operator/(const Int32_M<T> &x, const U &y) {
return Int32_M<T>(x) /= Int32_M<T>(y);
}
template<std::size_t T, typename U>
inline std::strong_ordering operator<=>(const Int32_M<T> &x, const U &y) noexcept {
return Int32_M<T>(x) <=> Int32_M<T>(y);
}
} // derived data type for modular arithmetic
template class nm::Int32_M<nm::M17>;
namespace nm {
// instantiate initialization
template int32_m::Int32_M<std::int32_t>(std::int32_t);
template int32_m::Int32_M<std::int64_t>(std::int64_t);
template int32_m& int32_m::operator=<std::int32_t>(std::int32_t const&);
template int32_m& int32_m::operator=<std::int64_t>(std::int64_t const&);
template int32_m& int32_m::operator=<std::uint32_t>(std::uint32_t const&);
template int32_m& int32_m::operator=<std::uint64_t>(std::uint64_t const&);
template std::strong_ordering int32_m::operator<=><std::int32_t>(std::int32_t const&) const noexcept;
template std::strong_ordering int32_m::operator<=><std::int64_t>(std::int64_t const&) const noexcept;
// instantiate friends for binary operations
template int32_m raise<int32_m::modulus>(int32_m const&, int32_m const&);
template int32_m operator-<int32_m::modulus>(int32_m const&, int32_m const&);
template int32_m operator+<int32_m::modulus>(int32_m const&, int32_m const&);
template int32_m operator*<int32_m::modulus>(int32_m const&, int32_m const&);
template int32_m operator/<int32_m::modulus>(int32_m const&, int32_m const&);
template std::strong_ordering operator<=><int32_m::modulus>(int32_m const&, int32_m const&);
template int32_m raise<int32_m::modulus>(int32_m const&, std::int32_t const&);
template int32_m operator-<int32_m::modulus>(int32_m const&, std::int32_t const&);
template int32_m operator+<int32_m::modulus>(int32_m const&, std::int32_t const&);
template int32_m operator*<int32_m::modulus>(int32_m const&, std::int32_t const&);
template int32_m operator/<int32_m::modulus>(int32_m const&, std::int32_t const&);
template std::strong_ordering operator<=><int32_m::modulus>(int32_m const&, std::int32_t const&);
template int32_m raise<int32_m::modulus>(int32_m const&, std::int64_t const&);
template int32_m operator-<int32_m::modulus>(int32_m const&, std::int64_t const&);
template int32_m operator+<int32_m::modulus>(int32_m const&, std::int64_t const&);
template int32_m operator*<int32_m::modulus>(int32_m const&, std::int64_t const&);
template int32_m operator/<int32_m::modulus>(int32_m const&, std::int64_t const&);
template std::strong_ordering operator<=><int32_m::modulus>(int32_m const&, std::int64_t const&);
template int32_m raise<int32_m::modulus>(int32_m const&, std::uint32_t const&);
template int32_m operator-<int32_m::modulus>(int32_m const&, std::uint32_t const&);
template int32_m operator+<int32_m::modulus>(int32_m const&, std::uint32_t const&);
template int32_m operator*<int32_m::modulus>(int32_m const&, std::uint32_t const&);
template int32_m operator/<int32_m::modulus>(int32_m const&, std::uint32_t const&);
template std::strong_ordering operator<=><int32_m::modulus>(int32_m const&, std::uint32_t const&);
template int32_m raise<int32_m::modulus>(int32_m const&, std::uint64_t const&);
template int32_m operator-<int32_m::modulus>(int32_m const&, std::uint64_t const&);
template int32_m operator+<int32_m::modulus>(int32_m const&, std::uint64_t const&);
template int32_m operator*<int32_m::modulus>(int32_m const&, std::uint64_t const&);
template int32_m operator/<int32_m::modulus>(int32_m const&, std::uint64_t const&);
template std::strong_ordering operator<=><int32_m::modulus>(int32_m const&, std::uint64_t const&);
template int32_m raise<int32_m::modulus>(int32_m const&, char const&);
template int32_m operator-<int32_m::modulus>(int32_m const&, char const&);
template int32_m operator+<int32_m::modulus>(int32_m const&, char const&);
template int32_m operator*<int32_m::modulus>(int32_m const&, char const&);
template int32_m operator/<int32_m::modulus>(int32_m const&, char const&);
template std::strong_ordering operator<=><int32_m::modulus>(int32_m const&, char const&);
} // TODO: looped instantiation?
template class nm::Int32_M<nm::M93>;
template class nm::Int32_M<nm::M27>;