
N! = 1 * 2 * 3 *... N
The first line of input contains a single integer 'T', representing the number of test cases.
The first line of each test case contains three space-separated integers 'n', 'r', and 'p'.
For each test case, print a single line containing an integer representing the value of (nCr) % p.
The output of each test case will be printed on a separate line.
You don't need to input or print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= n, r, p <= 5 * 10 ^ 2
p is prime number.
Time limit: 1 sec.
Expression nCr = fact(n) / (fact(r) * fact(n - r))
Here fact() means factorial.
nCr % p = (fac[n] * powInverse(fac[r], 1) % p * powInverse(fac[n - r], 1) % p) % p; (From Fermat Little Algorithm) which will further be broken down to
nCr % p = (fac[n] % p * pow(fac[r], p - 2) % p * pow(fac[n - r]), p - 2) % p
Here powInverse() means inverse modulo under p i.e (1 / n) % p and pow() means power under modulo p.
To calculate the factorial we can maintain a dp array such that dp[X] = dp[X - 1] * X as X! = (X) * (X - 1)!. Where ‘X’ is some integer. And the factorial of the required number can be found at the last index of dp array.
Function to calculate (x ^ y) % p.
The function will find inverse modulo of 'n' under ‘p’, i.e. (1 / n ) % p.
Merge Two Sorted Arrays Without Extra Space
Merge Two Sorted Arrays Without Extra Space
Co-Prime
Count Repeating Digits
First Digit One
Special Digit Numbers