96namespace subset_sum_detail {
97template <std::
integral T>
100 if constexpr (std::is_signed_v<T>)
103 using UT = std::make_unsigned_t<T>;
105 if constexpr (
sizeof(
UT) >
sizeof(
size_t))
109 return static_cast<size_t>(
uvalue);
112template <std::
integral T>
116 for (
size_t i = 0; i < values.
size(); ++i)
127 <<
"subset_sum_mitm: each half must have fewer than 64 elements";
131 <<
"subset_sum_mitm: subset count does not fit size_t";
139 for (
size_t j = 0; j < len; ++j)
140 if (mask & (
static_cast<uint64_t>(1) << j))
141 s +=
static_cast<long long>(arr[start + j]);
142 result.
append(std::make_pair(s, mask));
165template <std::
integral T>
168 const size_t n = values.
size();
182 for (
size_t i = 0; i <= n; ++i)
185 for (
size_t s = 0; s <= tgt; ++s)
192 for (
size_t i = 1; i <= n; ++i)
194 const size_t vi = weights[i - 1];
195 for (
size_t s = 0; s <= tgt; ++s)
197 dp[i][s] = dp[i - 1][s];
198 if (
not dp[i][s]
and vi <= s
and dp[i - 1][s - vi])
209 for (
size_t i = n; i > 0
and s > 0; --i)
210 if (dp[i][s]
and not dp[i - 1][s])
219 for (
size_t k =
sel.size();
k > 0; --
k)
239template <std::
integral T>
242 const size_t n = values.
size();
253 for (
size_t s = 0; s <= tgt; ++s)
257 for (
size_t i = 0; i < n; ++i)
259 const size_t vi = weights[i];
260 for (
size_t s = tgt; s >= vi
and s !=
static_cast<size_t>(-1); --s)
281template <std::
integral T>
284 const size_t n = values.
size();
290 for (
size_t s = 0; s <= tgt; ++s)
294 for (
size_t i = 0; i < n; ++i)
296 const size_t vi = weights[i];
297 for (
size_t s = tgt; s >= vi
and s !=
static_cast<size_t>(-1); --s)
298 if (
const size_t count = dp[s - vi];
count > 0)
299 if (dp(s) > std::numeric_limits<size_t>::max() -
count)
300 dp(s) = std::numeric_limits<size_t>::max();
328template <std::
integral T>
331 const size_t n = values.
size();
336 const size_t half1 = n / 2;
344 [](
const auto &a,
const auto &b)
346 return a.first < b.first;
349 const auto target_ll =
static_cast<long long>(target);
350 for (
size_t i = 0; i <
sums1.size(); ++i)
355 size_t lo = 0, hi =
sums2.size();
358 const size_t mid = lo + (hi - lo) / 2;
372 for (
size_t j = 0; j <
half1; ++j)
376 for (
size_t j = 0; j <
half2; ++j)
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
size_t size_t int32_t value
Simple dynamic array with automatic resizing and functional operations.
static Array create(size_t n)
Create an array with n logical elements.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Array< size_t > extract_values_checked(const Array< T > &values, const char *fn_name)
Array< std::pair< long long, uint64_t > > enumerate_sums(const Array< T > &arr, size_t start, size_t len)
size_t to_size_checked(const T value, const char *fn_name, const char *field_name)
Main namespace for Aleph-w library functions.
bool subset_sum_exists(const Array< T > &values, T target)
Check if a subset summing to target exists (space-optimized).
size_t subset_sum_count(const Array< T > &values, T target)
Count the number of subsets that sum to target.
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T
Subset_Sum_Result< T > subset_sum_mitm(const Array< T > &values, T target)
Solve the subset sum problem via meet-in-the-middle (MITM).
Subset_Sum_Result< T > subset_sum(const Array< T > &values, T target)
Solve the subset sum problem via classical DP with reconstruction.
void introsort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using introsort (introspective sort).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Result of a subset sum computation.
Array< size_t > selected_indices
Indices (0-based) of the elements forming the subset.
bool exists
Whether a valid subset was found.
Dynamic array container with automatic resizing.
Comprehensive sorting algorithms and search utilities for Aleph-w.