GCD Monoid
(monoid/gcd.hpp)
- View this file on GitHub
- Last update: 2026-07-21 20:17:47+09:00
- Include:
#include "monoid/gcd.hpp"
Overview
A monoid representing the Greatest Common Divisor (GCD) operation. This is an idempotent monoid, meaning it can be safely used with a Sparse Table for $O(1)$ Range GCD queries.
Template Parameters
-
T: The underlying integer data type.
Properties
-
Operation:
std::gcd(a, b) - Identity Element: $0$ (Since $\gcd(x, 0) = x$)
Interface and Complexity
This is a stateless algebra tag. Generic data structures use its public
value_type, id(), and op(a, b) members. If the type also provides helpers
such as make(...) or inv(x), they are described above or in the documented
properties.
Each static operation runs in the cost of the underlying operation shown in the
properties. Scalar monoids are $O(1)$; monoids whose value_type stores several
items, permutations, or matrices scale with that stored size.
Verified with
Code
#ifndef M1UNE_MONOID_GCD_HPP
#define M1UNE_MONOID_GCD_HPP 1
#include <numeric>
namespace m1une {
namespace monoid {
// Monoid for Greatest Common Divisor (Range GCD).
template <typename T>
struct Gcd {
using value_type = T;
static constexpr bool commutative = true;
// The identity element for GCD is 0.
static constexpr T id() {
return T(0);
}
// Returns the greatest common divisor of a and b.
static constexpr T op(const T& a, const T& b) {
return std::gcd(a, b);
}
};
} // namespace monoid
} // namespace m1une
#endif // M1UNE_MONOID_GCD_HPP#line 1 "monoid/gcd.hpp"
#include <numeric>
namespace m1une {
namespace monoid {
// Monoid for Greatest Common Divisor (Range GCD).
template <typename T>
struct Gcd {
using value_type = T;
static constexpr bool commutative = true;
// The identity element for GCD is 0.
static constexpr T id() {
return T(0);
}
// Returns the greatest common divisor of a and b.
static constexpr T op(const T& a, const T& b) {
return std::gcd(a, b);
}
};
} // namespace monoid
} // namespace m1une