Algorithm-Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub UScuber/Algorithm-Library

:warning: math/combination.hpp

Depends on

Code

#include "mint.hpp"

constexpr int len = 2000010;
mint fact[len], invfact[len];
struct initializer {
  initializer(){
    fact[0] = fact[1] = 1;
    for(int i = 2; i < len; i++) fact[i] = fact[i - 1] * i;
    invfact[len - 1] = fact[len - 1].inv();
    for(int i = len - 2; i >= 0; i--) invfact[i] = invfact[i + 1] * (i + 1);
  }
} initializer;
mint C(const int n, const int r){
  assert(0 <= n && n < len);
  if(n < r) return 0;
  return fact[n] * invfact[n - r] * invfact[r];
}
mint P(const int n, const int r){
  assert(0 <= n && n < len);
  if(n < r) return 0;
  return fact[n] * invfact[n - r];
}
mint H(const int n, const int r){
  assert(0 < n && 0 <= r);
  assert(0 < n+r && n+r <= len);
  return fact[n + r - 1] * invfact[n - 1] * invfact[r];
}
#line 2 "math/mint.hpp"

template <int mod>
struct Mint {
  ll x;
  constexpr Mint(ll x = 0) : x((x + mod) % mod){}
  static constexpr int get_mod(){ return mod; }
  constexpr Mint operator-() const{ return Mint(-x); }
  constexpr Mint operator+=(const Mint &a){
    if((x += a.x) >= mod) x -= mod;
    return *this;
  }
  constexpr Mint &operator++(){
    if(++x == mod) x = 0;
    return *this;
  }
  constexpr Mint operator++(int){
    Mint temp = *this;
    if(++x == mod) x = 0;
    return temp;
  }
  constexpr Mint &operator-=(const Mint &a){
    if((x -= a.x) < 0) x += mod;
    return *this;
  }
  constexpr Mint &operator--(){
    if(--x < 0) x += mod;
    return *this;
  }
  constexpr Mint operator--(int){
    Mint temp = *this;
    if(--x < 0) x += mod;
    return temp;
  }
  constexpr Mint &operator*=(const Mint &a){
    (x *= a.x) %= mod;
    return *this;
  }
  constexpr Mint operator+(const Mint &a) const{ return Mint(*this) += a; }
  constexpr Mint operator-(const Mint &a) const{ return Mint(*this) -= a; }
  constexpr Mint operator*(const Mint &a) const{ return Mint(*this) *= a; }
  constexpr Mint pow(ll t) const{
    if(!t) return 1;
    Mint res = 1, v = *this;
    while(t){
      if(t & 1) res *= v;
      v *= v;
      t >>= 1;
    }
    return res;
  }
  constexpr Mint inv() const{ return pow(mod - 2); }
  constexpr Mint &operator/=(const Mint &a){ return (*this) *= a.inv(); }
  constexpr Mint operator/(const Mint &a) const{ return Mint(*this) /= a; }
  constexpr bool operator==(const Mint &a) const{ return x == a.x; }
  constexpr bool operator!=(const Mint &a) const{ return x != a.x; }
  constexpr bool operator<(const Mint &a) const{ return x < a.x; }
  constexpr bool operator>(const Mint &a) const{ return x > a.x; }
  friend istream &operator>>(istream &is, Mint &a){ return is >> a.x; }
  friend ostream &operator<<(ostream &os, const Mint &a){ return os << a.x; }
};
//using mint = Mint<1000000007>;
#line 2 "math/combination.hpp"

constexpr int len = 2000010;
mint fact[len], invfact[len];
struct initializer {
  initializer(){
    fact[0] = fact[1] = 1;
    for(int i = 2; i < len; i++) fact[i] = fact[i - 1] * i;
    invfact[len - 1] = fact[len - 1].inv();
    for(int i = len - 2; i >= 0; i--) invfact[i] = invfact[i + 1] * (i + 1);
  }
} initializer;
mint C(const int n, const int r){
  assert(0 <= n && n < len);
  if(n < r) return 0;
  return fact[n] * invfact[n - r] * invfact[r];
}
mint P(const int n, const int r){
  assert(0 <= n && n < len);
  if(n < r) return 0;
  return fact[n] * invfact[n - r];
}
mint H(const int n, const int r){
  assert(0 < n && 0 <= r);
  assert(0 < n+r && n+r <= len);
  return fact[n + r - 1] * invfact[n - 1] * invfact[r];
}
Back to top page