This documentation is automatically generated by online-judge-tools/verification-helper
#include "number/binary_gcd.hpp"二つの整数の最大公約数を求める高速なアルゴリズムとして知られる Binary GCD の実装.Euclid の互除法によるアルゴリズムと異なり,2 以外の除算が登場しない.
long long a, b;
long long g = binary_gcd(a, b);
#pragma once
#include <type_traits>
// CUT begin
template <typename Int> Int binary_gcd(Int x_, Int y_) {
using Uint = std::make_unsigned_t<Int>;
auto magnitude = [](Int v) -> Uint {
Uint u = static_cast<Uint>(v);
return v < 0 ? Uint(0) - u : u;
};
unsigned long long x = magnitude(x_), y = magnitude(y_);
if (!x or !y) return x + y;
int n = __builtin_ctzll(x), m = __builtin_ctzll(y);
x >>= n, y >>= m;
while (x != y) {
if (x > y) {
x = (x - y) >> __builtin_ctzll(x - y);
} else {
y = (y - x) >> __builtin_ctzll(y - x);
}
}
return x << (n > m ? m : n);
}#line 2 "number/binary_gcd.hpp"
#include <type_traits>
// CUT begin
template <typename Int> Int binary_gcd(Int x_, Int y_) {
using Uint = std::make_unsigned_t<Int>;
auto magnitude = [](Int v) -> Uint {
Uint u = static_cast<Uint>(v);
return v < 0 ? Uint(0) - u : u;
};
unsigned long long x = magnitude(x_), y = magnitude(y_);
if (!x or !y) return x + y;
int n = __builtin_ctzll(x), m = __builtin_ctzll(y);
x >>= n, y >>= m;
while (x != y) {
if (x > y) {
x = (x - y) >> __builtin_ctzll(x - y);
} else {
y = (y - x) >> __builtin_ctzll(y - x);
}
}
return x << (n > m ? m : n);
}