cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:question: パスカルの三角形 (Pascal's triangle)
(include/emthrm/math/twelvefold_way/pascal.hpp)

二項係数 (binomial coefficients)

\[\binom{n}{k} = \binom{n - 1}{r - 1} + \binom{n - 1}{r} = \frac{n!}{k!\,(n - k)!}\]

時間計算量

  時間計算量
パスカルの三角形 $O(N^2)$
二項係数 $\langle O(N + \log{M}), O(1) \rangle$
二項係数 巨大な $n$ 版 $O(K + \log{M})$
二項係数の数表 巨大な $n$ 版 $O(K)$

仕様

パスカルの三角形

名前 戻り値
template <typename T>
std::vector<std::vector<T>> pascal(const int n);
$n$ 段のパスカルの三角形

二項係数

二項係数 巨大な $n$ 版

二項係数の数表 巨大な $n$ 版

名前 戻り値
template <unsigned int T>
std::vector<MInt<T>> large_nCk_init(long long n, const int k);
$\binom{n}{r}$ ($0 \leq r \leq k$) の数表

参考文献

http://drken1215.hatenablog.com/entry/2018/06/08/210000

パスカルの三角形

TODO

Submissons

Verified with

Code

#ifndef EMTHRM_MATH_TWELVEFOLD_WAY_PASCAL_HPP_
#define EMTHRM_MATH_TWELVEFOLD_WAY_PASCAL_HPP_

#include <vector>

namespace emthrm {

template <typename T>
std::vector<std::vector<T>> pascal(const int n) {
  std::vector<std::vector<T>> c(n + 1, std::vector<T>(n + 1, 0));
  for (int i = 0; i <= n; ++i) {
    c[i][0] = 1;
    for (int j = 1; j <= i; ++j) {
      c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
    }
  }
  return c;
}

}  // namespace emthrm

#endif  // EMTHRM_MATH_TWELVEFOLD_WAY_PASCAL_HPP_
#line 1 "include/emthrm/math/twelvefold_way/pascal.hpp"



#include <vector>

namespace emthrm {

template <typename T>
std::vector<std::vector<T>> pascal(const int n) {
  std::vector<std::vector<T>> c(n + 1, std::vector<T>(n + 1, 0));
  for (int i = 0; i <= n; ++i) {
    c[i][0] = 1;
    for (int j = 1; j <= i; ++j) {
      c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
    }
  }
  return c;
}

}  // namespace emthrm
Back to top page