templates/modint.cpp
compilable
$cat templates/modint
Modular Integer (ModInt)
Type-safe modular arithmetic with operator overloading for clean competitive programming code.
#modint#modular#arithmetic#operator-overloading
Log in to track progress, save custom versions, and organize into collections.Log In
$cat source_code/
cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <int MOD = 1000000007>
struct ModInt {
int val;
ModInt(ll v = 0) { val = v % MOD; if (val < 0) val += MOD; }
ModInt operator+(const ModInt &o) const { return ModInt(val + o.val); }
ModInt operator-(const ModInt &o) const { return ModInt(val - o.val); }
ModInt operator*(const ModInt &o) const { return ModInt((ll)val * o.val); }
ModInt operator/(const ModInt &o) const { return *this * o.inv(); }
ModInt &operator+=(const ModInt &o) { val = (val + o.val) % MOD; return *this; }
ModInt &operator-=(const ModInt &o) { val = (val - o.val + MOD) % MOD; return *this; }
ModInt &operator*=(const ModInt &o) { val = (ll)val * o.val % MOD; return *this; }
ModInt &operator/=(const ModInt &o) { return *this = *this / o; }
ModInt pow(ll e) const {
ModInt res(1), b(val);
while (e > 0) {
if (e & 1) res *= b;
b *= b;
e >>= 1;
}
return res;
}
ModInt inv() const { return pow(MOD - 2); }
bool operator==(const ModInt &o) const { return val == o.val; }
bool operator!=(const ModInt &o) const { return val != o.val; }
friend ostream &operator<<(ostream &os, const ModInt &m) { return os << m.val; }
friend istream &operator>>(istream &is, ModInt &m) { ll v; is >> v; m = ModInt(v); return is; }
};37 linesutf-8
$cat explanation_notes.md
notes_viewer --renderedmarkdown (math enabled)
ModInt
A wrapper type that automatically applies after every operation. Eliminates the most common source of bugs in CP: forgetting % MOD at intermediate steps.
Operations
+, -, *, / all work naturally between ModInt valuespow(e) computes via binary exponentiationinv() computes (Fermat's little theorem, requires prime )cout << x prints the value directlyWhen to Use
% MOD everywhereNotes
MOD must be prime for division/inverse to workMOD by using: using Mint = ModInt<998244353>;Complexity
+, -, * — /, inv() — pow(e) —