Advertisement
Abrar_Al_Samit

Combinatorics Template

Jun 9th, 2025 (edited)
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. template<int MOD>
  5. struct modint {
  6.     int val;
  7.  
  8.     modint(long long v = 0) {
  9.         if (v < 0) v = v % MOD + MOD;
  10.         if (v >= MOD) v %= MOD;
  11.         val = v;
  12.     }
  13.  
  14.     modint operator+ (const modint &other) const {
  15.         int res = val + other.val;
  16.         if (res >= MOD) res -= MOD;
  17.         return modint(res);
  18.     }
  19.  
  20.     modint operator- (const modint &other) const {
  21.         int res = val - other.val;
  22.         if (res < 0) res += MOD;
  23.         return modint(res);
  24.     }
  25.  
  26.     modint operator* (const modint &other) const {
  27.         return modint((long long)val * other.val % MOD);
  28.     }
  29.  
  30.     modint operator/ (const modint &other) const {
  31.         return *this * other.inv();
  32.     }
  33.  
  34.     modint pow(long long exp) const {
  35.         modint base = *this, result = 1;
  36.         while (exp) {
  37.             if (exp & 1) result = result * base;
  38.             base = base * base;
  39.             exp >>= 1;
  40.         }
  41.         return result;
  42.     }
  43.  
  44.     modint inv() const {
  45.         return pow(MOD - 2);
  46.     }
  47.  
  48.     modint& operator+= (const modint &other) { return *this = *this + other; }
  49.     modint& operator-= (const modint &other) { return *this = *this - other; }
  50.     modint& operator*= (const modint &other) { return *this = *this * other; }
  51.     modint& operator/= (const modint &other) { return *this = *this / other; }
  52.  
  53.     friend ostream& operator<<(ostream &os, const modint &m) {
  54.         return os << m.val;
  55.     }
  56.     friend istream& operator>>(istream &is, modint &m) {
  57.         long long x; is >> x; m = modint(x);
  58.         return is;
  59.     }
  60. };
  61.  
  62. const int nax = 2e5 + 10;
  63. const int mod = 1e9 + 7;
  64. using mint = modint<mod>;
  65. #warning update constant values
  66.  
  67.  
  68. mint fact[nax], invfact[nax];
  69. void precompute() {
  70.     fact[0] = invfact[0] = 1;
  71.     for (int i = 1; i < nax; ++i) fact[i] = fact[i-1] * i;
  72.     invfact[nax-1] = fact[nax-1].inv();
  73.     for (int i = nax-2; i >= 1; --i) invfact[i] = invfact[i+1] * (i+1);
  74. }
  75. mint nCr(int n, int r) {
  76.     if (r < 0 || r > n) return 0;
  77.     return fact[n] * invfact[r] * invfact[n - r];
  78. }
  79.  
  80. void PlayGround() {
  81.  
  82. }
  83. int main() {
  84.     ios_base::sync_with_stdio(0); cin.tie(0);
  85.     int t; cin>>t;
  86.     while(t--) PlayGround();
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement