Binomial coefficient modulo prime
Compute C(n, r) = n! / (r! * (n-r)!) modulo a prime p, for potentially large n. Return 0 if r < 0 or r > n. Constraints: 0 <= n <= 100000, p is prime with p > n (so all factorials are invertible mod p). You must never compute the full factorials as big integers proportional to n; work entirely under the modulus.
Implement
ncr_mod_p(n: int, r: int, p: int) → intExamples
in
[5,2,1000000007]out10What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 30 min
solution.py
InputExpectedGot
[5,2,1000000007]10not run yetsample