Advertisement
Araf_12

inverse_mod_process_01

Aug 8th, 2024 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | Source Code | 0 0
  1. ll fact[N];
  2. ll powerMod(ll a, ll b)
  3. {
  4.    if (b == 0)
  5.        return 1;
  6.  
  7.  
  8.    if (b % 2 == 0)
  9.    {
  10.        ll x = powerMod(a, b / 2);
  11.        return (x * x) % mod;
  12.    }
  13.    else
  14.    {
  15.        ll x = powerMod(a, b / 2);
  16.        return ((x * x) % mod * a) % mod;
  17.    }
  18.  
  19.  
  20.    return 0;
  21. }
  22.  
  23.  
  24. ll inverseMod(ll a)
  25. {
  26.    return powerMod(a, mod - 2);
  27. }
  28.  
  29.  
  30. ll nCrMod(ll n, ll r)
  31. {
  32.    if (r == 0)
  33.        return 1;
  34.    if (r > n)
  35.        return 0;
  36.  
  37.  
  38.    return (fact[n] * inverseMod((fact[r] * fact[n - r]) % mod)) % mod;
  39. }
  40. void factorial()
  41. {
  42.    fact[0] = 1;
  43.    for (int i = 1; i < N; i++)
  44.    {
  45.        fact[i] = (fact[i - 1] * i) % mod;
  46.    }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement