1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! 俗にいう一般的なModintです
use std::ops;

#[derive(Debug, Clone, Eq, PartialEq, Copy)]
#[allow(non_snake_case)]
pub struct Modint {
    /// どの値でmod を取るかの情報が入っています。
    pub MOD: usize,
    /// ここにModintの実体が入っています。結果を取り出すときはこの値を取り出すことになります  
    pub fact: usize,
}

impl Modint {
    /// first_modに剰余を取る値、initに初期値を渡してください  
    pub fn new(first_mod: usize, init: usize) -> Modint {
        Modint {
            MOD: first_mod,
            fact: init,
        }
    }
}

impl ops::Add for Modint {
    type Output = Modint;
    fn add(self, other: Self) -> Self {
        Modint::new(self.MOD, (self.fact + other.fact) % self.MOD)
    }
}
impl ops::Mul for Modint {
    type Output = Modint;
    fn mul(self, other: Self) -> Self {
        Modint::new(self.MOD, (self.fact * other.fact) % self.MOD)
    }
}
impl ops::Div for Modint {
    type Output = Modint;
    fn div(self, other: Self) -> Self {
        Modint::new(self.MOD, self.fact / other.fact)
    }
}
impl ops::Sub for Modint {
    type Output = Modint;
    fn sub(self, other: Self) -> Self {
        Modint::new(self.MOD, (self.MOD + self.fact + other.fact) % self.MOD)
    }
}
impl ops::AddAssign for Modint {
    fn add_assign(&mut self, other: Self) {
        *self = *self + other;
    }
}
impl ops::SubAssign for Modint {
    fn sub_assign(&mut self, other: Self) {
        *self = *self - other;
    }
}
impl ops::MulAssign for Modint {
    fn mul_assign(&mut self, other: Self) {
        *self = *self * other;
    }
}