Pagini recente » Cod sursa (job #3343305) | Cod sursa (job #3347143) | Cod sursa (job #1440761) | Cod sursa (job #67557) | Cod sursa (job #3341223)
#include <iostream>
int bezout_gcd(int a, int &x, int b, int &y)
{
int r_x, r_y, d;
if (b == 0) {
x = 1;
y = 0;
d = a;
} else {
d = bezout_gcd(b, r_x, a % b, r_y);
x = r_y;
y = r_x - (a / b) * r_y;
}
return d;
}
int main()
{
int a, n;
int x, y;
freopen("inversmodular.in", "r", stdin);
freopen("inversmodular.out", "w", stdout);
std::cin >> a >> n;
bezout_gcd(a, x, n, y);
while (x < 0)
x += n;
std::cout << x << '\n';
return 0;
}