Pagini recente » Cod sursa (job #3343442) | Cod sursa (job #3343217) | Cod sursa (job #3338743) | Cod sursa (job #3319602) | Cod sursa (job #3356414)
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int extgcd(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
int x1, y1;
int d = extgcd(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}
int modInverse(int a, int n)
{
int x, y;
int g = extgcd(a, n, x, y);
if (g != 1) return -1;
x %= n;
if (x < 0) x += n;
return x;
}
int main(void)
{
int a, n;
fin >> a >> n;
fout << modInverse(a, n);
return 0;
}