Class which contains all methods required for calculating nCr mod p.
More...
|
| NCRModuloP (const int64_t &size, const int64_t &p) |
| constructs an NCRModuloP object allowing to compute (nCr)p for inputs from 0 to size
|
|
int64_t | ncr (const int64_t &n, const int64_t &r) const |
| computes nCr % p
|
|
|
const int64_t | p = 0 |
|
const std::vector< int64_t > | fac |
| the p from (nCr % p)
|
|
Class which contains all methods required for calculating nCr mod p.
◆ NCRModuloP()
math::ncr_modulo_p::NCRModuloP::NCRModuloP |
( |
const int64_t & | size, |
|
|
const int64_t & | p ) |
|
inline |
constructs an NCRModuloP object allowing to compute (nCr)p for inputs from 0 to size
const std::vector< int64_t > fac
the p from (nCr % p)
Definition ncr_modulo_p.cpp:83
static std::vector< int64_t > computeFactorialsMod(const int64_t &max_arg_val, const int64_t &mod)
stores precomputed factorial(i) % p value
Definition ncr_modulo_p.cpp:92
◆ computeFactorialsMod()
static std::vector< int64_t > math::ncr_modulo_p::NCRModuloP::computeFactorialsMod |
( |
const int64_t & | max_arg_val, |
|
|
const int64_t & | mod ) |
|
inlinestaticprivate |
stores precomputed factorial(i) % p value
computes the array of values of factorials reduced modulo mod
- Parameters
-
max_arg_val | argument of the last factorial stored in the result |
mod | value of the divisor used to reduce factorials |
- Returns
- vector storing factorials of the numbers 0, ..., max_arg_val reduced modulo mod
93 {
95 res[0] = 1;
96 for (int64_t i = 1; i <= max_arg_val; i++) {
97 res[i] = (res[i - 1] * i) % mod;
98 }
99 return res;
100 }
◆ ncr()
int64_t math::ncr_modulo_p::NCRModuloP::ncr |
( |
const int64_t & | n, |
|
|
const int64_t & | r ) const |
|
inline |
computes nCr % p
- Parameters
-
[in] | n | the number of objects to be chosen |
[in] | r | the number of objects to choose from |
- Returns
- the value nCr % p
116 {
117
118 if (r > n) {
119 return 0;
120 }
121 if (r == 1) {
122 return n % p;
123 }
124 if (r == 0 || r == n) {
125 return 1;
126 }
127
128 const auto denominator = (
fac[r] *
fac[n - r]) % p;
129 const auto denominator_inv = utils::modInverse(denominator, p);
130 if (denominator_inv < 0) {
131 return -1;
132 }
133 return (
fac[n] * denominator_inv) % p;
134 }
The documentation for this class was generated from the following file: