7#include "cplib/port/bit.hpp"
20template <
typename T, std::enable_if_t<std::is_
unsigned_v<T>>* =
nullptr>
21constexpr T
gcd(T x, T y) {
27 int kx = port::countr_zero(x);
28 int ky = port::countr_zero(y);
31 y >>= port::countr_zero(y);
37 return x << std::min(kx, ky);
57template <
typename T, std::enable_if_t<std::is_
unsigned_v<T>>* =
nullptr>
58constexpr std::tuple<std::make_signed_t<T>, std::make_signed_t<T>, T>
bezout(T x, T y) {
72 using S = std::make_signed_t<T>;
73 S s0 = 1, s1 = 0, t0 = 0, t1 = 1;
75 T q = x / y, r = x % y;
83 S s2 = s0 - S(q) * s1, t2 = t0 - S(q) * t1;
101template <
typename T, std::enable_if_t<std::is_
unsigned_v<T>>* =
nullptr>
103 auto [s, t, g] =
bezout(x, m);
105 return s < 0 ? T(s) + m : s;
constexpr T gcd(T x, T y)
Greatest common divisor.
Definition: gcd.hpp:21
constexpr T mod_inverse(T x, T m)
Modular inverse.
Definition: gcd.hpp:102
constexpr std::tuple< std::make_signed_t< T >, std::make_signed_t< T >, T > bezout(T x, T y)
Bézout coefficients, i.e. such that .
Definition: gcd.hpp:58