sirkit-cpp-library

競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。

View the Project on GitHub reinsirk/sirkit-cpp-library

同じ要素数の部分集合マスク

Algorithm/next_combination.cpp · ソース · 文書 Last Commit

通常版・コンパイラ共通 · この版の一覧へ

通常版・GCC専用 · この版の一覧へ

通常版・Clang専用 · この版の一覧へ

通常版・MSVC専用 · この版の一覧へ

速度優先版・コンパイラ共通 · この版の一覧へ

速度優先版・GCC専用 · この版の一覧へ

速度優先版・Clang専用 · この版の一覧へ

速度優先版・MSVC専用 · この版の一覧へ

符号なし整数の1ビットを1要素とし、同じ個数を選ぶ組合せをマスクの数値昇順に列挙します。UIntの既定値はstd::uint64_tです。

API

next_combination

template<std::unsigned_integral UInt>
constexpr UInt next_combination(UInt mask) noexcept;
constexpr int next_combination(int mask);
template<std::unsigned_integral UInt>
constexpr bool next_combination(UInt& mask, int n);

1引数版は1の個数が同じ次のマスクを返します。入力が0か、次のマスクがなければ0を返します。幅指定版は下位nビット内で次のマスクに更新します。次がなければfalseを返し、maskは変えません。

制約: UIntはbool以外。int版は非負。幅指定版は0<=n<=型幅、maskが下位nビット内。

計算量: O(1)時間・追加領域。

first_combination

template<std::unsigned_integral UInt = std::uint64_t>
constexpr UInt first_combination(int k);

下位kビットが1の最小マスクを返します。

制約: UIntはbool以外、0<=k<=型幅。

計算量: O(1)時間・追加領域。

last_combination

template<std::unsigned_integral UInt = std::uint64_t>
constexpr UInt last_combination(int n, int k);

下位nビット内で1がk個の最大マスクを返します。

制約: UIntはbool以外、0<=k<=n<=型幅。

計算量: O(1)時間・追加領域。

for_each_combination

template<std::unsigned_integral UInt = std::uint64_t, class Visitor>
constexpr void for_each_combination(int n, int k, Visitor&& visit);

各マスクを値の小さい順にvisit(mask)へ渡します。k=0の場合もvisitを1回呼びます。

制約: UIntはbool以外、0<=k<=n<=型幅。callbackの例外は伝わります。

計算量: O(C(n,k))回のcallback・O(1)追加領域。callback自身の費用は別。

追加領域はO(1)、列挙のcallbackの費用は別です。callbackの例外は呼出元へ伝わります。

前提

0<=k<=n<=UIntの型幅です。1引数版にはint版もあり、maskは非負、次が正のintに収まらなければ0を返します。全幅シフトは起こしません。

入力検査はSKIP_RUNTIME_CHECKSで省けます。列挙回数の上限は設けないため、C(n,k)の大きさは利用側で確認します。

完全な使用例

ソースのAPI注記
[次のマスク]
next_combination(sub): 同じ1の個数で次に大きいマスク。0・型内に次がなければ0。O(1)。
boolを除くunsigned整数型に対応。既存int版は非負が前提で、正のintに収まらない次も0。

[幅指定・列挙]
first_combination<UInt=uint64_t>(k): 下位kビットが1の最小マスク。0<=k<=型幅。O(1)。
last_combination<UInt=uint64_t>(n,k): nビット内で1がk個の最大マスク。0<=k<=n<=型幅。O(1)。
next_combination(sub, n): 下位nビット内の次へsubを進める。次がなければfalseでsub不変。O(1)。
for_each_combination<UInt=uint64_t>(n,k,visit): nビット内のk個の1を持つ全マスクを昇順にvisit(mask)へ渡す。k=0も1回。
for_eachはC(n,k)回の呼出し・O(1)追加領域(visitの処理を除く)。入力検査・終端の計算は最初だけ行う。
n,k,maskの検査はSKIPで省く。全幅のシフト・0除算を避ける処理は常に維持する。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

問題 プログラム 現行ソースの状態 提出結果
組合せマスク全探索・幅端ローカル検査 combination_masks_local.cpp ローカル補足 —
Maximum Independent Set(組合せ列挙) coverage_next_combination_independent_set.cpp 現行未照合・要再提出 AC
E - Modulo MST next_combination.cpp 現行未照合・要再提出 AC

✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。

ローカル・CI検査

検査 状態
正常系 未実施
失敗系 未測定
Library Checker公式ケース 未測定
性能測定 未実施
ASan/UBSan 未実施
カバレッジ 未実施

到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。

実行条件・件数・測定結果 · 結果JSON

Verified with

Code

// SPDX-License-Identifier: CC0-1.0
// 参考: https://github.com/drken1215/algorithm/blob/master/Search/next_combination.cpp
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/combination_masks.md
//
// API一覧
// [次のマスク]
// next_combination(sub): 同じ1の個数で次に大きいマスク。0・型内に次がなければ0。O(1)。
// boolを除くunsigned整数型に対応。既存int版は非負が前提で、正のintに収まらない次も0。
//
// [幅指定・列挙]
// first_combination<UInt=uint64_t>(k): 下位kビットが1の最小マスク。0<=k<=型幅。O(1)。
// last_combination<UInt=uint64_t>(n,k): nビット内で1がk個の最大マスク。0<=k<=n<=型幅。O(1)。
// next_combination(sub, n): 下位nビット内の次へsubを進める。次がなければfalseでsub不変。O(1)。
// for_each_combination<UInt=uint64_t>(n,k,visit): nビット内のk個の1を持つ全マスクを昇順にvisit(mask)へ渡す。k=0も1回。
// for_eachはC(n,k)回の呼出し・O(1)追加領域(visitの処理を除く)。入力検査・終端の計算は最初だけ行う。
// n,k,maskの検査はSKIPで省く。全幅のシフト・0除算を避ける処理は常に維持する。


// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <bit>
#include <concepts>
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <type_traits>


// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_NEXT_COMBINATION_DEFINED
#define SIRKIT_NEXT_COMBINATION_DEFINED
template <std::unsigned_integral UInt> requires (!std::same_as<UInt, bool>)
constexpr UInt next_combination(UInt sub) noexcept {
    if (sub == 0) return 0;
    const UInt low = static_cast<UInt>(sub & static_cast<UInt>(UInt{0} - sub));
    const UInt high = static_cast<UInt>(sub + low);
    if (high == 0) return 0;
    const UInt tail = static_cast<UInt>(static_cast<UInt>((sub ^ high) >> 2) >> std::countr_zero(sub));
    return static_cast<UInt>(high | tail);
}
inline constexpr int next_combination(int sub) {
#ifndef SKIP_RUNTIME_CHECKS
    if (sub < 0) throw std::out_of_range("next_combination: negative mask");
#endif
    const auto next = next_combination(static_cast<unsigned int>(sub));
    return next > static_cast<unsigned int>(std::numeric_limits<int>::max()) ? 0 : static_cast<int>(next);
}
#endif

#ifndef SIRKIT_COMBINATION_MASK_UTILITIES_DEFINED
#define SIRKIT_COMBINATION_MASK_UTILITIES_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ {
template <std::unsigned_integral UInt> requires (!std::same_as<UInt, bool>)
constexpr bool NextCombinationBounded(UInt& sub, int n) noexcept {
    constexpr int width = std::numeric_limits<UInt>::digits;
    const auto next = next_combination(sub);
    if (next == 0) return false;
#ifdef SKIP_RUNTIME_CHECKS
    if (n <= 0) return false;
#endif
    if (n < width && (next >> n) != 0) return false;
    sub = next;
    return true;
}
} // namespace reinsirk::detail
template <std::unsigned_integral UInt = std::uint64_t> requires (!std::same_as<UInt, bool>)
constexpr UInt first_combination(int k) {
    constexpr int width = std::numeric_limits<UInt>::digits;
#ifndef SKIP_RUNTIME_CHECKS
    if (k < 0 || k > width) throw std::out_of_range("first_combination: invalid count");
#endif
    if (k <= 0) return 0;
    if (k >= width) return std::numeric_limits<UInt>::max();
    return static_cast<UInt>((UInt{1} << k) - UInt{1});
}
template <std::unsigned_integral UInt = std::uint64_t> requires (!std::same_as<UInt, bool>)
constexpr UInt last_combination(int n, int k) {
#ifndef SKIP_RUNTIME_CHECKS
    constexpr int width = std::numeric_limits<UInt>::digits;
    if (n < 0 || n > width || k < 0 || k > n)
        throw std::out_of_range("last_combination: invalid width or count");
#endif
    if (k <= 0) return 0;
    return static_cast<UInt>(first_combination<UInt>(k) << (n - k));
}
template <std::unsigned_integral UInt> requires (!std::same_as<UInt, bool>)
constexpr bool next_combination(UInt& sub, int n) {
#ifndef SKIP_RUNTIME_CHECKS
    constexpr int width = std::numeric_limits<UInt>::digits;
    if (n < 0 || n > width || (n < width && (sub >> n) != 0))
        throw std::out_of_range("next_combination: invalid width or mask");
#endif
    return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::NextCombinationBounded(sub, n);
}
template <std::unsigned_integral UInt = std::uint64_t, class Visitor> requires (!std::same_as<UInt, bool>)
constexpr void for_each_combination(int n, int k, Visitor&& visit) {
#ifndef SKIP_RUNTIME_CHECKS
    constexpr int width = std::numeric_limits<UInt>::digits;
    if (n < 0 || n > width || k < 0 || k > n)
        throw std::out_of_range("for_each_combination: invalid width or count");
#else
    if (n < 0 || k < 0 || k > n) return;
#endif
    UInt mask = first_combination<UInt>(k);
    const UInt last = k == 0 ? UInt{0} : static_cast<UInt>(mask << (n - k));
    for (;;) {
        visit(UInt{mask});
        if (mask == last) break;
        if (k == 1) mask = static_cast<UInt>(mask << 1);
        else {
            // 終端以外は次が型内に存在し、k>=2なのでシフト量も型幅未満。
            const UInt low = static_cast<UInt>(mask & static_cast<UInt>(UInt{0} - mask));
            const UInt high = static_cast<UInt>(mask + low);
            const UInt tail = static_cast<UInt>((mask ^ high) >> (std::countr_zero(mask) + 2));
            mask = static_cast<UInt>(high | tail);
        }
    }
}
#endif

} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
Back to top page