checked_pow
関数 · 整数ユーティリティ
constexpr std::optional<std::int64_t> checked_pow(std::int64_t n, std::int64_t k);
概要
オーバーフローを検出する累乗
$n^k$ が std::int64_t に収まるならその値を、収まらないなら std::nullopt を返す。 底が負でも扱え、$k = 0$ のときは $0^0 = 1$ を含めて $1$ を返す。
パラメータ
| 名前 | 説明 |
|---|---|
n |
底 |
k |
指数、0 <= k |
戻り値
収まるなら $n^k$、収まらないなら std::nullopt
計算量
- $O(\log k)$