Hey I am trying to evaluate, $\binom{n}{k}\bmod m$. I know that to evaluate multiplication modulo I can evaluate it as follows, $(x_1 x_2 x_3\cdots x_n) \bmod m = ((x_1 x_2)\bmod{m})\cdot x_3)\bmod m\cdots$. But to evaluate $\dfrac{x_n x_{n-1} \cdots x_{n-k-1}}{x_1 x_2\cdots x_k} \bmod m$ I have a denominator part which I am not sure how to evaluate.
Please provide some hints or guide me on how to proceed from here.
There are 3 ways i can think of.
Method 1
Use recurrence relation. ${n \choose k}\%m = ({n-1 \choose k-1} + {n-1 \choose k}) \% m$
Required base cases,
${n \choose 0}=1$
${n \choose n}=1$ .
Advantages
Disadvantages
Method 2
This might be an overkill for your purpose.
Re-write recurrence in method 1 in form of 2D transformation matrix. Then use logarithmic power exponentiation with modulo to find out in logarithmic nos of operations. I am assuming this is an overkill, will workout transformation matrix in case you are looking for this solution.
Advantages
Disadvantages
Method 3
We can use Fermat's Little theorem if $m$ is prime. So you can use your solution described while handling the denominator this way.
$a^{-1} \equiv a^{m-2} \pmod m$ So basically $a/b \pmod m = a*b^{-1} \pmod m$. Now you can use Fermat's Little theorem.
Kindly point out if you find anything incorrect,my first answer :)