Hi everyone.
Lately I've done some combinatorial problems. And I am wondering.
What methods are used to calculate the binomial coefficients?
What is the most effective?
I have used dynamic programming, although it is effective for many
queries and can be modulated with any number, the range is too limited.
When the number is a bit higher, I have used modular arithmetic
but I need to precalculate all factorials and is needed a prime number
as a module and really the range is not so large just 10 ^ 5 maybe.
If the problem have higher numbers, I'm lost
Can you tell me what techniques you use and when they can be used are?
I hope you can help me
Thank you








Sorry, I didn't read your blog fully
??
TooSuspicious
But I think he's asking for when the range is bigger than 10^5, so I think it's a coincidence
I really did not know about the contest
But thank you for answering me at the end
In nCr mod p if numbers are large(upto 10^18) and p is not a prime number. So in that case: p = a1^b1*a2^b2*a3^b3....an^bn. so in that case find nCr mod (a1^b1), nCr mod(a2^b2) till nCr mod(an^bn) using lucas theorem and further use CRT to get the end result. Problem link: https://www.codechef.com/MAY17/problems/SANDWICH
Contest finished, I'm ready to give solution for your question:
You can find C(n, r) in
Unable to parse markup [type=CF_TEX]
time with O(n) precalculating. Read about modular inverse of number, precalculate factorials, then use formula:You can precalculate modular inverses in
Unable to parse markup [type=CF_TEX]
and you can find C(n, r) inUnable to parse markup [type=CF_TEX]
.Yes, but code will be O(nlogn) anyway.
If there are queries, precalculating will be nice optimization.
Nope, it will be
Unable to parse markup [type=CF_TEX]
.If you calculate inverses of factorials then how can you do in
Unable to parse markup [type=CF_TEX]
? I know a way to calculate all modular inverses from 1 to Mod, inUnable to parse markup [type=CF_TEX]
Then Pre-calculation will take O(n log MOD) too!
Click
This is not what he's asking for :) he says that he already knows this.
Oh, I'm seeing that now, sorry.