Fast n choose k mod p for large n?
So, here is how you can solve your problem. Of course you know the formula: comb(n,k) = n!/(k!*(n-k)!) = (n*(n-1)*…(n-k+1))/k! (See http://en.wikipedia.org/wiki/Binomial_coefficient#Computing_the_value_of_binomial_coefficients) You know how to compute the numerator: long long res = 1; for (long long i = n; i > n- k; –i) { res = (res * i) % p; } Now, … Read more