競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
大きなグリッドで、左上から右下へ右と下の移動だけで進む経路を数えます。グリッド全体を用意せず、壁の座標だけを使います。
count_sparse_grid_pathstemplate <typename Choose>
auto count_sparse_grid_paths(int height, int width,
std::span<const std::pair<int, int>> walls, Choose choose);
walls は0始まりの (行, 列) の列です。順不同で、同じ壁が複数あっても構いません。左上か右下に壁がある場合や、どちらかの寸法が0の場合、経路数は0です。
choose(n, k) は2個の std::int64_t を受け、二項係数を返す関数です。結果は choose と同じ型で、その型で0の構築、減算、乗算ができる必要があります。剰余がほしい場合は、二項係数も返却型の演算も同じ法で行います。
壁の数を $K$ として $O((K+1)^2)$ 時間・$O(K+1)$ 領域です。二項係数と返却型の演算は $O(1)$ とします。検査が有効なら、負の寸法は std::invalid_argument、グリッド外の壁は std::out_of_range になります。
const auto choose = [](std::int64_t n, std::int64_t k)
{
std::int64_t result = 1;
for (std::int64_t i = 1; i <= k; ++i)
{
result = result * (n - i + 1) / i;
}
return result;
};
const std::array walls{std::pair{1, 1}};
const auto paths = sirkit::count_sparse_grid_paths(3, 3, walls, choose);
assert(paths == 2);
count_sparse_grid_paths(height,width,walls,choose): 左上から右下へ、右・下移動で壁を避ける経路数。 wallsは0始まりの(行,列)。順不同・重複可。空グリッドや両端のどちらかが壁なら0。 choose(n,k)はint64_tの添字を受け、その返却型で二項係数を返す。演算は0の構築・乗算・減算。 壁の数Kに対してO((K+1)^2)時間・O(K+1)領域。二項係数と返却型の演算をO(1)とする。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Grid 2 | sparse_obstacle_grid_paths.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// 参考(疎な障害物の用途): https://atcoder.jp/contests/dp/tasks/dp_y
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/sparse_obstacle_grid_paths.md
//
// API一覧
// count_sparse_grid_paths(height,width,walls,choose): 左上から右下へ、右・下移動で壁を避ける経路数。
// wallsは0始まりの(行,列)。順不同・重複可。空グリッドや両端のどちらかが壁なら0。
// choose(n,k)はint64_tの添字を受け、その返却型で二項係数を返す。演算は0の構築・乗算・減算。
// 壁の数Kに対してO((K+1)^2)時間・O(K+1)領域。二項係数と返却型の演算をO(1)とする。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <span>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_SPARSE_OBSTACLE_GRID_PATHS_DEFINED
#define SIRKIT_SPARSE_OBSTACLE_GRID_PATHS_DEFINED
template <typename Choose>
[[nodiscard]] auto count_sparse_grid_paths(
int height, int width, std::span<const std::pair<int, int>> walls, Choose choose)
-> std::remove_cvref_t<std::invoke_result_t<Choose &, std::int64_t, std::int64_t>>
{
using Ways = std::remove_cvref_t<std::invoke_result_t<Choose &, std::int64_t, std::int64_t>>;
#ifndef SKIP_RUNTIME_CHECKS
if (height < 0 || width < 0)
{
throw std::invalid_argument("count_sparse_grid_paths: negative dimension");
}
for (const auto &[row, col] : walls)
{
if (row < 0 || row >= height || col < 0 || col >= width)
{
throw std::out_of_range("count_sparse_grid_paths: wall outside grid");
}
}
#endif
if (height == 0 || width == 0)
{
return Ways{0};
}
const std::pair start{0, 0};
const std::pair finish{height - 1, width - 1};
std::vector points(walls.begin(), walls.end());
std::ranges::sort(points);
points.erase(std::unique(points.begin(), points.end()), points.end());
if (!points.empty() && (points.front() == start || points.back() == finish))
{
return Ways{0};
}
points.push_back(finish);
std::vector<Ways> leftmost_hit;
leftmost_hit.reserve(points.size());
for (std::size_t i = 0; i < points.size(); ++i)
{
const auto [row, col] = points[i];
Ways ways = std::invoke(choose, static_cast<std::int64_t>(row) + col, static_cast<std::int64_t>(row));
// この点より先に通る壁ごとに、その壁を最初に踏む経路を引く。
for (std::size_t j = 0; j < i; ++j)
{
const auto [prior_row, prior_col] = points[j];
if (prior_col > col)
{
continue;
}
const auto dr = static_cast<std::int64_t>(row) - prior_row;
const auto dc = static_cast<std::int64_t>(col) - prior_col;
ways = ways - leftmost_hit[j] * std::invoke(choose, dr + dc, dr);
}
leftmost_hit.push_back(std::move(ways));
}
return std::move(leftmost_hit.back());
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1