Pagini recente » Cod sursa (job #991188) | Cod sursa (job #2895703) | Cod sursa (job #1605219) | Cod sursa (job #2872899) | Cod sursa (job #2884284)
// C++ program to find multiplicative modulo inverse using
// Extended Euclid algorithm.
#include<fstream>
using namespace std;
ifstream cin("inversmodular.in");
ofstream cout("inversmodular.in");
// C function for extended Euclidean Algorithm
int gcdExtended(int a, int b, int* x, int* y);
// Function to find modulo inverse of a
void modInverse(int a, int m)
{
int x, y;
int g = gcdExtended(a, m, &x, &y);
if (g != 1)
cout << "1";
else
{
// m is added to handle negative x
int res = (x % m + m) % m;
cout<< res;
}
}
// C function for extended Euclidean Algorithm
int gcdExtended(int a, int b, int* x, int* y)
{
// Base Case
if (a == 0)
{
*x = 0, * y = 1;
return b;
}
int x1, y1; // To store results of recursive call
int gcd = gcdExtended(b % a, a, &x1, &y1);
// Update x and y using results of recursive
// call
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
// Driver Program
int main()
{
int a, m;
cin >> a >> m;
modInverse(a, m);
return 0;
}