Struct MInt

Source
pub struct MInt<M>
where M: MIntBase,
{ /* private fields */ }

Implementations§

Source§

impl<M> MInt<M>
where M: MIntConvert<u32>,

Source

pub fn sqrt(self) -> Option<Self>

Examples found in repository?
crates/competitive/src/math/formal_power_series/mod.rs (line 67)
66    fn sqrt_coefficient(&self) -> Option<Self> {
67        self.sqrt()
68    }
More examples
Hide additional 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,

Source

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
Hide additional 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,

Source

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    }
Source

pub fn get_mod() -> M::Inner

Source

pub fn pow(self, y: usize) -> Self

Examples found in repository?
crates/competitive/src/algorithm/chromatic_number.rs (line 23)
19    pub fn k_colorable(&self, k: usize) -> bool {
20        !self
21            .ind
22            .iter()
23            .map(|d| d.pow(k))
24            .enumerate()
25            .map(|(s, d)| if s.count_ones() % 2 == 0 { d } else { -d })
26            .sum::<MInt<M>>()
27            .is_zero()
28    }
Source

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
Hide additional 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}
Source

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
Hide additional 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>

Source

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}
Source§

impl MInt<DynModuloU64>

Source

pub fn set_mod(m: u64)

Trait Implementations§

Source§

impl<M> Add<&MInt<M>> for &MInt<M>
where M: MIntBase,

Source§

type Output = <MInt<M> as Add>::Output

The resulting type after applying the + operator.
Source§

fn add(self, other: &MInt<M>) -> <MInt<M> as Add<MInt<M>>>::Output

Performs the + operation. Read more
Source§

impl<M> Add<&MInt<M>> for MInt<M>
where M: MIntBase,

Source§

type Output = <MInt<M> as Add>::Output

The resulting type after applying the + operator.
Source§

fn add(self, other: &MInt<M>) -> <MInt<M> as Add<MInt<M>>>::Output

Performs the + operation. Read more
Source§

impl<M> Add<MInt<M>> for &MInt<M>
where M: MIntBase,

Source§

type Output = <MInt<M> as Add>::Output

The resulting type after applying the + operator.
Source§

fn add(self, other: MInt<M>) -> <MInt<M> as Add<MInt<M>>>::Output

Performs the + operation. Read more
Source§

impl<M> Add for MInt<M>
where M: MIntBase,

Source§

type Output = MInt<M>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<M> AddAssign<&MInt<M>> for MInt<M>
where M: MIntBase,

Source§

fn add_assign(&mut self, other: &MInt<M>)

Performs the += operation. Read more
Source§

impl<M> AddAssign for MInt<M>
where M: MIntBase,

Source§

fn add_assign(&mut self, rhs: MInt<M>)

Performs the += operation. Read more
Source§

impl<M> Clone for MInt<M>
where M: MIntBase,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<M> Debug for MInt<M>
where M: MIntBase,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<M> Default for MInt<M>
where M: MIntBase,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<M> Display for MInt<M>
where M: MIntBase, M::Inner: Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<M> Div<&MInt<M>> for &MInt<M>
where M: MIntBase,

Source§

type Output = <MInt<M> as Div>::Output

The resulting type after applying the / operator.
Source§

fn div(self, other: &MInt<M>) -> <MInt<M> as Div<MInt<M>>>::Output

Performs the / operation. Read more
Source§

impl<M> Div<&MInt<M>> for MInt<M>
where M: MIntBase,

Source§

type Output = <MInt<M> as Div>::Output

The resulting type after applying the / operator.
Source§

fn div(self, other: &MInt<M>) -> <MInt<M> as Div<MInt<M>>>::Output

Performs the / operation. Read more
Source§

impl<M> Div<MInt<M>> for &MInt<M>
where M: MIntBase,

Source§

type Output = <MInt<M> as Div>::Output

The resulting type after applying the / operator.
Source§

fn div(self, other: MInt<M>) -> <MInt<M> as Div<MInt<M>>>::Output

Performs the / operation. Read more
Source§

impl<M> Div for MInt<M>
where M: MIntBase,

Source§

type Output = MInt<M>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl<M> DivAssign<&MInt<M>> for MInt<M>
where M: MIntBase,

Source§

fn div_assign(&mut self, other: &MInt<M>)

Performs the /= operation. Read more
Source§

impl<M> DivAssign for MInt<M>
where M: MIntBase,

Source§

fn div_assign(&mut self, rhs: MInt<M>)

Performs the /= operation. Read more
Source§

impl<M> FormalPowerSeriesCoefficientSqrt for MInt<M>

Source§

fn sqrt_coefficient(&self) -> Option<Self>

Source§

impl<M> From<MInt<M>> for i128
where M: MIntConvert<i128>,

Source§

fn from(x: MInt<M>) -> i128

Converts to this type from the input type.
Source§

impl<M> From<MInt<M>> for i16
where M: MIntConvert<i16>,

Source§

fn from(x: MInt<M>) -> i16

Converts to this type from the input type.
Source§

impl<M> From<MInt<M>> for i32
where M: MIntConvert<i32>,

Source§

fn from(x: MInt<M>) -> i32

Converts to this type from the input type.
Source§

impl<M> From<MInt<M>> for i64
where M: MIntConvert<i64>,

Source§

fn from(x: MInt<M>) -> i64

Converts to this type from the input type.
Source§

impl<M> From<MInt<M>> for i8
where M: MIntConvert<i8>,

Source§

fn from(x: MInt<M>) -> i8

Converts to this type from the input type.
Source§

impl<M> From<MInt<M>> for isize
where M: MIntConvert<isize>,

Source§

fn from(x: MInt<M>) -> isize

Converts to this type from the input type.
Source§

impl<M> From<MInt<M>> for u128
where M: MIntConvert<u128>,

Source§

fn from(x: MInt<M>) -> u128

Converts to this type from the input type.
Source§

impl<M> From<MInt<M>> for u16
where M: MIntConvert<u16>,

Source§

fn from(x: MInt<M>) -> u16

Converts to this type from the input type.
Source§

impl<M> From<MInt<M>> for u32
where M: MIntConvert<u32>,

Source§

fn from(x: MInt<M>) -> u32

Converts to this type from the input type.
Source§

impl<M> From<MInt<M>> for u64
where M: MIntConvert<u64>,

Source§

fn from(x: MInt<M>) -> u64

Converts to this type from the input type.
Source§

impl<M> From<MInt<M>> for u8
where M: MIntConvert<u8>,

Source§

fn from(x: MInt<M>) -> u8

Converts to this type from the input type.
Source§

impl<M> From<MInt<M>> for usize
where M: MIntConvert<usize>,

Source§

fn from(x: MInt<M>) -> usize

Converts to this type from the input type.
Source§

impl<M> From<i128> for MInt<M>
where M: MIntConvert<i128>,

Source§

fn from(x: i128) -> Self

Converts to this type from the input type.
Source§

impl<M> From<i16> for MInt<M>
where M: MIntConvert<i16>,

Source§

fn from(x: i16) -> Self

Converts to this type from the input type.
Source§

impl<M> From<i32> for MInt<M>
where M: MIntConvert<i32>,

Source§

fn from(x: i32) -> Self

Converts to this type from the input type.
Source§

impl<M> From<i64> for MInt<M>
where M: MIntConvert<i64>,

Source§

fn from(x: i64) -> Self

Converts to this type from the input type.
Source§

impl<M> From<i8> for MInt<M>
where M: MIntConvert<i8>,

Source§

fn from(x: i8) -> Self

Converts to this type from the input type.
Source§

impl<M> From<isize> for MInt<M>
where M: MIntConvert<isize>,

Source§

fn from(x: isize) -> Self

Converts to this type from the input type.
Source§

impl<M> From<u128> for MInt<M>
where M: MIntConvert<u128>,

Source§

fn from(x: u128) -> Self

Converts to this type from the input type.
Source§

impl<M> From<u16> for MInt<M>
where M: MIntConvert<u16>,

Source§

fn from(x: u16) -> Self

Converts to this type from the input type.
Source§

impl<M> From<u32> for MInt<M>
where M: MIntConvert<u32>,

Source§

fn from(x: u32) -> Self

Converts to this type from the input type.
Source§

impl<M> From<u64> for MInt<M>
where M: MIntConvert<u64>,

Source§

fn from(x: u64) -> Self

Converts to this type from the input type.
Source§

impl<M> From<u8> for MInt<M>
where M: MIntConvert<u8>,

Source§

fn from(x: u8) -> Self

Converts to this type from the input type.
Source§

impl<M> From<usize> for MInt<M>
where M: MIntConvert<usize>,

Source§

fn from(x: usize) -> Self

Converts to this type from the input type.
Source§

impl<M> FromStr for MInt<M>
where M: MIntConvert, M::Inner: FromStr,

Source§

type Err = <<M as MIntBase>::Inner as FromStr>::Err

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<M> Hash for MInt<M>
where M: MIntBase,

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<M> IterScan for MInt<M>
where M: MIntConvert, M::Inner: FromStr,

Source§

type Output = MInt<M>

Source§

fn scan<'a, I: Iterator<Item = &'a str>>(iter: &mut I) -> Option<Self::Output>

Source§

impl<M> Mul<&MInt<M>> for &MInt<M>
where M: MIntBase,

Source§

type Output = <MInt<M> as Mul>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, other: &MInt<M>) -> <MInt<M> as Mul<MInt<M>>>::Output

Performs the * operation. Read more
Source§

