Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
modular_combinatorics.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
47# ifndef MODULAR_COMBINATORICS_H
48# define MODULAR_COMBINATORICS_H
49
50# include <cstdint>
51# include <ah-errors.H>
52# include <tpl_array.H>
53# include <modular_arithmetic.H>
54# include <primality.H>
55
56namespace Aleph
57{
74 {
78
79 public:
91 {
93 << "ModularCombinatorics: modulus " << mod_ << " must be prime";
94
95 if (max_n >= mod_)
96 max_n = static_cast<size_t>(mod_ - 1);
97
98 fact_.reserve(max_n + 1);
100
101 fact_.append(1);
102 for (size_t i = 1; i <= max_n; i++)
103 fact_.append(mod_mul(fact_[i - 1], i, mod_));
104
105 invFact_.putn(max_n + 1);
107 for (size_t i = max_n; i > 0; i--)
108 invFact_(i - 1) = mod_mul(invFact_[i], i, mod_);
109 }
110
124 [[nodiscard]] uint64_t nCk(const uint64_t n, const uint64_t k) const
125 {
126 if (k > n) return 0;
128 << "ModularCombinatorics::nCk: n=" << n << " exceeds precomputed range "
129 << fact_.size() - 1;
130
131 const uint64_t num = fact_[n];
132 const uint64_t den = mod_mul(invFact_[k], invFact_[n - k], mod_);
133 return mod_mul(num, den, mod_);
134 }
135
154 [[nodiscard]] uint64_t lucas_nCk(const uint64_t n, const uint64_t k) const
155 {
156 if (k > n) return 0;
157 if (k == 0) return 1;
158
159 const uint64_t ni = n % mod_;
160 const uint64_t ki = k % mod_;
161
162 if (ki > ni) return 0;
163
164 return mod_mul(nCk(ni, ki), lucas_nCk(n / mod_, k / mod_), mod_);
165 }
166 };
167} // namespace Aleph
168
169# endif // MODULAR_COMBINATORICS_H
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:584
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:644
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:138
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:365
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:250
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:320
void putn(const size_t n)
Reserve n additional logical slots in the array without value-initializing them.
Definition tpl_array.H:310
High-performance combinatorics operations modulo a prime.
Array< uint64_t > fact_
Precomputed factorials array.
Array< uint64_t > invFact_
Precomputed inverse factorials array.
uint64_t lucas_nCk(const uint64_t n, const uint64_t k) const
Calculate using Lucas' Theorem.
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 in constant time.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Safe modular arithmetic, extended Euclidean algorithm, and Chinese Remainder Theorem.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
uint64_t mod_inv(const uint64_t a, const uint64_t m)
Modular Inverse.
bool miller_rabin(uint64_t n) noexcept
Miller-Rabin primality test for 64-bit integers.
Definition primality.H:88
uint64_t mod_mul(uint64_t a, uint64_t b, uint64_t m)
Safe 64-bit modular multiplication.
Advanced primality testing algorithms.
static int * k
Dynamic array container with automatic resizing.