(1420 DIV 2) problem D Rescue Nibel!( Help Needed...) Important Concept Of Combinatorics ::: 
Difference between en1 and en2, changed 1,022 character(s)
[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;↵
}↵

~~~~~↵


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)