|
Aleph-w 3.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'. | |
| 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 73 of file modular_combinatorics.H.
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 90 of file modular_combinatorics.H.
References ah_invalid_argument_if, Aleph::Array< T >::append(), Aleph::blossom_maximum_cardinality_matching(), fact_, invFact_, Aleph::miller_rabin(), mod_, Aleph::mod_inv(), Aleph::mod_mul(), Aleph::Array< T >::putn(), and Aleph::Array< T >::reserve().
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 154 of file modular_combinatorics.H.
References Aleph::blossom_maximum_cardinality_matching(), k, lucas_nCk(), mod_, Aleph::mod_mul(), and nCk().
Referenced by lucas_nCk().
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 124 of file modular_combinatorics.H.
References ah_out_of_range_error_if, Aleph::blossom_maximum_cardinality_matching(), fact_, invFact_, k, mod_, Aleph::mod_mul(), and Aleph::Array< T >::size().
Referenced by lucas_nCk().
Precomputed factorials array.
Definition at line 76 of file modular_combinatorics.H.
Referenced by ModularCombinatorics(), and nCk().
Precomputed inverse factorials array.
Definition at line 77 of file modular_combinatorics.H.
Referenced by ModularCombinatorics(), and nCk().
|
private |
Prime modulus \(P\).
Definition at line 75 of file modular_combinatorics.H.
Referenced by ModularCombinatorics(), lucas_nCk(), and nCk().