@@ -26,12 +26,20 @@ namespace math {
26
26
*/
27
27
namespace ncr_modulo_p {
28
28
29
+ /* *
30
+ * @namespace utils
31
+ * @brief this namespace contains the definitions of the functions called from
32
+ * the class NCRModuloP
33
+ */
29
34
namespace utils {
30
- /* * Finds the value of x, y such that a*x + b*y = gcd(a,b)
35
+ /* *
36
+ * @brief finds the values x and y such that a*x + b*y = gcd(a,b)
31
37
*
32
- * @params[in] the numbers 'a', 'b' and address of 'x' and 'y' from above
33
- * equation
34
- * @returns the gcd of a and b
38
+ * @param[in] a the first input of the gcd
39
+ * @param[in] a the second input of the gcd
40
+ * @param[out] x the Bézout coefficient of a
41
+ * @param[out] y the Bézout coefficient of b
42
+ * @return the gcd of a and b
35
43
*/
36
44
int64_t gcdExtended (const int64_t & a, const int64_t & b, int64_t & x,
37
45
int64_t & y) {
@@ -49,10 +57,11 @@ int64_t gcdExtended(const int64_t& a, const int64_t& b, int64_t& x,
49
57
return gcd;
50
58
}
51
59
52
- /* * Find modular inverse of a with m i.e. a number x such that (a*x)%m = 1
60
+ /* * Find modular inverse of a modulo m i.e. a number x such that (a*x)%m = 1
53
61
*
54
- * @params[in] the numbers 'a' and 'm' from above equation
55
- * @returns the modular inverse of a
62
+ * @param[in] a the number for which the modular inverse is queried
63
+ * @param[in] m the modulus
64
+ * @return the inverce of a modulo m, if it exists, -1 otherwise
56
65
*/
57
66
int64_t modInverse (const int64_t & a, const int64_t & m) {
58
67
int64_t x = 0 , y = 0 ;
@@ -91,17 +100,18 @@ class NCRModuloP {
91
100
}
92
101
93
102
public:
94
- /* * Constructor which precomputes the values of n! % mod from n=0 to size
95
- * and stores them in vector 'fac'
96
- * @params[in] the numbers ' size', 'mod'
103
+ /* *
104
+ * @brief constructs an NCRModuloP object allowing to compute (nCr)%p for
105
+ * inputs from 0 to size
97
106
*/
98
- NCRModuloP (const int64_t & size, const int64_t & mod )
99
- : p(mod ), fac(computeFactorialsMod(size, mod )) {}
107
+ NCRModuloP (const int64_t & size, const int64_t & p )
108
+ : p(p ), fac(computeFactorialsMod(size, p )) {}
100
109
101
- /* * Find nCr % p
102
- *
103
- * @params[in] the numbers 'n' and 'r'
104
- * @returns the value nCr % p
110
+ /* *
111
+ * @brief computes nCr % p
112
+ * @param[in] n the number of objects to be chosen
113
+ * @param[in] r the number of objects to choose from
114
+ * @return the value nCr % p
105
115
*/
106
116
int64_t ncr (const int64_t & n, const int64_t & r) const {
107
117
// Base cases
@@ -126,13 +136,7 @@ class NCRModuloP {
126
136
} // namespace ncr_modulo_p
127
137
} // namespace math
128
138
129
- /* *
130
- * @brief Test implementations
131
- * @param ncrObj object which contains the precomputed factorial values and
132
- * ncr function
133
- * @returns void
134
- */
135
- static void tests () {
139
+ void tests () {
136
140
struct TestCase {
137
141
const int64_t size;
138
142
const int64_t p;
@@ -161,19 +165,26 @@ static void tests() {
161
165
}
162
166
163
167
/* *
164
- * @brief Main function
165
- * @returns 0 on exit
168
+ * @brief example showing the usage of the NCRModuloP class
166
169
*/
167
- int main () {
168
- // populate the fac array
170
+ void example () {
169
171
const int64_t size = 1e6 + 1 ;
170
172
const int64_t p = 1e9 + 7 ;
173
+
174
+ // the ncrObj contains the precomputed values of factorials modulo p for
175
+ // values from 0 to size
171
176
const auto ncrObj = math::ncr_modulo_p::NCRModuloP (size, p);
172
- // test 6Ci for i=0 to 7
177
+
178
+ // having the ncrObj we can efficiently query the values of (n C r)%p
179
+ // note that time of the computation does not depend on size
173
180
for (int i = 0 ; i <= 7 ; i++) {
174
- std::cout << 6 << " C" << i << " = " << ncrObj.ncr (6 , i) << " \n " ;
181
+ std::cout << 6 << " C" << i << " mod " << p << " = " << ncrObj.ncr (6 , i)
182
+ << " \n " ;
175
183
}
176
- tests (); // execute the tests
177
- std::cout << " Assertions passed\n " ;
184
+ }
185
+
186
+ int main () {
187
+ tests ();
188
+ example ();
178
189
return 0 ;
179
190
}
0 commit comments