Advertisement
slash0t

bin power + combinations + factorial + inv

Aug 2nd, 2024 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. struct combinatorics {
  2.     vector<int> fact, inv, two;
  3.     int n;
  4.  
  5.     int bp(int num, int pow) {
  6.         int res = 1;
  7.         while (pow) {
  8.             if (pow & 1) res = num * res % M;
  9.             pow >>= 1;
  10.             num = num * num % M;
  11.         }
  12.         return res;
  13.     }
  14.  
  15.     combinatorics(int nn) {
  16.         int n = nn + 10;
  17.         fact.assign(n, 0);
  18.         inv.assign(n, 0);
  19.         two.assign(n, 0);
  20.  
  21.         two[0] = 1;
  22.         for (int i = 1; i < n; i++) two[i] = two[i - 1] * 2 % M;
  23.         fact[0] = 1;
  24.         for (int i = 1; i < n; i++) fact[i] = fact[i - 1] * i % M;
  25.         inv[n - 1] = bp(fact[n - 1], M - 2);
  26.         for (int i = n - 2; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % M;
  27.     }
  28.  
  29.     int comb(int n, int k) {
  30.         return fact[n] * inv[n - k] % M * inv[k] % M;
  31.     }
  32.  
  33.     int cat(int n) {
  34.         return fact[2 * n] * inv[n] % M * inv[n + 1] % M;
  35.     }
  36. };
  37. combinatorics cc(1e6);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement