Quote:
Originally Posted by lali.p
I am looking for an efficient way to calculate ((a+b)!/a!b!) mod p. p is prime.
|
You might find some of the ideas
here to be useful.
You definitely need a big number library for C/C++, or use bc which has arbitrary precision. Hint: Think of the difference in results between using libraries which support arbitrary number SIZE, and using a floating point library which would introduce a limit on PRECISION... (did I get that right?).
My first thoughts as programmer would be to craft a
carefully designed recursive algorithm for the job... good luck!
EDIT:
Thinking about this has stirred a few brain cells that I have not recently used, but the key for such a problem (once you have the right numeric types libraries in place) will be a tail-recursive algorithm as explained in this
article on Tail Recursion. This is what I had in mind with my comment about using a
carefully designed recursive algorithm for the job. And it should be simple for a factorial calculation - I thinks...
If you write a tail-recursive algorithm for the factorials using the big number libraries, it will be optimized into a very efficient iterative function by the compiler (gcc can handle this AFAIK).
Hope this points you in a good direction!
/EDIT: