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
40# ifndef MODULAR_COMBINATORICS_H
41# define MODULAR_COMBINATORICS_H
42
43# include <cstdint>
44# include <ah-errors.H>
45# include <tpl_array.H>
46# include <modular_arithmetic.H>
47# include <primality.H>
48
49namespace Aleph
50{
57 {
61
62 public:
71 {
73 << "ModularCombinatorics: modulus " << mod_ << " must be prime";
74
75 if (max_n >= mod_)
76 max_n = static_cast<size_t>(mod_ - 1);
77
78 fact_.reserve(max_n + 1);
80
81 fact_.append(1);
82 for (size_t i = 1; i <= max_n; i++)
83 fact_.append(mod_mul(fact_[i - 1], i, mod_));
84
85 invFact_.putn(max_n + 1);
87 for (size_t i = max_n; i > 0; i--)
88 invFact_(i - 1) = mod_mul(invFact_[i], i, mod_);
89 }
90
102 [[nodiscard]] uint64_t nCk(const uint64_t n, const uint64_t k) const
103 {
104 if (k > n) return 0;
106 << "ModularCombinatorics::nCk: n=" << n << " exceeds precomputed range "
107 << fact_.size() - 1;
108
109 const uint64_t num = fact_[n];
110 const uint64_t den = mod_mul(invFact_[k], invFact_[n - k], mod_);
111 return mod_mul(num, den, mod_);
112 }
113
127 [[nodiscard]] uint64_t lucas_nCk(const uint64_t n, const uint64_t k) const
128 {
129 if (k > n) return 0;
130 if (k == 0) return 1;
131
132 const uint64_t ni = n % mod_;
133 const uint64_t ki = k % mod_;
134
135 if (ki > ni) return 0;
136
137 return mod_mul(nCk(ni, ki), lucas_nCk(n / mod_, k / mod_), mod_);
138 }
139 };
140} // namespace Aleph
141
142# 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:579
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:639
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:351
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:245
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:315
void putn(const size_t n)
Reserve n additional logical slots in the array without value-initializing them.
Definition tpl_array.H:305
Combinatorics operations modulo a prime.
Array< uint64_t > fact_
Precomputed factorials.
Array< uint64_t > invFact_
Precomputed inverse factorials.
uint64_t lucas_nCk(const uint64_t n, const uint64_t k) const
Calculate nCk modulo p using Lucas' theorem.
ModularCombinatorics(size_t max_n, const uint64_t p)
Precomputes factorials up to 'max_n' modulo 'p'.
uint64_t nCk(const uint64_t n, const uint64_t k) const
Calculate nCk modulo p in O(1).
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.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
bool miller_rabin(uint64_t n) noexcept
Miller-Rabin primality test for 64-bit integers.
Definition primality.H:87
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.