a01sa01to's competitive programming library. Requires C++20 or higher with GCC. This documentation is automatically generated by online-judge-tools/verification-helper
#include "library/_internal/math/modpow.hpp"
#pragma once
#include <concepts>
using namespace std;
namespace asalib::_internal::math {
template<integral T>
T modpow(T a, T n, T mod) {
static_assert(sizeof(T) == 4 || sizeof(T) == 8, "T must be 32-bit or 64-bit integer type");
constexpr bool is_64bit = sizeof(T) == 8;
T res = 1;
while (n) {
if constexpr (is_64bit) {
if (n & 1) res = static_cast<__uint128_t>(res) * a % mod;
a = static_cast<__uint128_t>(a) * a % mod;
}
else {
if (n & 1) res = static_cast<unsigned long long>(res) * a % mod;
a = static_cast<unsigned long long>(a) * a % mod;
}
n >>= 1;
}
return res;
}
} // namespace asalib::_internal::math
#line 2 "library/_internal/math/modpow.hpp"
#include <concepts>
using namespace std;
namespace asalib::_internal::math {
template<integral T>
T modpow(T a, T n, T mod) {
static_assert(sizeof(T) == 4 || sizeof(T) == 8, "T must be 32-bit or 64-bit integer type");
constexpr bool is_64bit = sizeof(T) == 8;
T res = 1;
while (n) {
if constexpr (is_64bit) {
if (n & 1) res = static_cast<__uint128_t>(res) * a % mod;
a = static_cast<__uint128_t>(a) * a % mod;
}
else {
if (n & 1) res = static_cast<unsigned long long>(res) * a % mod;
a = static_cast<unsigned long long>(a) * a % mod;
}
n >>= 1;
}
return res;
}
} // namespace asalib::_internal::math