競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
$y_i=\lfloor(ai+b)/m\rfloor$ でできる階段を左から進み、横と縦の1マスに対応する値を順に掛けます。文字列の連結、行列の積、写像の合成など、順序で結果が変わる演算にも使えます。
floor_path_producttemplate <class T, class Combine>
T floor_path_product(std::uint32_t n, std::uint32_t m, std::uint64_t a, std::uint64_t b,
const T &horizontal, const T &vertical, const T &identity, const Combine &combine)
最初に vertical を $y_0$ 回掛けます。続けて $i=0,\ldots,n-1$ の順に horizontal を1回、vertical を $y_{i+1}-y_i$ 回掛けます。combine(left,right) には結合則が必要です。identity は単位元です。
m は正、a と b は非負です。m=0 は std::invalid_argument です。n=0 なら最初の縦移動だけを集計します。combine は $O(\log(n+1)+\log(m+1)+\log(a+1)+\log(b+1))$ 回です。領域は $O(\log(m+1))$ 個の T です。
const auto join = [](const std::string &s, const std::string &t) { return s + t; };
const auto path = sirkit::floor_path_product(3, 5, 2, 4, std::string("X"), std::string("Y"), std::string(), join);
// "XYXXY"
floor_path_product(n,m,a,b,horizontal,vertical,identity,combine): y_i=floor((a*i+b)/m)とし、vertical^y_0 と各 horizontal vertical^(y_(i+1)-y_i) (0<=i<n) の積。 n,mはuint32_t、m>0。a,bは非負uint64_t。combineの結合則とidentityが必要。可換性は不要。 O(log(n+1)+log(m+1)+log(a+1)+log(b+1))回のcombine、O(log(m+1))領域。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| ABC429 G - Sum of Pow of Mod of Linear | floor_path_product_abc429_g.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// 参考(floor sumのモノイド積): https://atcoder.jp/contests/arc183/editorial/10768
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/floor_path_product.md
//
// API一覧
// floor_path_product(n,m,a,b,horizontal,vertical,identity,combine):
// y_i=floor((a*i+b)/m)とし、vertical^y_0 と各 horizontal vertical^(y_(i+1)-y_i) (0<=i<n) の積。
// n,mはuint32_t、m>0。a,bは非負uint64_t。combineの結合則とidentityが必要。可換性は不要。
// O(log(n+1)+log(m+1)+log(a+1)+log(b+1))回のcombine、O(log(m+1))領域。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <cstdint>
#include <stdexcept>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_FLOOR_PATH_PRODUCT_DEFINED
#define SIRKIT_FLOOR_PATH_PRODUCT_DEFINED
namespace detail
{
template <class T, class Combine>
T FloorWordPower(T base, std::uint64_t exponent, const T &identity, const Combine &combine)
{
T result = identity;
for (; exponent != 0; exponent >>= 1)
{
if ((exponent & 1U) != 0)
{
result = combine(result, base);
}
if (exponent > 1)
{
base = combine(base, base);
}
}
return result;
}
template <class T, class Combine>
T FloorWordProduct(std::uint64_t n, std::uint64_t m, std::uint64_t a, std::uint64_t b,
T horizontal, const T &vertical, const T &identity, const Combine &combine)
{
const auto initial = FloorWordPower(vertical, b / m, identity, combine);
if (n == 0)
{
return initial;
}
horizontal = combine(horizontal, FloorWordPower(vertical, a / m, identity, combine));
a %= m;
b %= m;
const auto height = (a * n + b) / m;
if (height == 0)
{
return combine(initial, FloorWordPower(horizontal, n, identity, combine));
}
// j番目の上移動の直前に横移動をceil((m*j-b)/a)回終えている。
const auto transposed = FloorWordProduct(height - 1, a, m, m + a - 1 - b,
vertical, horizontal, identity, combine);
const auto last_column = (m * height + a - 1 - b) / a;
return combine(initial, combine(transposed, combine(vertical, FloorWordPower(horizontal, n - last_column, identity, combine))));
}
}
template <class T, class Combine>
T floor_path_product(std::uint32_t n, std::uint32_t m, std::uint64_t a, std::uint64_t b,
const T &horizontal, const T &vertical, const T &identity, const Combine &combine)
{
if (m == 0)
{
throw std::invalid_argument("floor_path_product: positive modulus required");
}
return detail::FloorWordProduct(n, m, a, b, horizontal, vertical, identity, combine);
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1