Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions content/number-theory/ModularArithmetic.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Author: Lukas Polacek
* Author: Lukas Polacek, Joshua Andersson
* Date: 2009-09-28
* License: CC0
* Source: folklore
Expand All @@ -13,17 +13,18 @@
const ll mod = 17; // change to something else
struct Mod {
ll x;
Mod(ll xx) : x(xx) {}
Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
Mod operator-(Mod b) { return Mod((x - b.x + mod) % mod); }
Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
Mod(ll y) : Mod(y%mod+mod,0){}
Mod(ll y,int) : x(y<mod?y:y-mod){}
Mod operator+(Mod b) { return {x + b.x,0}; }
Mod operator-(Mod b) { return {x - b.x + mod,0}; }
Mod operator*(Mod b) { return {x * b.x % mod,0}; }
Mod operator/(Mod b) { return *this * invert(b); }
Mod invert(Mod a) {
ll x, y, g = euclid(a.x, mod, x, y);
assert(g == 1); return Mod((x + mod) % mod);
assert(g == 1); return x;
}
Mod operator^(ll e) {
if (!e) return Mod(1);
if (!e) return 1;
Mod r = *this ^ (e / 2); r = r * r;
return e&1 ? *this * r : r;
}
Expand Down
Loading