C++ Library for Competitive Programming
#include "emthrm/math/twelvefold_way/partition_function.hpp"
自然数 $n$ を $m$ 個以下の正の整数の和で表す方法の総数の内、$n = m$ を満たすもの。
和の順序は問わず、$2 + 1 + 1$ と $1 + 2 + 1$ を区別しない。
分割数 $p(n)$ の母関数は
\[\sum_{n = 0}^\infty p(n) x^n = \prod_{n = 1}^{\infty} \dfrac{1}{1 - x^n}\]である。
時間計算量 | |
---|---|
$O(NM)$ | |
$n = m$ 版 | $O(N\log{N})$ |
名前 | 戻り値 |
---|---|
template <typename T> std::vector<std::vector<T>> partition_function(const int n, const int m);
|
分割数の数表 |
名前 | 戻り値 |
---|---|
template <typename T> std::vector<T> partition_function_by_fps(const int n);
|
$n = m$ のときの分割数の数表 |
#ifndef EMTHRM_MATH_TWELVEFOLD_WAY_PARTITION_FUNCTION_HPP_
#define EMTHRM_MATH_TWELVEFOLD_WAY_PARTITION_FUNCTION_HPP_
#include <algorithm>
#include <vector>
namespace emthrm {
template <typename T>
std::vector<std::vector<T>> partition_function(const int n, const int m) {
std::vector<std::vector<T>> p(n + 1, std::vector<T>(m + 1, 0));
p[0][0] = 1;
for (int i = 1; i <= n; ++i) {
std::copy(p[i - 1].begin(), p[i - 1].end(), p[i].begin());
for (int j = i; j <= m; ++j) {
p[i][j] += p[i][j - i];
}
}
return p;
}
} // namespace emthrm
#endif // EMTHRM_MATH_TWELVEFOLD_WAY_PARTITION_FUNCTION_HPP_
#line 1 "include/emthrm/math/twelvefold_way/partition_function.hpp"
#include <algorithm>
#include <vector>
namespace emthrm {
template <typename T>
std::vector<std::vector<T>> partition_function(const int n, const int m) {
std::vector<std::vector<T>> p(n + 1, std::vector<T>(m + 1, 0));
p[0][0] = 1;
for (int i = 1; i <= n; ++i) {
std::copy(p[i - 1].begin(), p[i - 1].end(), p[i].begin());
for (int j = i; j <= m; ++j) {
p[i][j] += p[i][j - i];
}
}
return p;
}
} // namespace emthrm