This documentation is automatically generated by online-judge-tools/verification-helper
#include "lib/others/exhaustive_search.hpp"
a の部分集合の和の組み合わせをすべて列挙します。
bit 全探索のサンプルコードです。
a の要素数を $N$ とすると、$\mathrm{O}(N 2^N)$
#pragma once
/**
* @brief Exhaustive Search (bit 全探索)
* @docs docs/others/exhaustive_search.md
*/
template<typename T>
vector<long long> sum_subsets(T a){
vector<long long> res;
int n = a.size();
for(int bit = 0; bit < (1 << n); bit++){
long long sum = 0;
for(int i = 0; i < n; i++){
int mask = 1 << i;
if(bit & mask){
sum += a[i];
}
}
res.emplace_back(sum);
}
return res;
}
#line 2 "lib/others/exhaustive_search.hpp"
/**
* @brief Exhaustive Search (bit 全探索)
* @docs docs/others/exhaustive_search.md
*/
template<typename T>
vector<long long> sum_subsets(T a){
vector<long long> res;
int n = a.size();
for(int bit = 0; bit < (1 << n); bit++){
long long sum = 0;
for(int i = 0; i < n; i++){
int mask = 1 << i;
if(bit & mask){
sum += a[i];
}
}
res.emplace_back(sum);
}
return res;
}