Replace int64_t to uint64_t + add namespace + detailed documentation

This commit is contained in:
KaustubhDamania
2020-10-23 01:29:53 +05:30
parent 2ba05ed8ff
commit e09a05793b

View File

@@ -1,29 +1,38 @@
/** /**
* @file * @file
* @brief This program aims at calculating nCr modulo p * @brief This program aims at calculating nCr modulo p
* * @details nCr is defined as n! / (r! * (n-r)!) where n! represents factorial
* of n. In many cases, the value of nCr is too large to fit in a 64 bit integer.
* Hence, in competitive programming, there are many problems or subproblems
* to compute nCr modulo p where p is a given number.
* @author [Kaustubh Damania](https://github.com/KaustubhDamania)
*/ */
#include <cassert> #include <cassert> /// for assert
#include <iostream> #include <iostream> /// for io operations
#include <vector> #include <vector> /// for std::vector
/** Class which contains all methods required for calculating nCr mod p
* /**
* @namespace math
* @brief Mathematical algorithms
*/
namespace math {
/**
* @brief Class which contains all methods required for calculating nCr mod p
*/ */
class NCRModuloP { class NCRModuloP {
private: private:
std::vector<int64_t> fac; std::vector<uint64_t> fac;
int64_t p; uint64_t p;
public: public:
/** Constructor which precomputes the values of n! % mod from n=0 to size /** Constructor which precomputes the values of n! % mod from n=0 to size
* and stores them in vector 'fac' * and stores them in vector 'fac'
* @params[in] the numbers 'size', 'mod' * @params[in] the numbers 'size', 'mod'
*/ */
NCRModuloP(int64_t size, int64_t mod) { NCRModuloP(uint64_t size, uint64_t mod){
p = mod; p = mod;
fac = std::vector<int64_t>(size); fac = std::vector<uint64_t>(size);
fac[0] = 1; fac[0] = 1;
for (int i = 1; i <= size; i++) { for (int i = 1; i <= size; i++) {
fac[i] = (fac[i - 1] * i) % p; fac[i] = (fac[i - 1] * i) % p;
@@ -36,14 +45,14 @@ class NCRModuloP {
* equation * equation
* @returns the gcd of a and b * @returns the gcd of a and b
*/ */
int64_t gcdExtended(int64_t a, int64_t b, int64_t *x, int64_t *y) { uint64_t gcdExtended(uint64_t a, uint64_t b, int64_t *x, int64_t *y) {
if (a == 0) { if (a == 0) {
*x = 0, *y = 1; *x = 0, *y = 1;
return b; return b;
} }
int64_t x1 = 0, y1 = 0; int64_t x1 = 0, y1 = 0;
int64_t gcd = gcdExtended(b % a, a, &x1, &y1); uint64_t gcd = gcdExtended(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1; *x = y1 - (b / a) * x1;
*y = x1; *y = x1;
@@ -55,13 +64,14 @@ class NCRModuloP {
* @params[in] the numbers 'a' and 'm' from above equation * @params[in] the numbers 'a' and 'm' from above equation
* @returns the modular inverse of a * @returns the modular inverse of a
*/ */
int64_t modInverse(int64_t a, int64_t m) { int64_t modInverse(uint64_t a, uint64_t m) {
int64_t x = 0, y = 0; int64_t x = 0, y = 0;
int64_t g = gcdExtended(a, m, &x, &y); uint64_t g = gcdExtended(a, m, &x, &y);
if (g != 1) { // modular inverse doesn't exist if (g != 1) { // modular inverse doesn't exist
return -1; return -1;
} else { }
int64_t res = (x % m + m) % m; else {
int64_t res = ((x + m) % m);
return res; return res;
} }
} }
@@ -71,7 +81,7 @@ class NCRModuloP {
* @params[in] the numbers 'n', 'r' and 'p' * @params[in] the numbers 'n', 'r' and 'p'
* @returns the value nCr % p * @returns the value nCr % p
*/ */
int64_t ncr(int64_t n, int64_t r, int64_t p) { int64_t ncr(uint64_t n, uint64_t r, uint64_t p) {
// Base cases // Base cases
if (r > n) { if (r > n) {
return 0; return 0;
@@ -94,8 +104,15 @@ class NCRModuloP {
return (fac[n] * denominator) % p; return (fac[n] * denominator) % p;
} }
}; };
} // namespace math
void tests(NCRModuloP ncrObj) { /**
* @brief Test implementations
* @param ncrObj object which contains the precomputed factorial values and
* ncr function
* @returns void
*/
void tests(math::NCRModuloP ncrObj) {
// (52323 C 26161) % (1e9 + 7) = 224944353 // (52323 C 26161) % (1e9 + 7) = 224944353
assert(ncrObj.ncr(52323, 26161, 1000000007) == 224944353); assert(ncrObj.ncr(52323, 26161, 1000000007) == 224944353);
// 6 C 2 = 30, 30%5 = 0 // 6 C 2 = 30, 30%5 = 0
@@ -104,19 +121,20 @@ void tests(NCRModuloP ncrObj) {
assert(ncrObj.ncr(7,3,29) == 6); assert(ncrObj.ncr(7,3,29) == 6);
} }
/** /**
* @brief Main function * @brief Main function
* @returns 0 on exit * @returns 0 on exit
*/ */
int main() { int main() {
// populate the fac array // populate the fac array
const int64_t size = 1e6 + 1; const uint64_t size = 1e6 + 1;
const int64_t p = 1e9 + 7; const uint64_t p = 1e9 + 7;
NCRModuloP ncrObj = NCRModuloP(size, p); math::NCRModuloP ncrObj = math::NCRModuloP(size, p);
// test 6Ci for i=0 to 7 // test 6Ci for i=0 to 7
for (int i = 0; i <= 7; i++) { for (int i = 0; i <= 7; i++) {
std::cout << 6 << "C" << i << " = " << ncrObj.ncr(6, i, p) << "\n"; std::cout << 6 << "C" << i << " = " << ncrObj.ncr(6, i, p) << "\n";
} }
tests(ncrObj); tests(ncrObj); // execute the tests
std::cout << "Assertions passed\n"; std::cout << "Assertions passed\n";
} }