I have often encountered calculating nCr problems the last one being one of the IIITM problems but it probably had weak test cases that got easy solutions AC. The other one https://cs3.interviewstreet.com/challenges/dashboard/#problem/50877a587c389 of codesprint was completey out of my minds and hands , I tried the inverse euler , power of p in n! ( I would also like if someone could explain me the theory of this method ) but all went in vain . Anyone who could suggest probably the best algos for cases when nCr is to be calculated with large n and r with and without % M where M is prime/not prime + the above method which I coded but couldn't understand how it was so.
This question is marked "community wiki".
asked 15 Nov '12, 14:45 kavishrox 
I encountered ^{n}C_{r} for the first time on GCJ 08, Round 3 Problem D .. link to analysis. The first key idea is that of Lucas' Theorem. Lucas's Theorem reduces ^{n}C_{r} % M to (^{n0}C_{r0} % M) (^{n1}C_{r1} % M) ... (^{nk}C_{rk} % M) Where, (n_{k}n_{k1}...n_{0}) is the base M representation of n
This means that any of the terms in the expansion of ^{ni}C_{ri} is not divisible by M. But this is only half the job done. Now you have to calculate ^{n}C_{r} % M (ignoring subscripts for brevity) for some 0 ≤ r ≤ n < M There are no ways around it, but to calculate [ n! / ( r! (nr)! ) ] % M Without loss of generality, we can assume r ≤ nr Remember, you can always do the Obvious. Calculate the binomial and then take a modulo. This is mostly not possible because the binomial will be too large to fit into either int or long long int (and Big Int will be too slow) This can then be simplified by using some clever ideas from Modular Arithmetic.
For brevity, we say B % M = A^{1} % M It is not always possible to calculate modular multiplicative inverses. If A and M are not coprime, finding a B will not be possible. Most problems give us a prime M. This means calculating B is always possible for any A < M. 142857 = 3^{3} * 11 * 13 * 37 You can find the result of ^{n}C_{r} % m for each m = 27, 11, 13, 37. Once you have the answers, you can reconstruct the answer modulo 142857 using Chinese Remainder Theorem. These answers can be found by Naive Methods since, m is small. I have also seen problems where M is a product of large primes, but square free. In these cases, you can calculate the answers modulo the primes that M is composed of using modular inverses (a little more about that below), and reconstruct the answer using CRT. I am yet to see a problem where M is neither, but if it is. I do not know if there is a way to calculate binomial coefficients generally (since you cannot calculate modular inverses, and neither can you brote force). I can dream of a problem where there are powers of small primes, but squarefree larger ones for a Number Theory extravaganza. There is one other way to calculate ^{n}C_{r} for any M which is small enough (say M ≤ 5000) or small n and r (say r ≤ n ≤ 5000) by using the following recursion with memoization ^{n}C_{r} = ^{n1}C_{r} + ^{n1}C_{r1} Since there are no divisions involved (no multiplications too) the answer is easy and precise to calculate even if the actual binomials would be very large. So, back to calculating [ n! / ( r! (nr)! ) ] % M, you can convert it to n * (n1) ... * (nr+1) * r^{1} * (r1)^{1} ... * 1 Of course, each product is maintained modulo M. This may be fast enough for problems where M is large and r is small. But sometimes, n and r can be very large. Fortunately, such problems always have a small enough M :D The trick is, you precalculate factorials, modulo M and precalculate inverse factorials, modulo M. fact[n] = n * fact[n1] % M Modular Multiplicative Inverse for a prime M is in fact very simple. From Fermat's Little Theorem A^{M1} % M = 1 Hence, A * A^{M2} % M = 1 A^{1} % M = A^{M2} % M, which is easy (and fast) to find using repeated squaring. There is one last link I wish to paste to make this complete. Modular inverses can also be found using the Extended Euclid's Algorithm. I have only had to use it once or twice among all the problems I ever solved. answered 15 Nov '12, 18:57 gamabunta ♦♦ 2
@gamabunta: this one is a godd recipe for a tutorial on codechef and topcoder @admins do try to make it a tutorial ..
(15 Nov '12, 22:06)
kavishrox
@gamabunta: Firstly Thanks  Well written . Lucas's Theorem reduces nCr % M to (n0Cr0 % M) (n1Cr1 % M) ... (nkCrk % M) Where, (nknk1...n0) is the base M representation of n (rkrk1...r0) is the base M representation of r This is only where M is prime .. But in that particular problem M is not prime and so can we reduce it into Base form (And still Lucas Theorem Holds) ? And if we use CRT how can we use it ? Thanks Anu :)
(17 Nov '12, 22:49)
anudeep2011
@gamabunta: yes probably if you explain chinese remainder that would be even more beneficial...I have read it a lot many times but forget it too easily.
(18 Nov '12, 00:13)
kavishrox
1
Amazing Detail. Cheers. Just a great feeling seeing someone spend so much time and energy typing this out for others. :) P.S. @anudeep2011:Lucas' Theorem holds true even for prime powers(like 27=3^3) (Called generalized Lucas' theorem)
(14 Mar '14, 01:31)
pvaish
Would be great if there was separate thread about this in latex format. Great answer!!
(22 Oct '16, 11:45)
mightymercado
showing 5 of 6
show all

I agree completely with what @kavishrox wrote... Admins, would it be possible to have some sort of "pin" feature, so that the "tutorial" like posts wouldn't go down? ;) It would greatly help newbies and ofc make this community even more respected :D Bruno answered 17 Nov '12, 04:56 kuruma 
somebody plz upvote me .i need to ask some questions. answered 25 Sep '16, 09:36 verma_anil 
@imujjwalanand the last method described. Keep factorials and inversefactorials modulo p. And you can get nCr % p in O(1) time with precomputation of O(p) and space O(p) answered 25 Sep '16, 05:44 kaushala 
To take as example : "142857 = 27 * 11 * 13 * 37. You can find the result of nCr % m for each m = 27, 11, 13, 37." as 27 is not a prime, i hope we would have to resort to last method of finding all fact[n] and inversefact[n] for 27. So we want nCr and we have x! for all x, WHY do we need inverse factorial ? Is it for (nr)! and r!. To find inverse of (nr) and r mod M and then multiply all of their factorials and then find % M. answered 18 Nov '12, 15:08 ashishnegi001 
what if do not want modulo, just ncr which does't fit in even long long? answered 15 Dec '13, 13:31 surajk9035 
How would you calculate nCr mod 27? You can't use the inverse modulo here. and also one can't do this by nCr=(n1)Cr+(n1)C(r1) given that the minimum space would be just 2 rows but each of size r which in this case is 1000000000? answered 17 Jul '13, 16:53 jaskaran_1 
What is the fastest among all the ways to calculate the value of nCr mod M? where M is definitely prime? answered 04 Apr '15, 06:25 imujjwalanand 
I think we should go for Inverse Modulo in this way. This is cheap, I mean O(m). r[i] = 1 for ( i = 2 ; i < limit ; i ++ ) Here is the link Modulo_inverse
link
This answer is marked "community wiki".
answered 18 Mar '16, 11:05 sameerhssns 