This documentation is automatically generated by online-judge-tools/verification-helper
#include "math/extgcd.hpp"
与えられた$a, b$に対して, 最大公約数$gcd(a, b)$と, $ax + by = gcd(a, b)$なる$x, y$を求める.
$O(\log a)$
extgcd(a, b, x, y)
: $a$と$b$に対して拡張ユークリッドの互除法を行う.
x
, y
に格納される./**
* @brief 拡張ユークリッドの互除法
* @docs docs/math/extgcd.md
*/
long long extgcd(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = extgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
#line 1 "math/extgcd.hpp"
/**
* @brief 拡張ユークリッドの互除法
* @docs docs/math/extgcd.md
*/
long long extgcd(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = extgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}