This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "nt/extgcd.hpp"
#pragma once
#include "prelude.hpp"
// Returns (g, x, y) with ax + by = g, 0 <= x < |b|/g
template <class T>
tuple<T, T, T> extgcd(T a0, T b0) {
// a - a0 x - b0 y = 0
// b - a0 u - b0 v = 0
T a = a0, b = b0;
T x = 1, u = 0;
while (b) {
T q = a / b;
swap(a -= q * b, b);
swap(x -= q * u, u);
}
if (a < 0) a = -a, x = -x;
if (x < 0) x += abs(b0) / a;
T y = b0 ? (a - (__int128)a0 * x) / b0 : 0;
return {a, x, y};
}
#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "nt/extgcd.hpp"
// Returns (g, x, y) with ax + by = g, 0 <= x < |b|/g
template <class T>
tuple<T, T, T> extgcd(T a0, T b0) {
// a - a0 x - b0 y = 0
// b - a0 u - b0 v = 0
T a = a0, b = b0;
T x = 1, u = 0;
while (b) {
T q = a / b;
swap(a -= q * b, b);
swap(x -= q * u, u);
}
if (a < 0) a = -a, x = -x;
if (x < 0) x += abs(b0) / a;
T y = b0 ? (a - (__int128)a0 * x) / b0 : 0;
return {a, x, y};
}