|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
High-performance combinatorics operations modulo a prime. More...
#include <modular_combinatorics.H>
Public Member Functions | |
| ModularCombinatorics (size_t max_n, const uint64_t p) | |
| Construct and precompute factorials up to 'max_n' modulo 'p'. | |
| ModularCombinatorics (const ModularCombinatorics &other)=default | |
| Copy the modulus and the tables of another object. | |
| ModularCombinatorics (ModularCombinatorics &&other) noexcept=default | |
Take the tables of another object, which keeps its modulus and no table: its nCk() throws out_of_range for any k <= n. | |
| ModularCombinatorics & | operator= (const ModularCombinatorics &other)=default |
| Copy the modulus and the tables of another object. | |
| ModularCombinatorics & | operator= (ModularCombinatorics &&other) noexcept |
| Exchange this object with another, which is left with the previous modulus and tables of this one. | |
| void | swap (ModularCombinatorics &other) noexcept |
| Exchange in O(1) the moduli and the tables of this object and another. | |
| uint64_t | nCk (const uint64_t n, const uint64_t k) const |
| Calculate \(\binom{n}{k} \pmod{P}\)in constant time. | |
| uint64_t | lucas_nCk (const uint64_t n, const uint64_t k) const |
| Calculate \(\binom{n}{k} \pmod{P}\)using Lucas' Theorem. | |
Private Attributes | |
| uint64_t | mod_ |
| Prime modulus \(P\). | |
| Array< uint64_t > | fact_ |
| Precomputed factorials array. | |
| Array< uint64_t > | invFact_ |
| Precomputed inverse factorials array. | |
High-performance combinatorics operations modulo a prime.
The ModularCombinatorics class encapsulates the state required to compute binomial coefficients modulo a prime \(P\).
It performs an initial precomputation of:
This allows computing \(\binom{n}{k} \equiv \frac{n!}{k!(n-k)!} \pmod{P}\) in O(1) time per query.
Definition at line 74 of file modular_combinatorics.H.
|
inline |
Construct and precompute factorials up to 'max_n' modulo 'p'.
| [in] | max_n | Maximum value for \(n\)to be used in O(1) nCk queries. |
| [in] | p | The prime modulus. |
p must be a prime number. | std::invalid_argument | if p is not prime (verified via Miller-Rabin). |
Definition at line 91 of file modular_combinatorics.H.
References ah_invalid_argument_if, Aleph::Array< T >::append(), fact_, invFact_, Aleph::miller_rabin(), mod_, Aleph::mod_inv(), Aleph::mod_mul(), Aleph::Array< T >::putn(), and Aleph::Array< T >::reserve().
|
default |
Copy the modulus and the tables of another object.
| [in] | other | object to copy. |
| std::bad_alloc | if the tables cannot be allocated. |
|
defaultnoexcept |
Take the tables of another object, which keeps its modulus and no table: its nCk() throws out_of_range for any k <= n.
| [in,out] | other | object to move from. |
|
inline |
Calculate \(\binom{n}{k} \pmod{P}\)using Lucas' Theorem.
Provides a way to compute combinations when \(n\)and \(k\)are very large (exceeding max_n) but the prime modulus \(P\)is small.
Lucas' Theorem states:
\[ \binom{n}{k} \equiv \prod_{i=0}^m \binom{n_i}{k_i} \pmod{P} \]
where \(n_i\)and \(k_i\)are the digits of \(n\)and \(k\) in base \(P\).
mod_ - 1.| [in] | n | Total number of items. |
| [in] | k | Number of items to choose. |
Definition at line 201 of file modular_combinatorics.H.
References k, lucas_nCk(), mod_, Aleph::mod_mul(), and nCk().
Referenced by demo_modular_combinatorics(), lucas_nCk(), and TEST().
|
inline |
Calculate \(\binom{n}{k} \pmod{P}\)in constant time.
Computes the binomial coefficient using the precomputed tables.
| [in] | n | Total number of items. |
| [in] | k | Number of items to choose. |
max_n range provided at construction. | ah_out_of_range_error | if \(n\)exceeds the precomputed range. |
Definition at line 171 of file modular_combinatorics.H.
References ah_out_of_range_error_if, fact_, invFact_, k, mod_, Aleph::mod_mul(), and Aleph::Array< T >::size().
Referenced by demo_modular_combinatorics(), lucas_nCk(), and TEST().
|
default |
Copy the modulus and the tables of another object.
| [in] | other | object to copy. |
| std::bad_alloc | if the tables cannot be allocated. |
|
inlinenoexcept |
Exchange this object with another, which is left with the previous modulus and tables of this one.
The implicit assignment exchanged the tables but copied the modulus: the source then computed with the tables of another modulus, and nCk(5, 2) gave 24.
| [in,out] | other | object to move from. |
Definition at line 141 of file modular_combinatorics.H.
References swap().
|
inlinenoexcept |
Exchange in O(1) the moduli and the tables of this object and another.
| [in,out] | other | object to exchange with. |
Definition at line 151 of file modular_combinatorics.H.
References fact_, invFact_, mod_, and Aleph::Array< T >::swap().
Referenced by operator=().
|
private |
Precomputed factorials array.
Definition at line 77 of file modular_combinatorics.H.
Referenced by ModularCombinatorics(), nCk(), and swap().
|
private |
Precomputed inverse factorials array.
Definition at line 78 of file modular_combinatorics.H.
Referenced by ModularCombinatorics(), nCk(), and swap().
|
private |
Prime modulus \(P\).
Definition at line 76 of file modular_combinatorics.H.
Referenced by ModularCombinatorics(), lucas_nCk(), nCk(), and swap().