44int64_t
gcdExtended(
const int64_t& a,
const int64_t& b, int64_t& x,
52 int64_t x1 = 0, y1 = 0;
55 x = y1 - (b / a) * x1;
82 const std::vector<int64_t>
94 auto res = std::vector<int64_t>(max_arg_val + 1);
96 for (int64_t i = 1; i <= max_arg_val; i++) {
97 res[i] = (res[i - 1] * i) % mod;
116 int64_t
ncr(
const int64_t& n,
const int64_t& r)
const {
124 if (r == 0 || r == n) {
128 const auto denominator = (
fac[r] *
fac[n - r]) % p;
129 const auto denominator_inv = utils::modInverse(denominator, p);
130 if (denominator_inv < 0) {
133 return (
fac[n] * denominator_inv) % p;
148 const int64_t expected;
150 TestCase(
const int64_t size,
const int64_t p,
const int64_t n,
151 const int64_t r,
const int64_t expected)
152 : size(size), p(p), n(n), r(r), expected(expected) {}
154 const std::vector<TestCase> test_cases = {
155 TestCase(60000, 1000000007, 52323, 26161, 224944353),
158 TestCase(1000, 13, 10, 3, 120 % 13),
164 for (
const auto& tc : test_cases) {
169 std::cout <<
"\n\nAll tests have successfully passed!\n";
176 const int64_t size = 1e6 + 1;
177 const int64_t p = 1e9 + 7;
185 for (
int i = 0; i <= 7; i++) {
186 std::cout << 6 <<
"C" << i <<
" mod " << p <<
" = " << ncrObj.ncr(6, i)
Class which contains all methods required for calculating nCr mod p.
int64_t ncr(const int64_t &n, const int64_t &r) const
computes nCr % p
const std::vector< int64_t > fac
the p from (nCr % p)
NCRModuloP(const int64_t &size, const int64_t &p)
constructs an NCRModuloP object allowing to compute (nCr)p for inputs from 0 to size
static std::vector< int64_t > computeFactorialsMod(const int64_t &max_arg_val, const int64_t &mod)
stores precomputed factorial(i) % p value
int gcd(int num1, int num2)
Functions for nCr modulo p implementation.
this namespace contains the definitions of the functions called from the class math::ncr_modulo_p::NC...
static void tests()
tests math::ncr_modulo_p::NCRModuloP
int64_t modInverse(const int64_t &a, const int64_t &m)
int64_t gcdExtended(const int64_t &a, const int64_t &b, int64_t &x, int64_t &y)
finds the values x and y such that a*x + b*y = gcd(a,b)
void example()
example showing the usage of the math::ncr_modulo_p::NCRModuloP class
represents single example inputs and expected output of the function longest_common_string_length