@@ -33,16 +33,16 @@ namespace utils {
33
33
* equation
34
34
* @returns the gcd of a and b
35
35
*/
36
- uint64_t gcdExtended (const uint64_t & a, const uint64_t & b, int64_t & x,
37
- int64_t & y) {
36
+ int64_t gcdExtended (const int64_t & a, const int64_t & b, int64_t & x,
37
+ int64_t & y) {
38
38
if (a == 0 ) {
39
39
x = 0 ;
40
40
y = 1 ;
41
41
return b;
42
42
}
43
43
44
44
int64_t x1 = 0 , y1 = 0 ;
45
- uint64_t gcd = gcdExtended (b % a, a, x1, y1);
45
+ const int64_t gcd = gcdExtended (b % a, a, x1, y1);
46
46
47
47
x = y1 - (b / a) * x1;
48
48
y = x1;
@@ -54,14 +54,13 @@ uint64_t gcdExtended(const uint64_t& a, const uint64_t& b, int64_t& x,
54
54
* @params[in] the numbers 'a' and 'm' from above equation
55
55
* @returns the modular inverse of a
56
56
*/
57
- int64_t modInverse (const uint64_t & a, const uint64_t & m) {
57
+ int64_t modInverse (const int64_t & a, const int64_t & m) {
58
58
int64_t x = 0 , y = 0 ;
59
- uint64_t g = gcdExtended (a, m, x, y);
59
+ const int64_t g = gcdExtended (a, m, x, y);
60
60
if (g != 1 ) { // modular inverse doesn't exist
61
61
return -1 ;
62
62
} else {
63
- int64_t res = ((x + m) % m);
64
- return res;
63
+ return ((x + m) % m);
65
64
}
66
65
}
67
66
}; // namespace utils
@@ -70,8 +69,8 @@ int64_t modInverse(const uint64_t& a, const uint64_t& m) {
70
69
*/
71
70
class NCRModuloP {
72
71
private:
73
- const uint64_t p; // / the p from (nCr % p)
74
- const std::vector<uint64_t >
72
+ const int64_t p; // / the p from (nCr % p)
73
+ const std::vector<int64_t >
75
74
fac; // / stores precomputed factorial(i) % p value
76
75
77
76
/* *
@@ -81,11 +80,11 @@ class NCRModuloP {
81
80
* @return vector storing factorials of the numbers 0, ..., max_arg_val
82
81
* reduced modulo mod
83
82
*/
84
- static std::vector<uint64_t > computeFactorialsMod (
85
- const uint64_t & max_arg_val, const uint64_t & mod) {
86
- auto res = std::vector<uint64_t >(max_arg_val + 1 );
83
+ static std::vector<int64_t > computeFactorialsMod (const int64_t & max_arg_val,
84
+ const int64_t & mod) {
85
+ auto res = std::vector<int64_t >(max_arg_val + 1 );
87
86
res[0 ] = 1 ;
88
- for (uint64_t i = 1 ; i <= max_arg_val; i++) {
87
+ for (int64_t i = 1 ; i <= max_arg_val; i++) {
89
88
res[i] = (res[i - 1 ] * i) % mod;
90
89
}
91
90
return res;
@@ -96,15 +95,15 @@ class NCRModuloP {
96
95
* and stores them in vector 'fac'
97
96
* @params[in] the numbers 'size', 'mod'
98
97
*/
99
- NCRModuloP (const uint64_t & size, const uint64_t & mod)
98
+ NCRModuloP (const int64_t & size, const int64_t & mod)
100
99
: p(mod), fac(computeFactorialsMod(size, mod)) {}
101
100
102
101
/* * Find nCr % p
103
102
*
104
103
* @params[in] the numbers 'n', 'r' and 'p'
105
104
* @returns the value nCr % p
106
105
*/
107
- int64_t ncr (const uint64_t & n, const uint64_t & r) const {
106
+ int64_t ncr (const int64_t & n, const int64_t & r) const {
108
107
// Base cases
109
108
if (r > n) {
110
109
return 0 ;
@@ -135,14 +134,14 @@ class NCRModuloP {
135
134
*/
136
135
static void tests () {
137
136
struct TestCase {
138
- const uint64_t size;
139
- const uint64_t p;
140
- const uint64_t n;
141
- const uint64_t r;
137
+ const int64_t size;
138
+ const int64_t p;
139
+ const int64_t n;
140
+ const int64_t r;
142
141
const int64_t expected;
143
142
144
- TestCase (const uint64_t size, const uint64_t p, const uint64_t n,
145
- const uint64_t r, const int64_t expected)
143
+ TestCase (const int64_t size, const int64_t p, const int64_t n,
144
+ const int64_t r, const int64_t expected)
146
145
: size(size), p(p), n(n), r(r), expected(expected) {}
147
146
};
148
147
const std::vector<TestCase> test_cases = {
@@ -167,8 +166,8 @@ static void tests() {
167
166
*/
168
167
int main () {
169
168
// populate the fac array
170
- const uint64_t size = 1e6 + 1 ;
171
- const uint64_t p = 1e9 + 7 ;
169
+ const int64_t size = 1e6 + 1 ;
170
+ const int64_t p = 1e9 + 7 ;
172
171
const auto ncrObj = math::ncr_modulo_p::NCRModuloP (size, p);
173
172
// test 6Ci for i=0 to 7
174
173
for (int i = 0 ; i <= 7 ; i++) {
0 commit comments