2020-05-28 04:09:50 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief C++ Program to find the modular inverse using [Fermat's Little
|
|
|
|
* Theorem](https://en.wikipedia.org/wiki/Fermat%27s_little_theorem)
|
2020-05-09 02:43:06 +08:00
|
|
|
*
|
2020-05-28 04:09:50 +08:00
|
|
|
* Fermat's Little Theorem state that \f[ϕ(m) = m-1\f]
|
|
|
|
* where \f$m\f$ is a prime number.
|
|
|
|
* \f{eqnarray*}{
|
|
|
|
* a \cdot x &≡& 1 \;\text{mod}\; m\\
|
|
|
|
* x &≡& a^{-1} \;\text{mod}\; m
|
|
|
|
* \f}
|
2020-05-09 02:43:06 +08:00
|
|
|
* Using Euler's theorem we can modify the equation.
|
2020-05-28 04:09:50 +08:00
|
|
|
*\f[
|
|
|
|
* a^{ϕ(m)} ≡ 1 \;\text{mod}\; m
|
|
|
|
* \f]
|
|
|
|
* (Where '^' denotes the exponent operator)
|
2020-05-09 02:43:06 +08:00
|
|
|
*
|
2020-05-28 04:09:50 +08:00
|
|
|
* Here 'ϕ' is Euler's Totient Function. For modular inverse existence 'a' and
|
|
|
|
* 'm' must be relatively primes numbers. To apply Fermat's Little Theorem is
|
|
|
|
* necessary that 'm' must be a prime number. Generally in many competitive
|
|
|
|
* programming competitions 'm' is either 1000000007 (1e9+7) or 998244353.
|
2020-05-09 02:43:06 +08:00
|
|
|
*
|
|
|
|
* We considered m as large prime (1e9+7).
|
2020-05-28 04:09:50 +08:00
|
|
|
* \f$a^{ϕ(m)} ≡ 1 \;\text{mod}\; m\f$ (Using Euler's Theorem)
|
|
|
|
* \f$ϕ(m) = m-1\f$ using Fermat's Little Theorem.
|
|
|
|
* \f$a^{m-1} ≡ 1 \;\text{mod}\; m\f$
|
|
|
|
* Now multiplying both side by \f$a^{-1}\f$.
|
|
|
|
* \f{eqnarray*}{
|
|
|
|
* a^{m-1} \cdot a^{-1} &≡& a^{-1} \;\text{mod}\; m\\
|
|
|
|
* a^{m-2} &≡& a^{-1} \;\text{mod}\; m
|
|
|
|
* \f}
|
2020-05-09 02:43:06 +08:00
|
|
|
*
|
2020-05-28 04:09:50 +08:00
|
|
|
* We will find the exponent using binary exponentiation. Such that the
|
|
|
|
* algorithm works in \f$O(\log m)\f$ time.
|
2020-05-09 02:43:06 +08:00
|
|
|
*
|
2020-05-28 04:09:50 +08:00
|
|
|
* Examples: -
|
|
|
|
* * a = 3 and m = 7
|
|
|
|
* * \f$a^{-1} \;\text{mod}\; m\f$ is equivalent to
|
|
|
|
* \f$a^{m-2} \;\text{mod}\; m\f$
|
|
|
|
* * \f$3^5 \;\text{mod}\; 7 = 243 \;\text{mod}\; 7 = 5\f$
|
|
|
|
* <br/>Hence, \f$3^{-1} \;\text{mod}\; 7 = 5\f$
|
|
|
|
* or \f$3 \times 5 \;\text{mod}\; 7 = 1 \;\text{mod}\; 7\f$
|
|
|
|
* (as \f$a\times a^{-1} = 1\f$)
|
2020-05-09 02:43:06 +08:00
|
|
|
*/
|
|
|
|
|
2020-05-28 04:09:50 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
2020-05-09 02:43:06 +08:00
|
|
|
|
2020-05-28 04:09:50 +08:00
|
|
|
/** Recursive function to calculate exponent in \f$O(\log n)\f$ using binary
|
|
|
|
* exponent.
|
|
|
|
*/
|
2020-05-30 07:26:30 +08:00
|
|
|
int64_t binExpo(int64_t a, int64_t b, int64_t m)
|
|
|
|
{
|
2020-05-09 02:43:06 +08:00
|
|
|
a %= m;
|
2020-05-09 02:51:24 +08:00
|
|
|
int64_t res = 1;
|
2020-05-30 07:26:30 +08:00
|
|
|
while (b > 0)
|
|
|
|
{
|
|
|
|
if (b % 2)
|
|
|
|
{
|
2020-05-09 02:43:06 +08:00
|
|
|
res = res * a % m;
|
|
|
|
}
|
|
|
|
a = a * a % m;
|
|
|
|
// Dividing b by 2 is similar to right shift.
|
|
|
|
b >>= 1;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-05-28 04:09:50 +08:00
|
|
|
/** Prime check in \f$O(\sqrt{m})\f$ time.
|
|
|
|
*/
|
2020-05-30 07:26:30 +08:00
|
|
|
bool isPrime(int64_t m)
|
|
|
|
{
|
|
|
|
if (m <= 1)
|
|
|
|
{
|
2020-05-09 19:57:29 +08:00
|
|
|
return false;
|
2020-05-30 07:26:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int64_t i = 2; i * i <= m; i++)
|
|
|
|
{
|
|
|
|
if (m % i == 0)
|
|
|
|
{
|
2020-05-09 19:57:29 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-28 04:09:50 +08:00
|
|
|
/**
|
|
|
|
* Main function
|
|
|
|
*/
|
2020-05-30 07:26:30 +08:00
|
|
|
int main()
|
|
|
|
{
|
2020-05-09 19:57:29 +08:00
|
|
|
int64_t a, m;
|
|
|
|
// Take input of a and m.
|
2020-05-09 20:01:48 +08:00
|
|
|
std::cout << "Computing ((a^(-1))%(m)) using Fermat's Little Theorem";
|
2020-05-11 06:55:16 +08:00
|
|
|
std::cout << std::endl << std::endl;
|
2020-05-09 20:01:48 +08:00
|
|
|
std::cout << "Give input 'a' and 'm' space separated : ";
|
2020-05-09 19:57:29 +08:00
|
|
|
std::cin >> a >> m;
|
2020-05-30 07:26:30 +08:00
|
|
|
if (isPrime(m))
|
|
|
|
{
|
2020-05-09 19:57:29 +08:00
|
|
|
std::cout << "The modular inverse of a with mod m is (a^(m-2)) : ";
|
2020-05-28 04:09:50 +08:00
|
|
|
std::cout << binExpo(a, m - 2, m) << std::endl;
|
2020-05-30 07:26:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-09 20:03:08 +08:00
|
|
|
std::cout << "m must be a prime number.";
|
2020-05-09 20:01:48 +08:00
|
|
|
std::cout << std::endl;
|
2020-05-09 19:57:29 +08:00
|
|
|
}
|
2020-05-09 02:43:06 +08:00
|
|
|
}
|