2
$\begingroup$

I want to calculate nCr (mod $10^9+1)$.so for calculating nCr we have: $$nCr=\frac{n!}{r!(n-r)!}$$ so I want to know whether it is true that I perform modulo operator to numerator and denominator separately instead of perform it to the result of nCr fraction directly?

I mean if it is true I can calculate the result like this: $${nCr} \pmod {10^9+1}=\frac{n! \pmod{10^9+1}}{r!(n-r)! \pmod{10^9+1}}$$ and if it is not true what is the solution because my numbers are very large and I can't perform modulo operator to the final result of the fraction.

$\endgroup$
  • $\begingroup$ You may find it useful to replace $\frac{n!}{(n-r)!}$ with $n \cdot (n-1) \cdot \cdots \cdot (n-r + 1)$. For example, $\frac{6!}{3!} = 6 \cdot 5 \cdot 4$. $\endgroup$ – Austin Mohr Feb 12 at 17:56
1
$\begingroup$

Yes, as I explain here (and its links) modular arithmetic is well-defined on all fractions expressible with denominator coprime to the modulus.

So in your example you'll first need to cancel from the denominator all factors in common with your modulus $10^9 + 1 = 7\cdot 11\cdot 13\cdot 19\cdot 52579\ $ (compare this purely arithmetical proof that binomial coefficients are integers).

As an example of problems with denominators not coprime to the modulus consider $ 6 = (2\cdot 6)/2.\,$ Reduced $\!\bmod 10\,$ it yields $\,2/2\,$ so if we naively cancel $2$ then we get the wrong answer $1$ (vs. $6).\,$ The problem is that $2$ is not cancellable $\bmod 10$. In fact $\,2/2\pmod{\!10} = 1\pmod{\!5}\,$ is the solution of $\,2x\equiv 2\pmod{\!10},\,$ so we need to cancel $2$ from the modulus too to obtain the complete set of modular solutions, i.e. this modular "fraction" or division is multi-valued (see here for much more).

But the original division operation in $\,6 = (2\cdot 6)/2\,$ denotes single-valued division in integers, not the prior multi-valued modular division. Generally the two agree only when the divisor is coprime to the modulus. So we need to preprocess the fraction by performing said cancellations (generally if you can't cancel all factors in common with the modulus then the modular fraction doesn't exist). This is explained at length if you chase links starting with the first link above.

$\endgroup$
1
$\begingroup$

You can if the number you dividing by and the modulus are relatively prime.

Suppose $a*k \equiv b*k \pmod n$ and we wish to (erroneously) conclude $a \equiv b \pmod n$.

$a*k = b*k + m n$ for some integer $n$.

So $a = b + \frac {mn}k$ so $k|mn$

Case 1: If $\gcd(k,n) =1$ then $k|mn\implies k|m$ and $a = b + \frac mkn $ and $a \equiv b\pmod n$

Case 2: If $\gcd(n,k) =d$ and $n = n'd$ and $k = k'd$ then $\gcd(n',k') =1$. So $k|mn \implies k'd|mn'd \implies k'|mn'\implies k'|m$ so $a = b + \frac {mn}k = b + \frac m{k'}n'$ so $a \equiv b \pmod {n' = \frac n{\gcd(n,k)}}$.

So in your case:

If $\gcd(r!(n-r)!, 10^9 + 1) = D$

And $n! \equiv A \pmod {\frac{10^9 -1}D}$

Then $\frac {n!}{r!(n-r)!}\equiv \frac A{\frac {r!(n-r)!}D} \pmod {\frac{10^9 -1}D}$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.