Binomial Coefficient
Implementation stolen from: https://cp-algorithms.com/combinatorics/binomial-coefficients.html#improved-implementation
A simple recursive implementation
Warning
Recently, I tried to use this formula for this Codeforces problem https://codeforces.com/contest/1999/submission/277997377, but I ran into integer overflow problems, even after using
long long
.
They do something like this in the solution https://codeforces.com/blog/entry/132373 (Problem F). This is the tourist solution https://codeforces.com/contest/1999/submission/274741514
These implementations use a different formula for modulus.
Binomial coefficient with MOD
Learned this from CP algorithms:
You precompute a factorial table.
Define your modular inverse:
Some Properties
Another Note: