Luzhiled's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub ei1333/library

:heavy_check_mark: Factorial(階乗) (math/combinatorics/factorial.hpp)

Depends on

Verified with

Code

#pragma once

#include <algorithm>
#include <cstdint>
#include <iterator>
#include <vector>

#include "sample-point-shift.hpp"

/**
 * @brief Factorial(階乗)
 */
template <typename Mint, typename F>
Mint factorial(std::int64_t n, const F& multiply) {
  if (n <= 1) return 1;
  if (n >= Mint::mod()) return 0;
  std::int64_t v = 1;
  while (v * v < n) v *= 2;
  Mint iv = Mint(1) / v;
  std::vector<Mint> G{1, v + 1};
  for (std::int64_t d = 1; d != v; d <<= 1) {
    std::vector<Mint> G1 = sample_point_shift(G, Mint(d) * iv, multiply);
    std::vector<Mint> G2 =
        sample_point_shift(G, Mint(d * v + v) * iv, multiply);
    std::vector<Mint> G3 =
        sample_point_shift(G, Mint(d * v + d + v) * iv, multiply);
    for (int i = 0; i <= d; i++) G[i] *= G1[i], G2[i] *= G3[i];
    std::copy(std::begin(G2), std::end(G2) - 1, std::back_inserter(G));
  }
  Mint res = 1;
  std::int64_t i = 0;
  while (i + v <= n) res *= G[i / v], i += v;
  while (i < n) res *= ++i;
  return res;
}
#line 2 "math/combinatorics/factorial.hpp"

#include <algorithm>
#include <cstdint>
#include <iterator>
#include <vector>

#line 2 "math/combinatorics/sample-point-shift.hpp"

#line 5 "math/combinatorics/sample-point-shift.hpp"

#line 2 "math/combinatorics/enumeration.hpp"

#line 4 "math/combinatorics/enumeration.hpp"
#include <cstddef>
#line 6 "math/combinatorics/enumeration.hpp"

/**
 * @brief Enumeration(組み合わせ)
 */
template <typename T>
struct Enumeration {
 private:
  static std::vector<T> _fact, _finv, _inv;

  inline static void expand(std::size_t sz) {
    if (_fact.size() < sz + 1) {
      int pre_sz = std::max(1, (int)_fact.size());
      _fact.resize(sz + 1, T(1));
      _finv.resize(sz + 1, T(1));
      _inv.resize(sz + 1, T(1));
      for (int i = pre_sz; i <= (int)sz; i++) {
        _fact[i] = _fact[i - 1] * T(i);
      }
      _finv[sz] = T(1) / _fact[sz];
      for (int i = (int)sz - 1; i >= pre_sz; i--) {
        _finv[i] = _finv[i + 1] * T(i + 1);
      }
      for (int i = pre_sz; i <= (int)sz; i++) {
        _inv[i] = _finv[i] * _fact[i - 1];
      }
    }
  }

 public:
  explicit Enumeration(std::size_t sz = 0) { expand(sz); }

  static inline T fact(int k) {
    expand(k);
    return _fact[k];
  }

  static inline T finv(int k) {
    expand(k);
    return _finv[k];
  }

  static inline T inv(int k) {
    expand(k);
    return _inv[k];
  }

  static T P(int n, int r) {
    if (r < 0 || n < r) return 0;
    return fact(n) * finv(n - r);
  }

  static T C(int p, int q) {
    if (q < 0 || p < q) return 0;
    return fact(p) * finv(q) * finv(p - q);
  }

  static T H(int n, int r) {
    if (n < 0 || r < 0) return 0;
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};

template <typename T>
std::vector<T> Enumeration<T>::_fact = std::vector<T>();
template <typename T>
std::vector<T> Enumeration<T>::_finv = std::vector<T>();
template <typename T>
std::vector<T> Enumeration<T>::_inv = std::vector<T>();
#line 7 "math/combinatorics/sample-point-shift.hpp"

/**
 * @brief Sample Point Shift(標本点シフト)
 */
template <typename Mint, typename F>
std::vector<Mint> sample_point_shift(const std::vector<Mint>& ys, const Mint& m,
                                     const F& multiply) {
  Enumeration<Mint> comb;
  int d = (int)ys.size() - 1;
  std::vector<Mint> f(d + 1), g(d * 2 + 1);
  for (int i = 0; i <= d; i++) {
    f[i] = ys[i] * comb.finv(i) * comb.finv(d - i);
    if ((d - i) & 1) f[i] = -f[i];
  }
  for (int i = 0; i <= 2 * d; i++) {
    g[i] = Mint(1) / (m - d + i);
  }
  auto h = multiply(f, g);
  Mint coef = 1;
  for (int i = 0; i <= d; i++) {
    coef *= (m - d + i);
  }
  for (int i = 0; i <= d; i++) {
    h[i + d] *= coef;
    coef *= (m + i + 1) * g[i];
  }
  return std::vector<Mint>{std::begin(h) + d, std::begin(h) + 2 * d + 1};
}
#line 9 "math/combinatorics/factorial.hpp"

/**
 * @brief Factorial(階乗)
 */
template <typename Mint, typename F>
Mint factorial(std::int64_t n, const F& multiply) {
  if (n <= 1) return 1;
  if (n >= Mint::mod()) return 0;
  std::int64_t v = 1;
  while (v * v < n) v *= 2;
  Mint iv = Mint(1) / v;
  std::vector<Mint> G{1, v + 1};
  for (std::int64_t d = 1; d != v; d <<= 1) {
    std::vector<Mint> G1 = sample_point_shift(G, Mint(d) * iv, multiply);
    std::vector<Mint> G2 =
        sample_point_shift(G, Mint(d * v + v) * iv, multiply);
    std::vector<Mint> G3 =
        sample_point_shift(G, Mint(d * v + d + v) * iv, multiply);
    for (int i = 0; i <= d; i++) G[i] *= G1[i], G2[i] *= G3[i];
    std::copy(std::begin(G2), std::end(G2) - 1, std::back_inserter(G));
  }
  Mint res = 1;
  std::int64_t i = 0;
  while (i + v <= n) res *= G[i / v], i += v;
  while (i < n) res *= ++i;
  return res;
}
Back to top page