(1420 DIV 2) problem D Rescue Nibel!( Help Needed...) Important Concept Of Combinatorics :::

Revision en2, by Aditya_Sharma, 2020-09-27 13:00:48

[problem:1420 D] I dont know how to calculate N(c,r) by LOGIC applied in given below Mod_Inverse FUNCTION. Without this logic, this problem can't be solved. In the below code all factorials were precalculated till 3e6.
Tell me what problem was there in directly doing n! / ( r! * (n-r)! )....instead of using Mod_Inverse FUNCTION ...

((below code is not my code . I have got it from submissions.))

below is a code segment only to calculate N(c,r)...

(((( Fact[i] means (factorial i) or i! ))))

ll NCR (ll n, ll r, ll p)
{
   if (r == 0)
   {
      return 1;
   }
   return  (( Fact[n] * Mod_Inverse(Fact[n - r], p) ) % p  *  Mod_Inverse(Fact[r], p) ) % p ;
}
 
ll Mod_Inverse(ll a, ll m)
{
   // m is  A prime NUMBER.
   return Fast_Mod(a, m - 2, m);
}

ll Fast_Mod(ll a, ll b, ll m) 
{
    ll Result = 1;

    while (b > 0) 
    {
       if (b & 1)
       Result = (Result * a) % m;

       a = (a * a) % m;
       b = b >> 1;

    }

     return Result;
}

Tags concept learning, #combinatorics

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English Aditya_Sharma 2020-09-27 13:00:48 1022 Tiny change: '(c,r)...\n (((( ' -> '(c,r)...\n\n (((( ' (published)
en1 English Aditya_Sharma 2020-09-27 12:30:01 113 Initial revision (saved to drafts)