Code RoomBinomial coefficient modulo prime
HardPrep Room Coding #965

Binomial coefficient modulo prime

CodingAlgorithms & data structuresSenior–Staff~30 min

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) → int
Examples
in[5,2,1000000007]out10
What 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
InputExpectedGot
[5,2,1000000007]10not run yetsample