pub struct MInt<M>where
M: MIntBase,{ /* private fields */ }
Implementations§
Source§impl<M> MInt<M>where
M: MIntConvert<u32>,
impl<M> MInt<M>where
M: MIntConvert<u32>,
Sourcepub fn sqrt(self) -> Option<Self>
pub fn sqrt(self) -> Option<Self>
Examples found in repository?
More examples
crates/library_checker/src/math/sqrt_mod.rs (line 12)
6pub fn sqrt_mod(reader: impl Read, mut writer: impl Write) {
7 let s = read_all_unchecked(reader);
8 let mut scanner = Scanner::new(&s);
9 scan!(scanner, q, yp: [(u32, u32)]);
10 for (y, p) in yp.take(q) {
11 DynMIntU32::set_mod(p);
12 if let Some(x) = DynMIntU32::from(y).sqrt() {
13 writeln!(writer, "{}", x).ok();
14 } else {
15 writeln!(writer, "-1").ok();
16 }
17 }
18}
Source§impl<M> MInt<M>where
M: MIntConvert,
impl<M> MInt<M>where
M: MIntConvert,
Sourcepub fn new(x: M::Inner) -> Self
pub fn new(x: M::Inner) -> Self
Examples found in repository?
crates/library_checker/src/math/sum_of_totient_function.rs (line 19)
12pub fn sum_of_totient_function(reader: impl Read, mut writer: impl Write) {
13 let s = read_all_unchecked(reader);
14 let mut scanner = Scanner::new(&s);
15 scan!(scanner, n: u64);
16 let mut s = 1;
17 let mut pp = 0;
18 let mut pc = 0;
19 let inv2 = M::new(2).inv();
20 let qa = QuotientArray::from_fn(n, |i| [M::from(i), M::from(i) * M::from(i + 1) * inv2])
21 .map(|[x, y]| [x - M::one(), y - M::one()])
22 .lucy_dp::<ArrayOperation<AdditiveOperation<_>, 2>>(|[x, y], p| [x, y * M::from(p)])
23 .map(|[x, y]| y - x)
24 .min_25_sieve::<AddMulOperation<_>>(|p, c| {
25 if pp != p || pc > c {
26 pp = p;
27 pc = 1;
28 s = p - 1;
29 }
30 while pc < c {
31 pc += 1;
32 s *= p;
33 }
34 M::from(s)
35 });
36 writeln!(writer, "{}", qa[n]).ok();
37}
More examples
crates/competitive/src/math/number_theoretic_transform.rs (line 368)
367 fn inverse_transform(f: Self::F, len: usize) -> Self::T {
368 let t1 = MInt::<N2>::new(N1::get_mod()).inv();
369 let m1 = MInt::<M>::from(N1::get_mod());
370 let m1_3 = MInt::<N3>::new(N1::get_mod());
371 let t2 = (m1_3 * MInt::<N3>::new(N2::get_mod())).inv();
372 let m2 = m1 * MInt::<M>::from(N2::get_mod());
373 Convolve::<N1>::inverse_transform(f.0, len)
374 .into_iter()
375 .zip(Convolve::<N2>::inverse_transform(f.1, len))
376 .zip(Convolve::<N3>::inverse_transform(f.2, len))
377 .map(|((c1, c2), c3)| {
378 let d1 = c1.inner();
379 let d2 = ((c2 - MInt::<N2>::from(d1)) * t1).inner();
380 let x = MInt::<N3>::new(d1) + MInt::<N3>::new(d2) * m1_3;
381 let d3 = ((c3 - x) * t2).inner();
382 MInt::<M>::from(d1) + MInt::<M>::from(d2) * m1 + MInt::<M>::from(d3) * m2
383 })
384 .collect()
385 }
Source§impl<M> MInt<M>where
M: MIntBase,
impl<M> MInt<M>where
M: MIntBase,
Sourcepub const fn new_unchecked(x: M::Inner) -> Self
pub const fn new_unchecked(x: M::Inner) -> Self
Examples found in repository?
crates/competitive/src/num/mint/mint_base.rs (line 60)
59 pub fn new(x: M::Inner) -> Self {
60 Self::new_unchecked(<M as MIntConvert<M::Inner>>::from(x))
61 }
62}
63impl<M> MInt<M>
64where
65 M: MIntBase,
66{
67 #[inline]
68 pub const fn new_unchecked(x: M::Inner) -> Self {
69 Self {
70 x,
71 _marker: PhantomData,
72 }
73 }
74 #[inline]
75 pub fn get_mod() -> M::Inner {
76 M::get_mod()
77 }
78 #[inline]
79 pub fn pow(self, y: usize) -> Self {
80 Self::new_unchecked(M::mod_pow(self.x, y))
81 }
82 #[inline]
83 pub fn inv(self) -> Self {
84 Self::new_unchecked(M::mod_inv(self.x))
85 }
86 #[inline]
87 pub fn inner(self) -> M::Inner {
88 M::mod_inner(self.x)
89 }
90}
91
92impl<M> Clone for MInt<M>
93where
94 M: MIntBase,
95{
96 #[inline]
97 fn clone(&self) -> Self {
98 *self
99 }
100}
101impl<M> Copy for MInt<M> where M: MIntBase {}
102impl<M> Debug for MInt<M>
103where
104 M: MIntBase,
105{
106 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107 Debug::fmt(&self.inner(), f)
108 }
109}
110impl<M> Default for MInt<M>
111where
112 M: MIntBase,
113{
114 #[inline]
115 fn default() -> Self {
116 <Self as Zero>::zero()
117 }
118}
119impl<M> PartialEq for MInt<M>
120where
121 M: MIntBase,
122{
123 #[inline]
124 fn eq(&self, other: &Self) -> bool {
125 PartialEq::eq(&self.x, &other.x)
126 }
127}
128impl<M> Eq for MInt<M> where M: MIntBase {}
129impl<M> Hash for MInt<M>
130where
131 M: MIntBase,
132{
133 #[inline]
134 fn hash<H: Hasher>(&self, state: &mut H) {
135 Hash::hash(&self.x, state)
136 }
137}
138macro_rules! impl_mint_from {
139 ($($t:ty),*) => {
140 $(impl<M> From<$t> for MInt<M>
141 where
142 M: MIntConvert<$t>,
143 {
144 #[inline]
145 fn from(x: $t) -> Self {
146 Self::new_unchecked(<M as MIntConvert<$t>>::from(x))
147 }
148 }
149 impl<M> From<MInt<M>> for $t
150 where
151 M: MIntConvert<$t>,
152 {
153 #[inline]
154 fn from(x: MInt<M>) -> $t {
155 <M as MIntConvert<$t>>::into(x.x)
156 }
157 })*
158 };
159}
160impl_mint_from!(
161 u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize
162);
163impl<M> Zero for MInt<M>
164where
165 M: MIntBase,
166{
167 #[inline]
168 fn zero() -> Self {
169 Self::new_unchecked(M::mod_zero())
170 }
171}
172impl<M> One for MInt<M>
173where
174 M: MIntBase,
175{
176 #[inline]
177 fn one() -> Self {
178 Self::new_unchecked(M::mod_one())
179 }
180}
181
182impl<M> Add for MInt<M>
183where
184 M: MIntBase,
185{
186 type Output = Self;
187 #[inline]
188 fn add(self, rhs: Self) -> Self::Output {
189 Self::new_unchecked(M::mod_add(self.x, rhs.x))
190 }
191}
192impl<M> Sub for MInt<M>
193where
194 M: MIntBase,
195{
196 type Output = Self;
197 #[inline]
198 fn sub(self, rhs: Self) -> Self::Output {
199 Self::new_unchecked(M::mod_sub(self.x, rhs.x))
200 }
201}
202impl<M> Mul for MInt<M>
203where
204 M: MIntBase,
205{
206 type Output = Self;
207 #[inline]
208 fn mul(self, rhs: Self) -> Self::Output {
209 Self::new_unchecked(M::mod_mul(self.x, rhs.x))
210 }
211}
212impl<M> Div for MInt<M>
213where
214 M: MIntBase,
215{
216 type Output = Self;
217 #[inline]
218 fn div(self, rhs: Self) -> Self::Output {
219 Self::new_unchecked(M::mod_div(self.x, rhs.x))
220 }
221}
222impl<M> Neg for MInt<M>
223where
224 M: MIntBase,
225{
226 type Output = Self;
227 #[inline]
228 fn neg(self) -> Self::Output {
229 Self::new_unchecked(M::mod_neg(self.x))
230 }
pub fn get_mod() -> M::Inner
Sourcepub fn inv(self) -> Self
pub fn inv(self) -> Self
Examples found in repository?
crates/competitive/src/math/number_theoretic_transform.rs (line 286)
283 fn inverse_transform(mut f: Self::F, len: usize) -> Self::T {
284 intt(&mut f);
285 f.truncate(len);
286 let inv = MInt::from(len.max(2).next_power_of_two() as u32).inv();
287 for f in f.iter_mut() {
288 *f *= inv;
289 }
290 f
291 }
292 fn multiply(f: &mut Self::F, g: &Self::F) {
293 assert_eq!(f.len(), g.len());
294 for (f, g) in f.iter_mut().zip(g.iter()) {
295 *f *= *g;
296 }
297 }
298 fn convolve(mut a: Self::T, mut b: Self::T) -> Self::T {
299 if Self::length(&a).min(Self::length(&b)) <= 60 {
300 return convolve_naive(&a, &b);
301 }
302 let len = (Self::length(&a) + Self::length(&b)).saturating_sub(1);
303 let size = len.max(2).next_power_of_two();
304 if len <= size / 2 + 2 {
305 let xa = a.pop().unwrap();
306 let xb = b.pop().unwrap();
307 let mut c = vec![MInt::<M>::zero(); len];
308 *c.last_mut().unwrap() = xa * xb;
309 for (a, c) in a.iter().zip(&mut c[b.len()..]) {
310 *c += *a * xb;
311 }
312 for (b, c) in b.iter().zip(&mut c[a.len()..]) {
313 *c += *b * xa;
314 }
315 let d = Self::convolve(a, b);
316 for (d, c) in d.into_iter().zip(&mut c) {
317 *c += d;
318 }
319 return c;
320 }
321 let same = a == b;
322 let mut a = Self::transform(a, len);
323 if same {
324 for a in a.iter_mut() {
325 *a *= *a;
326 }
327 } else {
328 let b = Self::transform(b, len);
329 Self::multiply(&mut a, &b);
330 }
331 Self::inverse_transform(a, len)
332 }
333}
334type MVec<M> = Vec<MInt<M>>;
335impl<M, N1, N2, N3> ConvolveSteps for Convolve<(M, (N1, N2, N3))>
336where
337 M: MIntConvert + MIntConvert<u32>,
338 N1: Montgomery32NttModulus,
339 N2: Montgomery32NttModulus,
340 N3: Montgomery32NttModulus,
341{
342 type T = MVec<M>;
343 type F = (MVec<N1>, MVec<N2>, MVec<N3>);
344 fn length(t: &Self::T) -> usize {
345 t.len()
346 }
347 fn transform(t: Self::T, len: usize) -> Self::F {
348 let npot = len.max(2).next_power_of_two();
349 let mut f = (
350 MVec::<N1>::with_capacity(npot),
351 MVec::<N2>::with_capacity(npot),
352 MVec::<N3>::with_capacity(npot),
353 );
354 for t in t {
355 f.0.push(<M as MIntConvert<u32>>::into(t.inner()).into());
356 f.1.push(<M as MIntConvert<u32>>::into(t.inner()).into());
357 f.2.push(<M as MIntConvert<u32>>::into(t.inner()).into());
358 }
359 f.0.resize_with(npot, Zero::zero);
360 f.1.resize_with(npot, Zero::zero);
361 f.2.resize_with(npot, Zero::zero);
362 ntt(&mut f.0);
363 ntt(&mut f.1);
364 ntt(&mut f.2);
365 f
366 }
367 fn inverse_transform(f: Self::F, len: usize) -> Self::T {
368 let t1 = MInt::<N2>::new(N1::get_mod()).inv();
369 let m1 = MInt::<M>::from(N1::get_mod());
370 let m1_3 = MInt::<N3>::new(N1::get_mod());
371 let t2 = (m1_3 * MInt::<N3>::new(N2::get_mod())).inv();
372 let m2 = m1 * MInt::<M>::from(N2::get_mod());
373 Convolve::<N1>::inverse_transform(f.0, len)
374 .into_iter()
375 .zip(Convolve::<N2>::inverse_transform(f.1, len))
376 .zip(Convolve::<N3>::inverse_transform(f.2, len))
377 .map(|((c1, c2), c3)| {
378 let d1 = c1.inner();
379 let d2 = ((c2 - MInt::<N2>::from(d1)) * t1).inner();
380 let x = MInt::<N3>::new(d1) + MInt::<N3>::new(d2) * m1_3;
381 let d3 = ((c3 - x) * t2).inner();
382 MInt::<M>::from(d1) + MInt::<M>::from(d2) * m1 + MInt::<M>::from(d3) * m2
383 })
384 .collect()
385 }
More examples
crates/competitive/src/math/factorial.rs (line 24)
18 pub fn new(max_n: usize) -> Self {
19 let mut fact = vec![MInt::one(); max_n + 1];
20 let mut inv_fact = vec![MInt::one(); max_n + 1];
21 for i in 2..=max_n {
22 fact[i] = fact[i - 1] * MInt::from(i);
23 }
24 inv_fact[max_n] = fact[max_n].inv();
25 for i in (3..=max_n).rev() {
26 inv_fact[i - 1] = inv_fact[i] * MInt::from(i);
27 }
28 Self { fact, inv_fact }
29 }
30 #[inline]
31 pub fn combination(&self, n: usize, r: usize) -> MInt<M> {
32 debug_assert!(n < self.fact.len());
33 if r <= n {
34 self.fact[n] * self.inv_fact[r] * self.inv_fact[n - r]
35 } else {
36 MInt::zero()
37 }
38 }
39 #[inline]
40 pub fn permutation(&self, n: usize, r: usize) -> MInt<M> {
41 debug_assert!(n < self.fact.len());
42 if r <= n {
43 self.fact[n] * self.inv_fact[n - r]
44 } else {
45 MInt::zero()
46 }
47 }
48 #[inline]
49 pub fn homogeneous_product(&self, n: usize, r: usize) -> MInt<M> {
50 debug_assert!(n + r < self.fact.len() + 1);
51 if n == 0 && r == 0 {
52 MInt::one()
53 } else {
54 self.combination(n + r - 1, r)
55 }
56 }
57 #[inline]
58 pub fn inv(&self, n: usize) -> MInt<M> {
59 debug_assert!(n < self.fact.len());
60 debug_assert!(n > 0);
61 self.inv_fact[n] * self.fact[n - 1]
62 }
63}
64
65#[codesnip::entry("SmallModMemorizedFactorial", include("MIntBase", "prime_factors"))]
66#[derive(Clone, Debug)]
67pub struct SmallModMemorizedFactorial<M>
68where
69 M: MIntConvert<usize>,
70{
71 p: u32,
72 c: u32,
73 fact: Vec<MInt<M>>,
74 inv_fact: Vec<MInt<M>>,
75 pow: Vec<MInt<M>>,
76}
77#[codesnip::entry("SmallModMemorizedFactorial")]
78impl<M> Default for SmallModMemorizedFactorial<M>
79where
80 M: MIntConvert<usize>,
81{
82 fn default() -> Self {
83 let m = M::mod_into();
84 let pf = prime_factors(m as _);
85 assert!(pf.len() <= 1);
86 let p = pf[0].0 as u32;
87 let c = pf[0].1;
88 let mut fact = vec![MInt::one(); m];
89 let mut inv_fact = vec![MInt::one(); m];
90 let mut pow = vec![MInt::one(); c as usize];
91 for i in 2..m {
92 fact[i] = fact[i - 1]
93 * if i as u32 % p != 0 {
94 MInt::from(i)
95 } else {
96 MInt::one()
97 };
98 }
99 inv_fact[m - 1] = fact[m - 1].inv();
100 for i in (3..m).rev() {
101 inv_fact[i - 1] = inv_fact[i]
102 * if i as u32 % p != 0 {
103 MInt::from(i)
104 } else {
105 MInt::one()
106 };
107 }
108 for i in 1..c as usize {
109 pow[i] = pow[i - 1] * MInt::from(p as usize);
110 }
111 Self {
112 p,
113 c,
114 fact,
115 inv_fact,
116 pow,
117 }
118 }
crates/library_checker/src/math/sum_of_totient_function.rs (line 19)
12pub fn sum_of_totient_function(reader: impl Read, mut writer: impl Write) {
13 let s = read_all_unchecked(reader);
14 let mut scanner = Scanner::new(&s);
15 scan!(scanner, n: u64);
16 let mut s = 1;
17 let mut pp = 0;
18 let mut pc = 0;
19 let inv2 = M::new(2).inv();
20 let qa = QuotientArray::from_fn(n, |i| [M::from(i), M::from(i) * M::from(i + 1) * inv2])
21 .map(|[x, y]| [x - M::one(), y - M::one()])
22 .lucy_dp::<ArrayOperation<AdditiveOperation<_>, 2>>(|[x, y], p| [x, y * M::from(p)])
23 .map(|[x, y]| y - x)
24 .min_25_sieve::<AddMulOperation<_>>(|p, c| {
25 if pp != p || pc > c {
26 pp = p;
27 pc = 1;
28 s = p - 1;
29 }
30 while pc < c {
31 pc += 1;
32 s *= p;
33 }
34 M::from(s)
35 });
36 writeln!(writer, "{}", qa[n]).ok();
37}
crates/competitive/src/math/lagrange_interpolation.rs (line 78)
50pub fn lagrange_interpolation_polynomial<M>(x: &[MInt<M>], y: &[MInt<M>]) -> Vec<MInt<M>>
51where
52 M: MIntBase,
53{
54 let n = x.len() - 1;
55 let mut dp = vec![MInt::zero(); n + 2];
56 let mut ndp = vec![MInt::zero(); n + 2];
57 dp[0] = -x[0];
58 dp[1] = MInt::one();
59 for x in x.iter().skip(1) {
60 for j in 0..=n + 1 {
61 ndp[j] = -dp[j] * x + if j >= 1 { dp[j - 1] } else { MInt::zero() };
62 }
63 std::mem::swap(&mut dp, &mut ndp);
64 }
65 let mut res = vec![MInt::zero(); n + 1];
66 for i in 0..=n {
67 let t = y[i]
68 / (0..=n)
69 .map(|j| if i != j { x[i] - x[j] } else { MInt::one() })
70 .product::<MInt<M>>();
71 if t.is_zero() {
72 continue;
73 } else if x[i].is_zero() {
74 for j in 0..=n {
75 res[j] += dp[j + 1] * t;
76 }
77 } else {
78 let xinv = x[i].inv();
79 let mut pre = MInt::zero();
80 for j in 0..=n {
81 let d = -(dp[j] - pre) * xinv;
82 res[j] += d * t;
83 pre = d;
84 }
85 }
86 }
87 res
88}
Sourcepub fn inner(self) -> M::Inner
pub fn inner(self) -> M::Inner
Examples found in repository?
crates/competitive/src/num/mint/mint_base.rs (line 107)
106 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107 Debug::fmt(&self.inner(), f)
108 }
109}
110impl<M> Default for MInt<M>
111where
112 M: MIntBase,
113{
114 #[inline]
115 fn default() -> Self {
116 <Self as Zero>::zero()
117 }
118}
119impl<M> PartialEq for MInt<M>
120where
121 M: MIntBase,
122{
123 #[inline]
124 fn eq(&self, other: &Self) -> bool {
125 PartialEq::eq(&self.x, &other.x)
126 }
127}
128impl<M> Eq for MInt<M> where M: MIntBase {}
129impl<M> Hash for MInt<M>
130where
131 M: MIntBase,
132{
133 #[inline]
134 fn hash<H: Hasher>(&self, state: &mut H) {
135 Hash::hash(&self.x, state)
136 }
137}
138macro_rules! impl_mint_from {
139 ($($t:ty),*) => {
140 $(impl<M> From<$t> for MInt<M>
141 where
142 M: MIntConvert<$t>,
143 {
144 #[inline]
145 fn from(x: $t) -> Self {
146 Self::new_unchecked(<M as MIntConvert<$t>>::from(x))
147 }
148 }
149 impl<M> From<MInt<M>> for $t
150 where
151 M: MIntConvert<$t>,
152 {
153 #[inline]
154 fn from(x: MInt<M>) -> $t {
155 <M as MIntConvert<$t>>::into(x.x)
156 }
157 })*
158 };
159}
160impl_mint_from!(
161 u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize
162);
163impl<M> Zero for MInt<M>
164where
165 M: MIntBase,
166{
167 #[inline]
168 fn zero() -> Self {
169 Self::new_unchecked(M::mod_zero())
170 }
171}
172impl<M> One for MInt<M>
173where
174 M: MIntBase,
175{
176 #[inline]
177 fn one() -> Self {
178 Self::new_unchecked(M::mod_one())
179 }
180}
181
182impl<M> Add for MInt<M>
183where
184 M: MIntBase,
185{
186 type Output = Self;
187 #[inline]
188 fn add(self, rhs: Self) -> Self::Output {
189 Self::new_unchecked(M::mod_add(self.x, rhs.x))
190 }
191}
192impl<M> Sub for MInt<M>
193where
194 M: MIntBase,
195{
196 type Output = Self;
197 #[inline]
198 fn sub(self, rhs: Self) -> Self::Output {
199 Self::new_unchecked(M::mod_sub(self.x, rhs.x))
200 }
201}
202impl<M> Mul for MInt<M>
203where
204 M: MIntBase,
205{
206 type Output = Self;
207 #[inline]
208 fn mul(self, rhs: Self) -> Self::Output {
209 Self::new_unchecked(M::mod_mul(self.x, rhs.x))
210 }
211}
212impl<M> Div for MInt<M>
213where
214 M: MIntBase,
215{
216 type Output = Self;
217 #[inline]
218 fn div(self, rhs: Self) -> Self::Output {
219 Self::new_unchecked(M::mod_div(self.x, rhs.x))
220 }
221}
222impl<M> Neg for MInt<M>
223where
224 M: MIntBase,
225{
226 type Output = Self;
227 #[inline]
228 fn neg(self) -> Self::Output {
229 Self::new_unchecked(M::mod_neg(self.x))
230 }
231}
232impl<M> Sum for MInt<M>
233where
234 M: MIntBase,
235{
236 #[inline]
237 fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
238 iter.fold(<Self as Zero>::zero(), Add::add)
239 }
240}
241impl<M> Product for MInt<M>
242where
243 M: MIntBase,
244{
245 #[inline]
246 fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
247 iter.fold(<Self as One>::one(), Mul::mul)
248 }
249}
250impl<'a, M: 'a> Sum<&'a MInt<M>> for MInt<M>
251where
252 M: MIntBase,
253{
254 #[inline]
255 fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
256 iter.fold(<Self as Zero>::zero(), Add::add)
257 }
258}
259impl<'a, M: 'a> Product<&'a MInt<M>> for MInt<M>
260where
261 M: MIntBase,
262{
263 #[inline]
264 fn product<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
265 iter.fold(<Self as One>::one(), Mul::mul)
266 }
267}
268impl<M> Display for MInt<M>
269where
270 M: MIntBase,
271 M::Inner: Display,
272{
273 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
274 write!(f, "{}", self.inner())
275 }
More examples
crates/competitive/src/math/number_theoretic_transform.rs (line 355)
347 fn transform(t: Self::T, len: usize) -> Self::F {
348 let npot = len.max(2).next_power_of_two();
349 let mut f = (
350 MVec::<N1>::with_capacity(npot),
351 MVec::<N2>::with_capacity(npot),
352 MVec::<N3>::with_capacity(npot),
353 );
354 for t in t {
355 f.0.push(<M as MIntConvert<u32>>::into(t.inner()).into());
356 f.1.push(<M as MIntConvert<u32>>::into(t.inner()).into());
357 f.2.push(<M as MIntConvert<u32>>::into(t.inner()).into());
358 }
359 f.0.resize_with(npot, Zero::zero);
360 f.1.resize_with(npot, Zero::zero);
361 f.2.resize_with(npot, Zero::zero);
362 ntt(&mut f.0);
363 ntt(&mut f.1);
364 ntt(&mut f.2);
365 f
366 }
367 fn inverse_transform(f: Self::F, len: usize) -> Self::T {
368 let t1 = MInt::<N2>::new(N1::get_mod()).inv();
369 let m1 = MInt::<M>::from(N1::get_mod());
370 let m1_3 = MInt::<N3>::new(N1::get_mod());
371 let t2 = (m1_3 * MInt::<N3>::new(N2::get_mod())).inv();
372 let m2 = m1 * MInt::<M>::from(N2::get_mod());
373 Convolve::<N1>::inverse_transform(f.0, len)
374 .into_iter()
375 .zip(Convolve::<N2>::inverse_transform(f.1, len))
376 .zip(Convolve::<N3>::inverse_transform(f.2, len))
377 .map(|((c1, c2), c3)| {
378 let d1 = c1.inner();
379 let d2 = ((c2 - MInt::<N2>::from(d1)) * t1).inner();
380 let x = MInt::<N3>::new(d1) + MInt::<N3>::new(d2) * m1_3;
381 let d3 = ((c3 - x) * t2).inner();
382 MInt::<M>::from(d1) + MInt::<M>::from(d2) * m1 + MInt::<M>::from(d3) * m2
383 })
384 .collect()
385 }
Source§impl MInt<DynModuloU32>
impl MInt<DynModuloU32>
Sourcepub fn set_mod(m: u32)
pub fn set_mod(m: u32)
Examples found in repository?
crates/library_checker/src/math/sqrt_mod.rs (line 11)
6pub fn sqrt_mod(reader: impl Read, mut writer: impl Write) {
7 let s = read_all_unchecked(reader);
8 let mut scanner = Scanner::new(&s);
9 scan!(scanner, q, yp: [(u32, u32)]);
10 for (y, p) in yp.take(q) {
11 DynMIntU32::set_mod(p);
12 if let Some(x) = DynMIntU32::from(y).sqrt() {
13 writeln!(writer, "{}", x).ok();
14 } else {
15 writeln!(writer, "-1").ok();
16 }
17 }
18}
Trait Implementations§
Source§impl<M> AddAssign<&MInt<M>> for MInt<M>where
M: MIntBase,
impl<M> AddAssign<&MInt<M>> for MInt<M>where
M: MIntBase,
Source§fn add_assign(&mut self, other: &MInt<M>)
fn add_assign(&mut self, other: &MInt<M>)
Performs the
+=
operation. Read moreSource§impl<M> AddAssign for MInt<M>where
M: MIntBase,
impl<M> AddAssign for MInt<M>where
M: MIntBase,
Source§fn add_assign(&mut self, rhs: MInt<M>)
fn add_assign(&mut self, rhs: MInt<M>)
Performs the
+=
operation. Read moreSource§impl<M> DivAssign<&MInt<M>> for MInt<M>where
M: MIntBase,
impl<M> DivAssign<&MInt<M>> for MInt<M>where
M: MIntBase,
Source§fn div_assign(&mut self, other: &MInt<M>)
fn div_assign(&mut self, other: &MInt<M>)
Performs the
/=
operation. Read moreSource§impl<M> DivAssign for MInt<M>where
M: MIntBase,
impl<M> DivAssign for MInt<M>where
M: MIntBase,
Source§fn div_assign(&mut self, rhs: MInt<M>)
fn div_assign(&mut self, rhs: MInt<M>)
Performs the
/=
operation. Read moreSource§impl<M> FormalPowerSeriesCoefficientSqrt for MInt<M>
impl<M> FormalPowerSeriesCoefficientSqrt for MInt<M>
fn sqrt_coefficient(&self) -> Option<Self>
Source§impl<M> MulAssign<&MInt<M>> for MInt<M>where
M: MIntBase,
impl<M> MulAssign<&MInt<M>> for MInt<M>where
M: MIntBase,
Source§fn mul_assign(&mut self, other: &MInt<M>)
fn mul_assign(&mut self, other: &MInt<M>)
Performs the
*=
operation. Read moreSource§impl<M> MulAssign for MInt<M>where
M: MIntBase,
impl<M> MulAssign for MInt<M>where
M: MIntBase,
Source§fn mul_assign(&mut self, rhs: MInt<M>)
fn mul_assign(&mut self, rhs: MInt<M>)
Performs the
*=
operation. Read moreSource§impl<M> SubAssign<&MInt<M>> for MInt<M>where
M: MIntBase,
impl<M> SubAssign<&MInt<M>> for MInt<M>where
M: MIntBase,
Source§fn sub_assign(&mut self, other: &MInt<M>)
fn sub_assign(&mut self, other: &MInt<M>)
Performs the
-=
operation. Read moreSource§impl<M> SubAssign for MInt<M>where
M: MIntBase,
impl<M> SubAssign for MInt<M>where
M: MIntBase,
Source§fn sub_assign(&mut self, rhs: MInt<M>)
fn sub_assign(&mut self, rhs: MInt<M>)
Performs the
-=
operation. Read moreimpl<M> Copy for MInt<M>where
M: MIntBase,
impl<M> Eq for MInt<M>where
M: MIntBase,
impl<M> FormalPowerSeriesCoefficient for MInt<M>where
M: MIntConvert<usize>,
Auto Trait Implementations§
impl<M> Freeze for MInt<M>
impl<M> RefUnwindSafe for MInt<M>
impl<M> Send for MInt<M>
impl<M> Sync for MInt<M>
impl<M> Unpin for MInt<M>
impl<M> UnwindSafe for MInt<M>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more