Aleph-w 7.10.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 <utility>
52# include <ah-errors.H>
53# include <tpl_array.H>
54# include <modular_arithmetic.H>
55# include <primality.H>
56
57namespace Aleph
58{
75 {
76 uint64_t mod_;
79
80 public:
91 ModularCombinatorics(size_t max_n, const uint64_t p) : mod_(p)
92 {
94 << "ModularCombinatorics: modulus " << mod_ << " must be prime";
95
96 if (max_n >= mod_)
97 max_n = static_cast<size_t>(mod_ - 1);
98
99 fact_.reserve(max_n + 1);
100 invFact_.reserve(max_n + 1);
101
102 fact_.append(1);
103 for (size_t i = 1; i <= max_n; i++)
104 fact_.append(mod_mul(fact_[i - 1], i, mod_));
105
106 invFact_.putn(max_n + 1);
107 invFact_(max_n) = mod_inv(fact_[max_n], mod_);
108 for (size_t i = max_n; i > 0; i--)
109 invFact_(i - 1) = mod_mul(invFact_[i], i, mod_);
110 }
111
117
122 ModularCombinatorics(ModularCombinatorics && other) noexcept = default;
123
130
142 {
143 swap(other);
144 return *this;
145 }
146
151 void swap(ModularCombinatorics & other) noexcept
152 {
153 std::swap(mod_, other.mod_);
154 fact_.swap(other.fact_);
155 invFact_.swap(other.invFact_);
156 }
157
171 [[nodiscard]] uint64_t nCk(const uint64_t n, const uint64_t k) const
172 {
173 if (k > n) return 0;
175 << "ModularCombinatorics::nCk: n=" << n << " exceeds precomputed range "
176 << fact_.size() - 1;
177
178 const uint64_t num = fact_[n];
179 const uint64_t den = mod_mul(invFact_[k], invFact_[n - k], mod_);
180 return mod_mul(num, den, mod_);
181 }
182
201 [[nodiscard]] uint64_t lucas_nCk(const uint64_t n, const uint64_t k) const
202 {
203 if (k > n) return 0;
204 if (k == 0) return 1;
205
206 const uint64_t ni = n % mod_;
207 const uint64_t ki = k % mod_;
208
209 if (ki > ni) return 0;
210
211 return mod_mul(nCk(ni, ki), lucas_nCk(n / mod_, k / mod_), mod_);
212 }
213 };
214} // namespace Aleph
215
216# 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:727
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:787
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:313
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:402
void putn(const size_t n)
Reserve n additional logical slots in the array without value-initializing them.
Definition tpl_array.H:392
High-performance combinatorics operations modulo a prime.
Array< uint64_t > fact_
Precomputed factorials array.
void swap(ModularCombinatorics &other) noexcept
Exchange in O(1) the moduli and the tables of this object and another.
Array< uint64_t > invFact_
Precomputed inverse factorials array.
ModularCombinatorics(const ModularCombinatorics &other)=default
Copy the modulus and the tables of another object.
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'.
ModularCombinatorics & operator=(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_rang...
uint64_t nCk(const uint64_t n, const uint64_t k) const
Calculate in constant time.
Safe modular arithmetic, extended Euclidean algorithm, and Chinese Remainder Theorem.
Main namespace for Aleph-w library functions.
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.