This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "src/Math/binary_gcd.hpp"
除算の代わりにシフト演算と引き算を使う速い互除法.
constexpr で呼べる.
名前 | 概要 |
---|---|
binary_gcd(a,b) |
整数 $a,b$ の最大公約数を返す. |
#pragma once
#include <type_traits>
#include <algorithm>
#include <cstdint>
template <class Int> constexpr int bsf(Int a) {
if constexpr (sizeof(Int) == 16) {
uint64_t lo= a & uint64_t(-1);
return lo ? __builtin_ctzll(lo) : 64 + __builtin_ctzll(a >> 64);
} else if constexpr (sizeof(Int) == 8) return __builtin_ctzll(a);
else return __builtin_ctz(a);
}
template <class Int> constexpr Int binary_gcd(Int a, Int b) {
if (a == 0 || b == 0) return a + b;
int n= bsf(a), m= bsf(b), s= 0;
for (a>>= n, b>>= m; a != b;) {
Int d= a - b;
bool f= a > b;
s= bsf(d), b= f ? b : a, a= (f ? d : -d) >> s;
}
return a << std::min(n, m);
}
#line 2 "src/Math/binary_gcd.hpp"
#include <type_traits>
#include <algorithm>
#include <cstdint>
template <class Int> constexpr int bsf(Int a) {
if constexpr (sizeof(Int) == 16) {
uint64_t lo= a & uint64_t(-1);
return lo ? __builtin_ctzll(lo) : 64 + __builtin_ctzll(a >> 64);
} else if constexpr (sizeof(Int) == 8) return __builtin_ctzll(a);
else return __builtin_ctz(a);
}
template <class Int> constexpr Int binary_gcd(Int a, Int b) {
if (a == 0 || b == 0) return a + b;
int n= bsf(a), m= bsf(b), s= 0;
for (a>>= n, b>>= m; a != b;) {
Int d= a - b;
bool f= a > b;
s= bsf(d), b= f ? b : a, a= (f ? d : -d) >> s;
}
return a << std::min(n, m);
}