|
43 | 43 | * (as \f$a\times a^{-1} = 1\f$)
|
44 | 44 | */
|
45 | 45 |
|
46 |
| -#include <cassert> /// for assert |
47 |
| -#include <iostream> /// for IO implementations |
| 46 | +#include <cassert> /// for assert |
| 47 | +#include <cstdint> /// for int64_t |
| 48 | +#include <iostream> /// for IO implementations |
48 | 49 |
|
49 | 50 | /**
|
50 | 51 | * @namespace math
|
51 |
| - * @brief Maths algorithms |
| 52 | + * @brief Maths algorithms. |
52 | 53 | */
|
53 | 54 | namespace math {
|
54 | 55 | /**
|
55 |
| - * @namespace modular_inverse_fermat_little_theorem |
56 |
| - * @brief Calculate modular inverse using Fermat's Little Theorem |
| 56 | + * @namespace modular_inverse_fermat |
| 57 | + * @brief Calculate modular inverse using Fermat's Little Theorem. |
57 | 58 | */
|
58 |
| -namespace modular_inverse_fermat_little_theorem { |
59 |
| -/** |
60 |
| - * @brief Calculate exponent with modulo using divide-and-conquer. |
| 59 | +namespace modular_inverse_fermat { |
| 60 | +/** |
| 61 | + * @brief Calculate exponent with modulo using binary exponentiation. |
61 | 62 | * @param a The base
|
62 | 63 | * @param b The exponent
|
63 | 64 | * @param m The modulo
|
64 |
| - * @return res The result of \f$a^{b} % m\f$ |
| 65 | + * @return The result of \f$a^{b} % m\f$ |
65 | 66 | */
|
66 |
| -int64_t binExpo(int64_t a, int64_t b, int64_t m) { |
67 |
| - a %= m; |
68 |
| - int64_t res = 1; |
69 |
| - while (b > 0) { |
70 |
| - if (b % 2) { |
71 |
| - res = res * a % m; |
72 |
| - } |
73 |
| - a = a * a % m; |
74 |
| - // Dividing b by 2 is similar to right shift. |
75 |
| - b >>= 1; |
| 67 | +std::int64_t binExpo(std::int64_t a, std::int64_t b, std::int64_t m) { |
| 68 | + a %= m; |
| 69 | + std::int64_t res = 1; |
| 70 | + while (b > 0) { |
| 71 | + if (b % 2 != 0) { |
| 72 | + res = res * a % m; |
76 | 73 | }
|
77 |
| - return res; |
| 74 | + a = a * a % m; |
| 75 | + // Dividing b by 2 is similar to right shift by 1 bit |
| 76 | + b >>= 1; |
| 77 | + } |
| 78 | + return res; |
78 | 79 | }
|
79 | 80 | /**
|
80 |
| - * @brief Check if a given integer is a prime number |
| 81 | + * @brief Check if a given integer is a prime number. |
81 | 82 | * @param m An intger to check for primality
|
82 |
| - * @return 'true' if the number is prime |
83 |
| - * @return 'false' if the number is not prime |
| 83 | + * @return 'true' if the number is prime, otherwise 'false' |
84 | 84 | */
|
85 |
| -bool isPrime(int64_t m) { |
86 |
| - if (m <= 1) { |
87 |
| - return false; |
88 |
| - } else { |
89 |
| - for (int64_t i = 2; i * i <= m; i++) { |
90 |
| - if (m % i == 0) { |
91 |
| - return false; |
92 |
| - } |
93 |
| - } |
| 85 | +bool isPrime(std::int64_t m) { |
| 86 | + if (m <= 1) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + for (std::int64_t i = 2; i * i <= m; i++) { |
| 90 | + if (m % i == 0) { |
| 91 | + return false; |
94 | 92 | }
|
95 |
| - return true; |
| 93 | + } |
| 94 | + return true; |
96 | 95 | }
|
97 | 96 | /**
|
98 |
| - * @brief Main function to calculate modular inverse |
99 |
| - * @param a Integer value for base |
| 97 | + * @brief Main function to calculate the modular inverse. |
| 98 | + * @param a Integer value for the base |
100 | 99 | * @param m Integer value for modulo
|
101 |
| - * @return the result that is the modular inverse of a modulo m |
| 100 | + * @return The result that is the modular inverse of a modulo m |
102 | 101 | */
|
103 |
| -int64_t modular_inverse(int64_t a, int64_t m) { |
104 |
| - // modulo m is not prime |
105 |
| - if (!isPrime(m)) { |
106 |
| - return -1; // Using -1 to mark for invalid input |
107 |
| - } |
108 |
| - return binExpo(a, m - 2, m); |
| 102 | +std::int64_t modular_inverse(std::int64_t a, std::int64_t m) { |
| 103 | + while (a < 0) { |
| 104 | + a += m; |
| 105 | + } |
| 106 | + |
| 107 | + // Check for invalid cases |
| 108 | + if (!isPrime(m) || a == 0) { |
| 109 | + return -1; // Invalid input |
| 110 | + } |
| 111 | + |
| 112 | + return binExpo(a, m - 2, m); // Fermat's Little Theorem |
109 | 113 | }
|
110 |
| -} // namespace modular_inverse_fermat_little_theorem |
111 |
| -} // namespace math |
| 114 | +} // namespace modular_inverse_fermat |
| 115 | +} // namespace math |
112 | 116 |
|
113 | 117 | /**
|
114 | 118 | * @brief Self-test implementation
|
115 | 119 | * @return void
|
116 | 120 | */
|
117 | 121 | static void test() {
|
118 |
| - assert(math::modular_inverse_fermat_little_theorem::modular_inverse(3, 6) == -1); |
119 |
| - assert(math::modular_inverse_fermat_little_theorem::modular_inverse(3, 7) == 5); |
120 |
| - assert(math::modular_inverse_fermat_little_theorem::modular_inverse(1, 1) == 1); |
121 |
| - assert(math::modular_inverse_fermat_little_theorem::modular_inverse(1, 1) == 1); |
122 |
| - assert(math::modular_inverse_fermat_little_theorem::modular_inverse(1, 1) == 1); |
123 |
| - assert(math::modular_inverse_fermat_little_theorem::modular_inverse(1, 1) == 1); |
| 122 | + assert(math::modular_inverse_fermat::modular_inverse(0, 97) == -1); |
| 123 | + assert(math::modular_inverse_fermat::modular_inverse(15, -2) == -1); |
| 124 | + assert(math::modular_inverse_fermat::modular_inverse(3, 10) == -1); |
| 125 | + assert(math::modular_inverse_fermat::modular_inverse(3, 7) == 5); |
| 126 | + assert(math::modular_inverse_fermat::modular_inverse(1, 101) == 1); |
| 127 | + assert(math::modular_inverse_fermat::modular_inverse(-1337, 285179) == 165519); |
| 128 | + assert(math::modular_inverse_fermat::modular_inverse(123456789, 998244353) == 25170271); |
| 129 | + assert(math::modular_inverse_fermat::modular_inverse(-9876543210, 1000000007) == 784794281); |
124 | 130 | }
|
125 | 131 |
|
126 | 132 | /**
|
127 | 133 | * @brief Main function
|
128 | 134 | * @return 0 on exit
|
129 | 135 | */
|
130 | 136 | int main() {
|
131 |
| - test(); |
132 |
| - return 0; |
| 137 | + test(); // run self-test implementation |
| 138 | + return 0; |
133 | 139 | }
|
0 commit comments