Answers for "ncr mod p"

2

ncr mod p

ll nCrModp(ll n, ll r) 
{  
    if (r > n - r) 
        r = n - r; 
  
    ll C[r + 1]; 
    memset(C, 0, sizeof(C)); 
  
    C[0] = 1; 
  
	for (ll i = 1; i <= n; i++) { 
        for (ll j = min(i, r); j > 0; j--) 
            C[j] = (C[j] + C[j - 1]) % mod; 
    } 
    return C[r]; 
}
Posted by: Guest on September-24-2020

Browse Popular Code Answers by Language