-
Notifications
You must be signed in to change notification settings - Fork 30
/
math.cpp
375 lines (317 loc) · 9.07 KB
/
math.cpp
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
// =====================================================================================
/**
* Computes the greatest common divisors (GCD) of the given two numbers.
* Worst case time complexity when (a, b) are two consecutive Fibonacci numbers.
*
* Complexity: O(log(max(a, b))
*
* @return the GCD of "a" and "b".
*/
template<class T>
T gcd(T a, T b) {
while (b) {
int tmp = a % b;
a = b;
b = tmp;
}
return a;
}
// =====================================================================================
/**
* Computes the least common multiple (LCM) of the given two numbers.
* Worst case time complexity when (a, b) are two consecutive Fibonacci numbers.
*
* Complexity: O(log(max(a, b))
*
* @return the LCM of "a" and "b".
*/
template<class T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
// =====================================================================================
/**
* Computes the Bezout's coefficients of the smallest positive linear combination of "a" and "b".
* using the extended Euclidean algorithm.
* (i.e. GCD(a, b) = s.a + t.b).
*
* Complexity: O(log(max(a, b))
*
* @return the Bezout's coefficients: "s" and "t".
*/
template<class T>
pair<T, T> extendedEuclid(T a, T b) {
if (b == 0) {
return {1, 0};
}
pair<T, T> p = extendedEuclid(b, a % b);
T s = p.first;
T t = p.second;
return {t, s - t * (a / b)};
}
// =====================================================================================
/**
* Raises a number to the given power using iterative fast power algorithm.
* Note that if (base=0, exp=0) is passed to the function it will return 1.
*
* Complexity: O(log(exp))
*
* @param base the base number.
* @param exp the exponent.
* @param mod the modulus.
*
* @return ((base^exp) % mod).
*/
template<class T>
T power(T base, T exp, T mod) {
T ans = 1;
base %= mod;
while (exp > 0) {
if (exp & 1) ans = (ans * base) % mod;
exp >>= 1;
base = (base * base) % mod;
}
return ans;
}
// =====================================================================================
/**
* Computes the modular inverse of "a" modulo "m".
* (i.e. (a * mod_inverse(a)) == 1 (mod m)).
*
* Note that the function works correctly only if "m" is a prime number.
*
* Complexity: O(log(m))
*
* @param a the number to compute its modular inverse.
* @param m the modulus. Must be a prime number.
*
* @return the modular inverse of "a" modulo "m".
*/
template<class T>
T modInverse(T a, T m) {
return power(a, m - 2, m);
}
// =====================================================================================
/**
* Computes the number of distinct sets of size "r" chosen from "n" items.
*
* Note that C(n, r) = C(n, n - r).
* So call the function with nCr(n, min(r, n-r)) for better performance.
*
* Complexity: O(r)
*
* @return "n" choose "r".
*/
int nCr(int n, int r) {
if (n < r)
return 0;
if (r == 0)
return 1;
return n * nCr(n - 1, r - 1) / r;
}
// =====================================================================================
// fact[i] : holds the value of the factorial of integer "i" (i.e. "i!" modulo "mod").
int fact[N];
/**
* Pre-computes the factorial array (from "fact[0]" to "fact[n]")
* modulo a given number.
*
* Call this function once before using the "nCr" function below.
*
* @param n the number of factorials to populate.
* @param mod the modulus.
*/
void init(int n, int mod) {
fact[0] = 1;
for (int i = 1; i <= n; ++i) {
fact[i] = (fact[i - 1] * 1LL * i) % mod;
}
}
/**
* Computes the number of distinct sets of size "r" chosen from "n" items
* modulo a given number.
*
* This function can only be used after populating the factorial array
* using the above function "init" with the same modulus.
*
* Complexity: O(log(mod))
*
* @return "n" choose "r".
*/
int nCr(int n, int r, int mod) {
return (fact[n] * modInverse(fact[r] * 1LL * fact[n - r], mod + 0LL)) % mod;
}
// =====================================================================================
// comb[n][r] : holds the value of "n" choose "r" modulo "mod".
int comb[N][N];
/**
* Builds Pascal's triangle for computing combinations (i.e. "nCr").
* After calling this function, "comb[n][r]" will be equals to "nCr".
*
* Complexity: O(n^2)
*
* @param n the size of Pascal's triangle to build.
* @param mod the modulus.
*/
void buildPT(int n, int mod) {
for (int i = comb[0][0] = 1; i <= n; ++i)
for (int j = comb[i][0] = 1; j <= i; ++j)
comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
}
// =====================================================================================
/**
* Checks whether an integer is prime or not.
*
* Complexity: O(sqrt(n))
*
* @param n a positive integer to check its integer primality.
*
* @return {@code true} if "n" is prime; {@code false} otherwise.
*/
template<class T>
bool isPrime(T n) {
if (n < 2)
return 0;
if (n % 2 == 0)
return (n == 2);
for (int i = 3; i * i <= n; i += 2)
if (n % i == 0)
return 0;
return 1;
}
// =====================================================================================
/**
* Probabilistic test of Miller Rabin algorithm for an integer "n".
* Note that: "n - 1 = power(2, k) * q".
*
* This function is to be called internally from "isPrimeMillerRabin" function.
* Do not call it directly.
*
* @return {@code false} if "n" is a composite number; {@code true} if it is a probable prime.
*/
template<class T>
bool millerRabinTest(T a, T k, T q, T n) {
T x = power<long long>(a, q, n);
if (x == 1) {
return true;
}
while (k--) {
if (x == n - 1) {
return true;
}
x = (x * 1LL * x) % n;
}
return false;
}
/**
* Checks whether an integer is prime or not using a deterministic method of
* Miller Rabin algorithm.
*
* Complexity: O(t.log(n))
*
* @param n a positive integer to check its integer primality.
*
* @return {@code true} if "n" is prime; {@code false} otherwise.
*/
template<class T>
bool isPrimeMillerRabin(T n) {
if (n == 2) {
return true;
}
if (n < 2 || n % 2 == 0) {
return false;
}
// Compute coefficients k, q such that "n - 1 = power(2, k) * q"
T k = 0;
T q = n - 1;
while ((q & 1) == 0) {
k++;
q >>= 1;
}
for (int a : {2, 3, 5, 7, 11, 13, 17, 19, 23}) {
if (n == a) {
return true;
}
if (!millerRabinTest(a, k, q, n)) {
return false;
}
}
return true;
}
// =====================================================================================
// prime[i] : true if integer "i" is prime; false otherwise.
bool prime[N];
/**
* Generates all the prime numbers of the integers from 1 to "n"
* using Sieve of Eratosthenes' algorithm.
* After calling this function, "prime[i]" will be equal true if "i" is prime; false otherwise.
*
* Complexity: O(n.log(log(n)))
*/
void generatePrimes(int n) {
memset(prime, true, sizeof(prime));
prime[0] = prime[1] = false;
for (int i = 2; i * i <= n; ++i) {
if (!prime[i]) continue;
for (int j = i * i; j <= n; j += i) {
prime[j] = false;
}
}
}
// =====================================================================================
// divs[i] : holds a list of all the prime divisors of integer "i".
vector<int> primeDivs[N];
/**
* Generates all the prime divisors of the integers from 1 to "n".
* After calling this function, "primeDivs[i]" will contains all the prime divisors of integer "i".
*
* Complexity: O(n.log(log(n)))
*/
void generatePrimeDivisors(int n) {
for (int i = 2; i <= n; ++i) {
if (primeDivs[i].size()) continue;
for (int j = i; j <= n; j += i) {
primeDivs[j].push_back(i);
}
}
}
// =====================================================================================
/**
* Computes all the divisors of a positive integer.
*
* Complexity: O(sqrt(n))
*
* @param n the positive integer to compute its divisors.
*
* @return a sorted list of all the divisors of "n".
*/
template<class T>
vector<T> getDivisors(T n) {
vector<T> divs;
for (T i = 1; i * i <= n; ++i) {
if (n % i != 0) continue;
divs.push_back(i);
if (i * i == n) continue;
divs.push_back(n / i);
}
sort(divs.begin(), divs.end());
return divs;
}
// =====================================================================================
// divs[i] : holds a list of all the divisors of integer "i".
vector<int> divs[N];
/**
* Generates all the divisors of the integers from 1 to "n".
* After calling this function, "divs[i]" will contains all the divisors of integer "i".
*
* Complexity: O(n.log(n))
*/
void generateDivisors(int n) {
for (int i = 1; i <= n; ++i)
for (int j = i; j <= n; j += i)
divs[j].push_back(i);
}
// =====================================================================================