-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmint.js
More file actions
14 lines (14 loc) · 866 Bytes
/
mint.js
File metadata and controls
14 lines (14 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Mint {
static mod(a, p) { let r = a % p; while (r < 0) r += p; return r; }
static pow(a, e, p) { let r = 1; while (e > 0) { if ((e & 1) == 1) r = r * a % p; a = a * a % p; e >>= 1; } return r; }
static inv(a, p) { return Mint.pow(a, p-2, p); }
static factorials(n, p) { let f = [1], inv = []; for (let i = 1; i <= n; i++) inv[i] = Mint.inv(f[i] = i * f[i-1] % p, p); return [f, inv]; }
constructor(value, mod) { this.mod = mod; this.value = Mint.mod(value, mod); }
valueOf() { return this.value; }
add(x) { return new Mint(this.value + x, this.mod); }
sub(x) { return new Mint(this.value - x, this.mod); }
mul(x) { return new Mint(this.value * x, this.mod); }
div(x) { return new Mint(this.value * Mint.inv(x, this.mod), this.mod); }
pow(x) { return new Mint(Mint.pow(this.value, x, this.mod), this.mod); }
inv() { return this.pow(this.mod-2); }
}