競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
floor_sum(n, m, a, b) は、i = 0, …, n-1 について floor((a*i+b)/m) を足した値を返します。例えば、各行の長さが一次式で変わる格子点の個数を数えられます。
assert(sirkit::floor_sum(4, 3, 2, 1) == 4); // 0 + 1 + 1 + 2
assert(sirkit::floor_sum(2, 3, -1, 0) == -1); // 0 + (-1)
assert(sirkit::min_of_mod_of_linear(4, 7, 2, 3) == 0); // 3, 5, 0, 2
floor_sumstd::int64_t floor_sum(std::int64_t n, std::int64_t m,
std::int64_t a, std::int64_t b);
0 ≤ n < 2^32、1 ≤ m < 2^32。負の a、b も指定でき、各項は負の無限大へ丸めます。和は 2^64 を法とし、符号付き64ビット整数で返します。時間は O(log(m+1))、追加領域は O(1) です。
min_of_mod_of_linearstd::int64_t min_of_mod_of_linear(std::int64_t n, std::int64_t m,
std::int64_t a, std::int64_t b);
i = 0, …, n-1 での (a*i+b) mod m の最小値を返します。余りは0以上 m 未満です。1 ≤ n < 2^32、1 ≤ m < 2^32 で、a と b は負でも構いません。時間は O(log(m+1))、追加領域は O(1) です。
使用例 / 和のVerify / 最小値のVerify
参考 AC Library の仕様 / Nyaan
floor_sum(n,m,a,b): i=0..n-1 の floor((a*i+b)/m) の総和。a,bは負でもよい。 0<=n<2^32、1<=m<2^32。ACLと同じく結果は2^64を法とする符号付き64ビット値。 min_of_mod_of_linear(n,m,a,b): i=0..n-1 の (a*i+b) mod m の最小値。1<=n<2^32。 両方ともO(log(m+1))時間・O(1)領域。検査有効時、n,mが範囲外ならstd::invalid_argument。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Sum of Floor of Linear | floor_sum.cpp | ✓ 現行AC | AC |
| Min of Mod of Linear | min_of_mod_of_linear.cpp | ✓ 現行AC | AC |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 未測定 |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// 参考: https://nyaannyaan.github.io/library/math/floor-sum.hpp
// API仕様: https://atcoder.github.io/ac-library/production/document_ja/math.html
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/floor_sum.md
//
// API一覧
// floor_sum(n,m,a,b): i=0..n-1 の floor((a*i+b)/m) の総和。a,bは負でもよい。
// 0<=n<2^32、1<=m<2^32。ACLと同じく結果は2^64を法とする符号付き64ビット値。
// min_of_mod_of_linear(n,m,a,b): i=0..n-1 の (a*i+b) mod m の最小値。1<=n<2^32。
// 両方ともO(log(m+1))時間・O(1)領域。検査有効時、n,mが範囲外ならstd::invalid_argument。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <bit>
#include <cstdint>
#include <stdexcept>
#include <utility>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_FLOOR_SUM_DEFINED
#define SIRKIT_FLOOR_SUM_DEFINED
inline std::int64_t floor_sum(std::int64_t n, std::int64_t m, std::int64_t a, std::int64_t b)
{
#ifndef SKIP_RUNTIME_CHECKS
constexpr std::int64_t kLimit = std::int64_t{1} << 32;
if (n < 0 || n >= kLimit || m <= 0 || m >= kLimit)
{
throw std::invalid_argument("floor_sum: n or m");
}
#endif
if (n == 0)
{
return 0;
}
const auto count = static_cast<std::uint64_t>(n);
const auto triangular = count * (count - 1) / 2;
std::uint64_t sum = 0;
if (a < 0)
{
const auto remainder = a % m;
const auto quotient = a / m;
sum += triangular * static_cast<std::uint64_t>(quotient - (remainder != 0));
a = remainder == 0 ? 0 : remainder + m;
}
if (b < 0)
{
const auto remainder = b % m;
const auto quotient = b / m;
sum += count * static_cast<std::uint64_t>(quotient - (remainder != 0));
b = remainder == 0 ? 0 : remainder + m;
}
auto length = count;
auto modulus = static_cast<std::uint64_t>(m);
auto slope = static_cast<std::uint64_t>(a);
auto offset = static_cast<std::uint64_t>(b);
while (true)
{
if (slope >= modulus)
{
sum += (length * (length - 1) / 2) * (slope / modulus);
slope %= modulus;
}
if (offset >= modulus)
{
sum += length * (offset / modulus);
offset %= modulus;
}
const auto limit = slope * length + offset;
if (limit < modulus)
{
break;
}
length = limit / modulus; // 格子点を横方向から数え直す。
offset = limit % modulus;
std::swap(modulus, slope);
}
return std::bit_cast<std::int64_t>(sum);
}
inline std::int64_t min_of_mod_of_linear(std::int64_t n, std::int64_t m, std::int64_t a, std::int64_t b)
{
#ifndef SKIP_RUNTIME_CHECKS
constexpr std::int64_t kLimit = std::int64_t{1} << 32;
if (n <= 0 || n >= kLimit || m <= 0 || m >= kLimit)
{
throw std::invalid_argument("min_of_mod_of_linear: n or m");
}
#endif
const auto normalize = [](std::int64_t x, std::int64_t modulus)
{
x %= modulus;
return static_cast<std::uint64_t>(x < 0 ? x + modulus : x);
};
auto length = static_cast<std::uint64_t>(n);
auto modulus = static_cast<std::uint64_t>(m);
auto slope = normalize(a, m);
auto offset = normalize(b, m);
auto minimum = offset;
while (minimum != 0 && slope != 0)
{
if (slope * 2 > modulus) // 逆から走査して次の法を半分以下にする。
{
offset = (slope * (length - 1) + offset) % modulus;
slope = modulus - slope;
}
minimum = std::min(minimum, offset);
const auto wraps = (slope * (length - 1) + offset) / modulus;
if (wraps == 0)
{
break;
}
const auto reverse_step = (slope - modulus % slope) % slope; // 次の法は slope。
length = wraps;
offset = (offset % slope + reverse_step) % slope; // y回目の折り返しは (offset - modulus*y) mod slope。
modulus = slope;
slope = reverse_step;
minimum = std::min(minimum, offset);
}
return static_cast<std::int64_t>(minimum);
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1