-
Notifications
You must be signed in to change notification settings - Fork 0
/
mint.rs
260 lines (230 loc) · 6.51 KB
/
mint.rs
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#![allow(clippy::suspicious_arithmetic_impl)]
use std::fmt;
use std::marker::PhantomData;
use std::mem;
use std::ops;
/// Trait for `Mint`. `module()` should return prime number.
pub trait Module: Copy + Clone {
fn module() -> u32;
}
/// One of famous numbers in programming contest. `10^9 + 7`
pub const MOD_107: u32 = 1_000_000_007;
/// One of famous numbers in programming contest. `10^9 + 9`
pub const MOD_109: u32 = 1_000_000_009;
/// One of famous numbers in programming contest. `998_244_353`
pub const MOD_998: u32 = 998_244_353;
/// struct to implement Module trait. it returns `MOD_107`.
#[derive(Debug, Copy, Clone)]
pub struct Mod107;
impl Module for Mod107 {
fn module() -> u32 {
MOD_107
}
}
/// struct to implement Module trait. it returns `MOD_109`.
#[derive(Debug, Copy, Clone)]
pub struct Mod109;
impl Module for Mod109 {
fn module() -> u32 {
MOD_109
}
}
/// struct to implement Module trait. it returns `MOD_998`.
#[derive(Debug, Copy, Clone)]
pub struct Mod998;
impl Module for Mod998 {
fn module() -> u32 {
MOD_998
}
}
/// Wrapper class to compute mod `1_000_000_007` automatically.
///
/// # Examples
/// ```
/// use algorithms::math::{Mint107, MOD_107};
/// let x: Mint107 = 1234567.into();
/// let y: Mint107 = 2345678.into();
/// let z = x * y;
/// # // TODO: implement convert to u64
/// assert_eq!(z.val as u64, 1234567u64 * 2345678u64 % MOD_107 as u64);
/// ```
///
pub type Mint107 = Mint<Mod107>;
/// Wrapper class to compute mod `1_000_000_009` automatically.
///
/// # Examples
/// ```
/// use algorithms::math::{Mint109, MOD_109};
/// let x: Mint109 = 1234567.into();
/// let y: Mint109 = 2345678.into();
/// let z = x * y;
/// assert_eq!(z.val as u64, 1234567u64 * 2345678u64 % MOD_109 as u64);
/// ```
///
pub type Mint109 = Mint<Mod109>;
/// Wrapper class to compute mod `998_244_353` automatically.
///
/// # Examples
/// ```
/// use algorithms::math::{Mint998, MOD_998};
/// let x: Mint998 = 1234567.into();
/// let y: Mint998 = 2345678.into();
/// let z = x * y;
/// assert_eq!(z.val as u64, 1234567u64 * 2345678u64 % MOD_998 as u64);
/// ```
///
pub type Mint998 = Mint<Mod998>;
/// Wrapper class to compute modulo operation.
/// See examples
/// [`Mint107`](type.Mint107.html),
/// [`Mint109`](type.Mint109.html),
/// [`Mint998`](type.Mint998.html)
///
/// # Examples
/// ```
/// use algorithms::math::{Mint107, MOD_107};
/// let x: Mint107 = 1234567.into();
/// let y: Mint107 = 2345678.into();
/// let z = x * y;
/// assert_eq!(z.val as u64, 1234567u64 * 2345678u64 % MOD_107 as u64);
/// ```
#[derive(Debug, Copy, Clone, Eq)]
pub struct Mint<M: Module> {
/// internal value. this is always less than `self.module()`.
pub val: u32,
m: PhantomData<M>,
}
impl<M: Module> Mint<M> {
fn module(self) -> u32 {
M::module()
}
fn new(val: u32) -> Mint<M> {
assert!(val < M::module());
Mint {
val,
m: PhantomData,
}
}
}
impl<T: Into<Mint<M>>, M: Module> ops::Add<T> for Mint<M> {
type Output = Mint<M>;
fn add(self, other: T) -> Mint<M> {
let nval = self.val + other.into().val;
Mint::new(if nval >= self.module() {
nval - self.module()
} else {
nval
})
}
}
impl<T: Into<Mint<M>>, M: Module> ops::Sub<T> for Mint<M> {
type Output = Mint<M>;
fn sub(self, other: T) -> Mint<M> {
let nval = self.val + self.module() - other.into().val;
Mint::new(if nval >= self.module() {
nval - self.module()
} else {
nval
})
}
}
impl<T: Into<Mint<M>>, M: Module> ops::Mul<T> for Mint<M> {
type Output = Mint<M>;
fn mul(self, other: T) -> Mint<M> {
let nval = self.val as u64 * other.into().val as u64;
Mint::new((nval % (self.module() as u64)) as u32)
}
}
impl<T: Into<Mint<M>>, M: Module> ops::Div<T> for Mint<M> {
type Output = Mint<M>;
fn div(self, other: T) -> Mint<M> {
self * other.into().inv()
}
}
impl<M: Module> Mint<M> {
/// Returns number `y` that satisfies `x * y == 1` where `x` is the original value.
///
/// This assumes `module()` returns prime number.
pub fn inv(self) -> Mint<M> {
let mut a = self.val as i32;
let mut b = self.module() as i32;
let mut u = 1_i32;
let mut v = 0_i32;
while b != 0 {
let quo = a / b;
a -= quo * b;
mem::swap(&mut a, &mut b);
u -= quo * v;
mem::swap(&mut u, &mut v);
}
Mint::new(if u < 0 { u + self.module() as i32 } else { u } as u32)
}
}
impl<M: Module> PartialEq for Mint<M> {
fn eq(&self, other: &Mint<M>) -> bool {
self.val == other.val
}
}
impl<M: Module> fmt::Display for Mint<M> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.val.fmt(f)
}
}
macro_rules! impl_signed_mint {
($($t:ty)*) => ($(
impl<M: Module> From<$t> for Mint<M> {
#[inline]
fn from(x: $t) -> Mint<M> {
let t = (x as i64) % (M::module() as i64);
if x >= 0 {
Mint{ val: t as u32, m: PhantomData }
} else {
Mint{ val: (M::module() as i64 + t) as u32, m: PhantomData }
}
}
}
)*)
}
macro_rules! impl_unsigned_mint {
($($t:ty)*) => ($(
impl<M: Module> From<$t> for Mint<M> {
#[inline]
fn from(x: $t) -> Mint<M> {
let t = x as u64 % M::module() as u64;
Mint::new(t as u32)
}
}
)*)
}
impl_signed_mint! { i8 i16 i32 i64 isize }
impl_unsigned_mint! { u8 u16 u32 u64 usize }
impl<T: Into<Mint<M>>, M: Module> ops::AddAssign<T> for Mint<M> {
fn add_assign(&mut self, other: T) {
*self = *self + other.into();
}
}
impl<T: Into<Mint<M>>, M: Module> ops::SubAssign<T> for Mint<M> {
fn sub_assign(&mut self, other: T) {
*self = *self - other.into();
}
}
impl<T: Into<Mint<M>>, M: Module> ops::MulAssign<T> for Mint<M> {
fn mul_assign(&mut self, other: T) {
*self = *self * other.into();
}
}
impl<T: Into<Mint<M>>, M: Module> ops::DivAssign<T> for Mint<M> {
fn div_assign(&mut self, other: T) {
*self = *self / other.into();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simple() {
let a: Mint<Mod107> = Mint::from(3);
let b: Mint<Mod107> = Mint::from(1000000000);
assert_eq!(Mint::from(3000000000u64 % Mod107::module() as u64), a * b);
}
}