This documentation is automatically generated by online-judge-tools/verification-helper
#include "enumerate/partition.hpp"#include "enumerate/bits.hpp"
/*
partition は、「減少列」として辞書式の降順に列挙する。
N=10,20,30,40:42, 527, 5604, 37338
N = 50(204226):12 ms
N = 60(966467):60 ms
N = 70(4087968):270 ms
N = 80(15796476):1100 ms
N = 90(56634173):4800 ms
N = 100 (190569292) : 15600 ms
*/
template <typename F>
void enumerate_partition(int N, F query, int LIM_len = -1, int LIM_val = -1) {
assert(N >= 0);
auto dfs = [&](auto self, vc<int> &p, int sum) -> void {
if (sum == N) {
query(p);
return;
}
if (LIM_len != -1 && len(p) == LIM_len) return;
int nxt = (len(p) == 0 ? N : p.back());
if (LIM_val != -1) chmin(nxt, LIM_val);
chmin(nxt, N - sum);
p.eb(0);
FOR3_R(x, 1, nxt + 1) {
p.back() = x;
self(self, p, sum + x);
}
p.pop_back();
};
vc<int> p;
dfs(dfs, p, 0);
}
// N 元集合の分割の列挙 (Bell number)
// f({s0,s1,...}), f(vc<int>)
// https://atcoder.jp/contests/abc390/tasks/abc390_d
// N = 11(678570):29 ms
// N = 12(4213597):208 ms
// N = 13(27644437):2084 ms
template <typename F>
void enumerate_set_partition(int N, F f) {
vc<u32> S;
auto dfs = [&](auto &dfs, u32 rest) -> void {
if (rest == 0) {
return f(S);
}
int a = lowbit(rest);
rest -= u32(1) << a;
enumerate_all_subset<u32, true>(rest, [&](u32 s) -> void {
S.eb(s | 1 << a);
dfs(dfs, rest - s);
POP(S);
});
};
dfs(dfs, (u32(1) << N) - 1);
}#line 1 "other/bit.hpp"
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_sgn(int x) { return (__builtin_parity(unsigned(x)) & 1 ? -1 : 1); }
int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(ll x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
int popcnt_sgn(u64 x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
template <typename T>
T kth_bit(int k) {
return T(1) << k;
}
template <typename T>
bool has_kth_bit(T x, int k) {
return x >> k & 1;
}
template <typename UINT>
struct all_bit {
UINT s;
all_bit(UINT s) : s(s) {}
struct iter {
UINT s;
int operator*() const { return lowbit(s); }
void operator++() { s &= s - 1; }
bool operator!=(nullptr_t) const { return s; }
};
iter begin() const { return {s}; }
nullptr_t end() const { return nullptr; }
};
template <typename UINT>
struct all_subset {
UINT s;
all_subset(UINT s) : s(s) {}
struct iter {
UINT s, t;
bool done = false;
UINT operator*() const { return t; }
void operator++() {
done = (t == 0);
t = (t - 1) & s;
}
bool operator!=(nullptr_t) const { return !done; }
};
iter begin() const { return {s, s}; }
nullptr_t end() const { return nullptr; }
};
constexpr u64 full_mask(int n) { return n == 64 ? -1ULL : (1ULL << n) - 1; }
#line 2 "enumerate/bits.hpp"
template <typename BS, typename F>
void enumerate_bits_bitset(BS& b, int L, int R, F&& f) {
if (L >= len(b)) return;
int p = (b[L] ? L : b._Find_next(L));
while (p < R) {
f(p);
p = b._Find_next(p);
}
}
template <typename UINT, typename F>
inline void enumerate_all_bit(UINT s, F&& f) {
static_assert(is_unsigned<UINT>::value);
while (s) {
f(lowbit(s));
s &= s - 1;
}
}
template <typename UINT, bool inc_empty, typename F>
inline void enumerate_all_subset(UINT s, F&& f) {
static_assert(is_unsigned<UINT>::value);
for (UINT t = s; t; t = (t - 1) & s) f(t);
if constexpr (inc_empty) f(0);
}
#line 2 "enumerate/partition.hpp"
/*
partition は、「減少列」として辞書式の降順に列挙する。
N=10,20,30,40:42, 527, 5604, 37338
N = 50(204226):12 ms
N = 60(966467):60 ms
N = 70(4087968):270 ms
N = 80(15796476):1100 ms
N = 90(56634173):4800 ms
N = 100 (190569292) : 15600 ms
*/
template <typename F>
void enumerate_partition(int N, F query, int LIM_len = -1, int LIM_val = -1) {
assert(N >= 0);
auto dfs = [&](auto self, vc<int> &p, int sum) -> void {
if (sum == N) {
query(p);
return;
}
if (LIM_len != -1 && len(p) == LIM_len) return;
int nxt = (len(p) == 0 ? N : p.back());
if (LIM_val != -1) chmin(nxt, LIM_val);
chmin(nxt, N - sum);
p.eb(0);
FOR3_R(x, 1, nxt + 1) {
p.back() = x;
self(self, p, sum + x);
}
p.pop_back();
};
vc<int> p;
dfs(dfs, p, 0);
}
// N 元集合の分割の列挙 (Bell number)
// f({s0,s1,...}), f(vc<int>)
// https://atcoder.jp/contests/abc390/tasks/abc390_d
// N = 11(678570):29 ms
// N = 12(4213597):208 ms
// N = 13(27644437):2084 ms
template <typename F>
void enumerate_set_partition(int N, F f) {
vc<u32> S;
auto dfs = [&](auto &dfs, u32 rest) -> void {
if (rest == 0) {
return f(S);
}
int a = lowbit(rest);
rest -= u32(1) << a;
enumerate_all_subset<u32, true>(rest, [&](u32 s) -> void {
S.eb(s | 1 << a);
dfs(dfs, rest - s);
POP(S);
});
};
dfs(dfs, (u32(1) << N) - 1);
}