๐
LeetGoat
Interview Prep
Competitive Programming
Discord
Math templates
Range queries
2D range queries
Sets
Union find
Strings
Tree queries
Tree helpers
Graphs
Matrices
Math
Primes
Bit ops & Bitset
Geometry
Misc.
Golden References
Counting (ModCalc)
ModCalc
Open code โ
Factorials and choosing
getFactorialWithMod(x)
-> x! % mod
e.g. getFactorialWithMod(5) -> 120
0 once x >= mod; works for any mod
O(1) amortized
lazily grows when called with bigger x
getInverseFactorialWithMod(x)
-> 1 / x! % mod
e.g. getInverseFactorialWithMod(5) -> 808333339 (that times 120 is 1 mod 1e9 + 7)
needs x < mod; mod must be prime
O(1) amortized
lazily grows when called with bigger x
nChooseKWithMod(n, k)
-> ways to pick k of n, order ignored
e.g. nChooseKWithMod(5, 2) -> 10
0 if k < 0 or k > n; mod must be prime
O(1) amortized
lazily grows when called with bigger n (k doesn't matter)
nPermuteKWithMod(n, k)
-> ways to pick k of n in order
e.g. nPermuteKWithMod(5, 2) -> 20
0 if k < 0 or k > n; mod must be prime
O(1) amortized
lazily grows when called with bigger n (k doesn't matter)
interleaveTwoSequencesWithMod(length1, length2)
-> ways to merge them, each keeping its order
e.g. interleaveTwoSequencesWithMod(2, 1) -> 3 (aab, aba, baa)
mod must be prime
O(1) amortized
lazily grows when called with bigger length1 + length2
Putting items into buckets (no Stirling)
waysToPutIdenticalItemsIntoDistinctBucketsWithMod(items, buckets, allowEmptyBuckets: bool โ required)
-> count
e.g. (3, 2, True) -> 4: 3+0, 2+1, 1+2, 0+3; (3, 2, False) -> 2: 2+1, 1+2
stars and bars; mod must be prime
O(1) amortized
lazily grows when called with bigger items + buckets
waysToPutDistinctItemsIntoDistinctBucketsWithMod(items, buckets, allowEmptyBuckets: bool โ required)
-> count
e.g. (3, 2, True) -> 8 (2^3); (3, 2, False) -> 6
O(log items) with empty buckets allowed (any mod); O(buckets * log items) without (mod must be prime)
O(buckets ยท log items)
lazily grows when called with bigger buckets (already inside this cost)
Mod arithmetic
modPow(base, exponent)
-> base^exponent % mod
e.g. modPow(2, 10) -> 1024
works for any mod
O(log exponent)
modInv(x)
-> 1 / x % mod
e.g. modInv(2) -> 500000004 (that times 2 is 1 mod 1e9 + 7)
x must not be a multiple of mod; mod must be prime
O(log mod)
Stirling
Stirling numbers (distinct items, no empty buckets)
Open code โ
StirlingByFormula: every call pays, no table to build
waysToPutDistinctItemsIntoIdenticalBucketsWithMod(items, buckets, allowEmptyBuckets: bool โ required)
-> count
e.g. (3, 2, False) -> 3: {a}{bc}, {b}{ac}, {c}{ab}; (3, 2, True) -> 4: those, plus {abc}{}
buckets * log items on every call; needs buckets < mod; mod must be prime
O(buckets ยท log items)
lazily grows when called with bigger buckets (already inside this cost)
waysToPutDistinctItemsIntoDistinctBucketsWithMod(items, buckets, allowEmptyBuckets: bool โ required)
-> count
e.g. (3, 2, False) -> 6; (3, 2, True) -> 8 (2^3)
buckets * log items on every call; needs buckets < mod; mod must be prime
O(buckets ยท log items)
lazily grows when called with bigger buckets (already inside this cost)
StirlingByTable: grows lazily with the calls you make, then every call is a lookup
waysToPutDistinctItemsIntoIdenticalBucketsWithMod(items, buckets, allowEmptyBuckets: bool โ required)
-> count
e.g. (3, 2, False) -> 3: {a}{bc}, {b}{ac}, {c}{ab}; (3, 2, True) -> 4: those, plus {abc}{}
growing costs largest items * largest buckets in total over all calls, and that much memory; a call the table already covers is O(1); works for any mod
O(1) amortized
lazily grows when called with bigger items or bigger buckets
waysToPutDistinctItemsIntoDistinctBucketsWithMod(items, buckets, allowEmptyBuckets: bool โ required)
-> count
e.g. (3, 2, False) -> 6; (3, 2, True) -> 8 (2^3)
growing costs largest items * largest buckets in total over all calls, and that much memory; a call the table already covers is O(1); works for any mod
O(1) amortized
lazily grows when called with bigger items or bigger buckets
Fibonacci
Fibonacci
Open code โ
getXthFib(x)
-> the xth Fibonacci number, 1-indexed
e.g. getXthFib(1) -> 0, getXthFib(2) -> 1, getXthFib(10) -> 34
needs x >= 1; C++ overflows past x = 94
O(log x)
getXthFibMod(x, mod)
-> getXthFib(x) % mod
e.g. getXthFibMod(10, 7) -> 6
needs x >= 1; any mod
O(log x)
isFib(x)
-> True if x is a Fibonacci number
e.g. isFib(21) -> True, isFib(22) -> False
O(1)
getFibList(n)
-> every Fibonacci number <= n, in order
e.g. getFibList(10) -> [0, 1, 1, 2, 3, 5, 8]
[] if n < 0
O(log n)