Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
ncr_modulo_p.cpp File Reference

This program aims at calculating nCr modulo p. More...

#include <cassert>
#include <iostream>
#include <vector>
Include dependency graph for ncr_modulo_p.cpp:

Classes

class  math::ncr_modulo_p::NCRModuloP
 Class which contains all methods required for calculating nCr mod p. More...
 

Namespaces

namespace  math
 for IO operations
 
namespace  ncr_modulo_p
 Functions for nCr modulo p implementation.
 

Functions

static void tests (math::ncr_modulo_p::NCRModuloP ncrObj)
 Test implementations.
 
int main ()
 Main function.
 

Detailed Description

This program aims at calculating nCr modulo p.

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

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
137 {
138 // populate the fac array
139 const uint64_t size = 1e6 + 1;
140 const uint64_t p = 1e9 + 7;
143 // test 6Ci for i=0 to 7
144 for (int i = 0; i <= 7; i++) {
145 std::cout << 6 << "C" << i << " = " << ncrObj.ncr(6, i, p) << "\n";
146 }
147 tests(ncrObj); // execute the tests
148 std::cout << "Assertions passed\n";
149 return 0;
150}
Class which contains all methods required for calculating nCr mod p.
Definition ncr_modulo_p.cpp:31
int64_t ncr(const uint64_t &n, const uint64_t &r, const uint64_t &p)
Definition ncr_modulo_p.cpp:92
Testcases to check Union of Two Arrays.
Here is the call graph for this function:

◆ tests()

static void tests ( math::ncr_modulo_p::NCRModuloP ncrObj)
static

Test implementations.

Parameters
ncrObjobject which contains the precomputed factorial values and ncr function
Returns
void
124 {
125 // (52323 C 26161) % (1e9 + 7) = 224944353
126 assert(ncrObj.ncr(52323, 26161, 1000000007) == 224944353);
127 // 6 C 2 = 30, 30%5 = 0
128 assert(ncrObj.ncr(6, 2, 5) == 0);
129 // 7C3 = 35, 35 % 29 = 8
130 assert(ncrObj.ncr(7, 3, 29) == 6);
131}
Here is the call graph for this function: