JJ
template <std::size_t P>
struct Z
{
constexpr __host__ __device__ explicit Z(std::size_t x) noexcept:
x{ x % P }
{
}
[[nodiscard]] static constexpr __host__ __device__ std::size_t reverse(std::size_t x) noexcept
{
// https://e-maxx.ru/algo/reverse_element
constexpr auto r = []{
std::array<std::size_t, P> r{};
r[0] = 0;
r[1] = 1;
for (std::size_t i = 2; i < P; ++i)
r[i] = (P - (P / i) * r[P % i] % P) % P;
return r;
}();
return r[x];
}
std::size_t x{};
};