impl<M> Mul<&MInt<M>> for MInt<M>
where M: MIntBase,

Source§

type Output = <MInt<M> as Mul>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, other: &MInt<M>) -> <MInt<M> as Mul<MInt<M>>>::Output

Performs the * operation. Read more
Source§

impl<M> Mul<MInt<M>> for &MInt<M>
where M: MIntBase,

Source§

type Output = <MInt<M> as Mul>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, other: MInt<M>) -> <MInt<M> as Mul<MInt<M>>>::Output

Performs the * operation. Read more
Source§

impl<M> Mul for MInt<M>
where M: MIntBase,

Source§

type Output = MInt<M>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<M> MulAssign<&MInt<M>> for MInt<M>
where M: MIntBase,

Source§

fn mul_assign(&mut self, other: &MInt<M>)

Performs the *= operation. Read more
Source§

impl<M> MulAssign for MInt<M>
where M: MIntBase,

Source§

fn mul_assign(&mut self, rhs: MInt<M>)

Performs the *= operation. Read more
Source§

impl<M> Neg for &MInt<M>
where M: MIntBase,

Source§

type Output = <MInt<M> as Neg>::Output

The resulting type after applying the - operator.
Source§

fn neg(self) -> <MInt<M> as Neg>::Output

Performs the unary - operation. Read more
Source§

impl<M> Neg for MInt<M>
where M: MIntBase,

Source§

type Output = MInt<M>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<M> One for MInt<M>
where M: MIntBase,

Source§

fn one() -> Self

Source§

fn is_one(&self) -> bool
where Self: PartialEq,

Source§

fn set_one(&mut self)

Source§

impl<M> PartialEq for MInt<M>
where M: MIntBase,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, M> Product<&'a MInt<M>> for MInt<M>
where M: MIntBase + 'a,

Source§

fn product<I: Iterator<Item = &'a Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by multiplying the items.
Source§

impl<M> Product for MInt<M>
where M: MIntBase,

Source§

fn product<I: Iterator<Item = Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by multiplying the items.
Source§

impl<M> Sub<&MInt<M>> for &MInt<M>
where M: MIntBase,

Source§

type Output = <MInt<M> as Sub>::Output

The resulting type after applying the - operator.
Source§

fn sub(self, other: &MInt<M>) -> <MInt<M> as Sub<MInt<M>>>::Output

Performs the - operation. Read more
Source§

impl<M> Sub<&MInt<M>> for MInt<M>
where M: MIntBase,

Source§

type Output = <MInt<M> as Sub>::Output

The resulting type after applying the - operator.
Source§

fn sub(self, other: &MInt<M>) -> <MInt<M> as Sub<MInt<M>>>::Output

Performs the - operation. Read more
Source§

impl<M> Sub<MInt<M>> for &MInt<M>
where M: MIntBase,

Source§

type Output = <MInt<M> as Sub>::Output

The resulting type after applying the - operator.
Source§

fn sub(self, other: MInt<M>) -> <MInt<M> as Sub<MInt<M>>>::Output

Performs the - operation. Read more
Source§

impl<M> Sub for MInt<M>
where M: MIntBase,

Source§

type Output = MInt<M>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<M> SubAssign<&MInt<M>> for MInt<M>
where M: MIntBase,

Source§

fn sub_assign(&mut self, other: &MInt<M>)

Performs the -= operation. Read more
Source§

impl<M> SubAssign for MInt<M>
where M: MIntBase,

Source§

fn sub_assign(&mut self, rhs: MInt<M>)

Performs the -= operation. Read more
Source§

impl<'a, M> Sum<&'a MInt<M>> for MInt<M>
where M: MIntBase + 'a,

Source§

fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl<M> Sum for MInt<M>
where M: MIntBase,

Source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl<M> Zero for MInt<M>
where M: MIntBase,

Source§

fn zero() -> Self

Source§

fn is_zero(&self) -> bool
where Self: PartialEq,

Source§

fn set_zero(&mut self)

Source§

impl<M> Copy for MInt<M>
where M: MIntBase,

Source§

impl<M> Eq for MInt<M>
where M: MIntBase,

Source§

impl<M> FormalPowerSeriesCoefficient for MInt<M>
where M: MIntConvert<usize>,

Auto Trait Implementations§

§

impl<M> Freeze for MInt<M>
where <M as MIntBase>::Inner: Freeze,

§

impl<M> RefUnwindSafe for MInt<M>
where <M as MIntBase>::Inner: RefUnwindSafe,

§

impl<M> Send for MInt<M>
where <M as MIntBase>::Inner: Send,

§

impl<M> Sync for MInt<M>
where <M as MIntBase>::Inner: Sync,

§

impl<M> Unpin for MInt<M>
where <M as MIntBase>::Inner: Unpin,

§

impl<M> UnwindSafe for MInt<M>
where <M as MIntBase>::Inner: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.