sirkit-cpp-library

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

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

接頭辞で比率を保つ2つの数列の数え上げ

Math/Prefix_Dominant_Compositions.cpp · ソース · 文書 Last Commit

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

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

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

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

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

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

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

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

prefix_dominant_composition_counts<Mod>(max_length, p, q) は、長さごとに非負整数列 A, B の組数を返します。全体の和がそれぞれ p, q で、どの接頭辞でも $q\sum A\ge p\sum B$ を満たす組を数えます。

  • p, q は正の互いに素な uint64_t です。法 Mod は素数で、既定値は $998244353$ です。
  • 結果の添字は長さを表し、0番目は0です。max_length < Mod が必要です。
  • 構築は $O(\text{max_length}+1)$ 時間・領域です。

たとえば p=q=1 のとき、A の1が B の1より先か同じ位置にあればよいので、長さ1、2、3の答えは順に1、3、6です。

使用例 / Verify / Cycle Lemma

ソースのAPI注記
prefix_dominant_composition_counts<Mod=998244353>(max_length,p,q):
長さn=0..max_lengthでΣA=p、ΣB=qかつ全ての接頭辞でqΣA>=pΣBとなる非負整数列の組数を法Modで返す。
p,qは正の互いに素なuint64_t。Modは素数、max_length<Mod。長さ0では0通り。
O(max_length+1)時間・領域。p,qが不正ならinvalid_argument、長さの上限超過はlength_error。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

問題 プログラム 現行ソースの状態 提出結果
Prefix Advantage prefix_dominant_compositions_yuki3480.cpp 未提出 —

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

ローカル・CI検査

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

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

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

Verified with

Code

// SPDX-License-Identifier: CC0-1.0
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/prefix_dominant_compositions.md
// 参考(巡回シフトによる数え上げ): https://info.atcoder.jp/entry/algorithm_lectures/cycle_lemma
//
// API一覧
// prefix_dominant_composition_counts<Mod=998244353>(max_length,p,q):
// 長さn=0..max_lengthでΣA=p、ΣB=qかつ全ての接頭辞でqΣA>=pΣBとなる非負整数列の組数を法Modで返す。
// p,qは正の互いに素なuint64_t。Modは素数、max_length<Mod。長さ0では0通り。
// O(max_length+1)時間・領域。p,qが不正ならinvalid_argument、長さの上限超過はlength_error。


// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <cstddef>
#include <cstdint>
#include <numeric>
#include <stdexcept>
#include <vector>


// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_PREFIX_DOMINANT_COMPOSITIONS_DEFINED
#define SIRKIT_PREFIX_DOMINANT_COMPOSITIONS_DEFINED

namespace detail
{
consteval bool PrefixDominancePrime(std::uint32_t modulus)
{
    if (modulus < 2)
    {
        return false;
    }
    for (std::uint32_t factor = 2; factor <= modulus / factor; ++factor)
    {
        if (modulus % factor == 0)
        {
            return false;
        }
    }
    return true;
}
} // namespace detail

template <std::uint32_t Mod = 998244353>
[[nodiscard]] std::vector<std::uint32_t> prefix_dominant_composition_counts(
    std::size_t max_length, std::uint64_t p, std::uint64_t q)
{
    static_assert(detail::PrefixDominancePrime(Mod), "prefix_dominant_composition_counts: Mod must be prime");
    if (p == 0 || q == 0 || std::gcd(p, q) != 1)
    {
        throw std::invalid_argument("prefix_dominant_composition_counts: positive coprime totals required");
    }
    if (max_length >= Mod)
    {
        throw std::length_error("prefix_dominant_composition_counts: length must be below Mod");
    }

    std::vector<std::uint32_t> count(max_length + 1);
    if (max_length == 0)
    {
        return count;
    }
    count[1] = 1;
    for (std::size_t i = 2; i <= max_length; ++i)
    {
        count[i] = Mod - static_cast<std::uint32_t>(std::uint64_t{Mod} / i * count[Mod % i] % Mod);
    }

    // 長さnの答えの増分は C(p+n-1,n-1)*C(q+n-1,n-1)/n。
    // (0,0)の列を除いた巡回シフトの代表を数え、零列を戻す二項変換で得る。
    const auto reduced_p = p % Mod;
    const auto reduced_q = q % Mod;
    std::uint64_t compositions = 1, prefix = 0;
    for (std::size_t length = 1; length <= max_length; ++length)
    {
        const auto inverse = count[length];
        prefix = (prefix + compositions * inverse % Mod) % Mod;
        count[length] = static_cast<std::uint32_t>(prefix);
        compositions = compositions * ((reduced_p + length) % Mod) % Mod * inverse % Mod;
        compositions = compositions * ((reduced_q + length) % Mod) % Mod * inverse % Mod;
    }
    return count;
}

#endif

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