45 for (
int i = 0; i < r; ++i) {
55 r = std::min(r, n - r);
57 for (
int i = 0; i < r; ++i) {
68 r = std::min(r, n - r);
69 std::vector<int64_t> dp(r + 1, 0);
71 for (
int i = 1; i <= n; ++i) {
72 for (
int j = std::min(i, r); j > 0; --j) {
73 dp[j] = dp[j] + dp[j - 1];
84 for (
int i = 1; i <= n; ++i) {
92 std::vector<std::vector<int>> result;
93 std::vector<int> partition;
94 generatePartitions(n, n, partition, result);
100 std::vector<int64_t> bell(n + 1, 0);
102 for (
int i = 1; i <= n; ++i) {
103 for (
int j = 0; j < i; ++j) {
104 bell[i] += binom_coeff(i - 1, j) * bell[j];
114 if (n == 0 && k == 0)
116 if (k == 0 || k == n)
118 return k * stirling_num(n - 1, k) + stirling_num(n - 1, k - 1);
123 std::vector<int> gray;
124 for (
int i = 0; i < (1 << n); ++i) {
125 gray.push_back(i ^ (i >> 1));
136 return (n - 1) * (subfactorial(n - 1) + subfactorial(n - 2));
144 std::vector<int64_t> dp(n + 1, 0);
146 for (
int i = 1; i <= n; ++i) {
147 for (
int j = i - 1; j >= 0; --j) {
152 for (
int i = n - k; i <= n; ++i) {
160 std::vector<std::vector<int>> result;
161 std::vector<int> partition;
162 gen_partition_distinct(n, n, partition, result);
168 std::vector<std::vector<int>> result;
169 std::vector<int> composition;
170 gen_compositions(n, n, composition, result);
176 return binom_coeff(2 * n, n) / (n + 1);
181 std::vector<std::string> dyck_words;
182 gen_dyck_words(
"", n, n, dyck_words);
192 for (
int d : divisors(n)) {
193 result += phi(n / d) * pow(k, d);
201 for (
int p = 2; p * p <= n; ++p) {
206 result -= result / p;
210 result -= result / n;
217 std::vector<std::vector<int>> necklaces;
218 std::vector<int> necklace(n, 0);
219 gen_necklaces(necklace, 0, n, k, necklaces);
226 for (
int d : divisors(n)) {
227 result += phi(n / d);
234 std::vector<std::string> lyndonWords;
235 gen_lyndon_words(
"", n, n, lyndonWords);
242 std::vector<int> &partition,
243 std::vector<std::vector<int>> &result) {
245 result.push_back(partition);
249 for (
int i = std::min(n, max); i >= 1; --i) {
250 partition.push_back(i);
251 generatePartitions(n - i, i, partition, result);
252 partition.pop_back();
259 std::vector<int> &partition,
260 std::vector<std::vector<int>> &result) {
262 result.push_back(partition);
266 for (
int i = std::min(n, max); i >= 1; --i) {
267 if (std::find(partition.begin(), partition.end(), i) ==
269 partition.push_back(i);
270 gen_partition_distinct(n - i, i, partition, result);
271 partition.pop_back();
279 std::vector<int> &composition,
280 std::vector<std::vector<int>> &result) {
282 result.push_back(composition);
286 for (
int i = std::min(n, max); i >= 1; --i) {
287 composition.push_back(i);
288 gen_compositions(n - i, i, composition, result);
289 composition.pop_back();
297 std::vector<std::string> &dyck_words) {
298 if (open == 0 && close == 0) {
299 dyck_words.push_back(prefix);
303 gen_dyck_words(prefix +
"(", open - 1, close, dyck_words);
306 gen_dyck_words(prefix +
")", open, close - 1, dyck_words);
315 std::vector<std::vector<int>> &necklaces) {
317 necklaces.push_back(necklace);
321 for (
int color = 1; color <= k; ++color) {
322 necklace[index] = color;
323 gen_necklaces(necklace, index + 1, n, k, necklaces);
331 std::vector<std::string> &lyndonWords) {
332 if (
static_cast<int>(prefix.size()) == n) {
333 lyndonWords.push_back(prefix);
337 for (
char c = (prefix.empty() ?
'a' : prefix.back() + 1);
338 c <=
'z' &&
static_cast<int>(prefix.size()) < n;
340 if (n % (
static_cast<int>(prefix.size()) + 1) == 0) {
341 gen_lyndon_words(prefix + c, n, prefix.size() + 1, lyndonWords);
343 gen_lyndon_words(prefix + c, n, max, lyndonWords);
350 std::vector<int> divs;
351 for (
int i = 1; i * i <= n; ++i) {
355 divs.push_back(n / i);
void gen_dyck_words(std::string prefix, int open, int close, std::vector< std::string > &dyck_words)
Generate Dyck words.
int64_t subfactorial(int n)
Calculate the subfactorial (!n) or derangement of n.
int phi(int n)
Calculate Euler's totient function (φ)
int64_t necklaces(int n, int k)
Calculate the number of necklaces of length n with k colors.
void gen_compositions(int n, int max, std::vector< int > &composition, std::vector< std::vector< int >> &result)
Generate compositions of a positive integer.
void gen_partition_distinct(int n, int max, std::vector< int > &partition, std::vector< std::vector< int >> &result)
Generate distinct partitions of a positive integer.
void generatePartitions(int n, int max, std::vector< int > &partition, std::vector< std::vector< int >> &result)
Generate partitions of a positive integer.
int64_t lyndonWords(int n)
Calculate the number of Lyndon words of length n.
std::vector< int64_t > bell_num(int n)
Calculate the Bell numbers.
int64_t dyck_words(int n)
Calculate the number of Dyck words of length 2n.
int64_t binom_coeff(int n, int r)
Calculate the binomial coefficient (n choose r)
void gen_necklaces(std::vector< int > &necklace, int index, int n, int k, std::vector< std::vector< int >> &necklaces)
Generate necklaces.
int64_t factorial(int n)
Calculate the factorial of a number.
std::vector< std::vector< int > > partitions(int n)
Generate all partitions of a positive integer n.
std::vector< int > gray_code(int n)
Generate the Gray code sequence of length n.
std::vector< std::vector< int > > partition_distinct(int n)
Generate all distinct partitions of a positive integer n.
std::vector< int > divisors(int n)
Calculate the divisors of a positive integer.
int64_t permutation(int n, int r)
Calculate the number of permutations (nPr)
int64_t stirling_num(int n, int k)
Calculate the Stirling number of the second kind.
int64_t combination(int n, int r)
Calculate the number of combinations (nCr)
std::vector< std::vector< int > > compositions(int n)
Generate all compositions of a positive integer n.
void gen_lyndon_words(std::string prefix, int n, int max, std::vector< std::string > &lyndonWords)
Generate Lyndon words.
int64_t subsequences(int n, int k)
Calculate the number of subsequences of length k from n items.
std::vector< std::string > generateLyndonWords(int n)
Generate all Lyndon words of length n.
std::vector< std::string > gen_dyck_words_until(int n)
Generate Dyck words up to length n.
std::vector< std::vector< int > > generateNecklaces(int n, int k)
Generate all necklaces of length n with k colors.