cp_library_rs/algebraic_structure/
operation.rs

1//! 基本的な演算
2
3use std::{fmt::Debug, marker::PhantomData, ops::Rem};
4
5use num_traits::Zero;
6
7/// 区間最小値
8#[derive(Debug)]
9pub struct Min<T>(PhantomData<T>);
10
11/// 最大値
12#[derive(Debug, Clone)]
13pub struct Max<T>(PhantomData<T>);
14
15/// 最小値,最大値を同時に管理
16#[derive(Debug, Clone)]
17pub struct MinMax<T>(PhantomData<T>);
18
19/// 和
20#[derive(Debug, Clone)]
21pub struct Add<T>(PhantomData<T>);
22
23/// overflow する和
24#[derive(Debug, Clone)]
25pub struct WrappingAdd;
26
27/// 積
28#[derive(Debug, Clone)]
29pub struct Mul<T>(PhantomData<T>);
30
31/// bit単位の排他的論理和
32#[derive(Debug, Clone)]
33pub struct Xor;
34
35/// 最大公約数
36#[derive(Debug)]
37pub struct GCD<T>(PhantomData<T>);
38impl<T: Clone + PartialEq + Zero + Rem<T, Output = T>> GCD<T> {
39    /// `a`,`b`の最大公約数を求める
40    pub fn gcd(a: &T, b: &T) -> T {
41        if b.is_zero() {
42            a.clone()
43        } else {
44            Self::gcd(b, &a.clone().rem(b.clone()))
45        }
46    }
47}