ku-library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub kogetsu0728/ku-library

:heavy_check_mark: Extended Euclidean Algorithm (拡張ユークリッドの互除法) (math/extended_gcd.hpp)

Required by

Verified with

Code

#pragma once

/**
 * @brief Extended Euclidean Algorithm (拡張ユークリッドの互除法)
 * @note 参考: https://qiita.com/drken/items/b97ff231e43bce50199a
 */
template <class T>
T ext_gcd(T a, T b, T& x, T& y) {
    if (b == T(0)) {
        x = T(1);
        y = T(0);

        return a;
    }

    T res = ext_gcd(b, a % b, y, x);
    y -= (a / b) * x;

    return res;
}
#line 2 "math/extended_gcd.hpp"

/**
 * @brief Extended Euclidean Algorithm (拡張ユークリッドの互除法)
 * @note 参考: https://qiita.com/drken/items/b97ff231e43bce50199a
 */
template <class T>
T ext_gcd(T a, T b, T& x, T& y) {
    if (b == T(0)) {
        x = T(1);
        y = T(0);

        return a;
    }

    T res = ext_gcd(b, a % b, y, x);
    y -= (a / b) * x;

    return res;
}
Back to top page