Pagini recente » Cod sursa (job #2292508) | Cod sursa (job #1855336) | Cod sursa (job #1999532) | Cod sursa (job #2290017) | Cod sursa (job #3250880)
#include <fstream>
using namespace std;
ifstream cin("inversmodular.in");
ofstream cout("inversmodular.out");
int gcd(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
else
{
int x0, y0, d;
d = gcd(b, a % b, x0, y0);
x = y0;
y = x0 - (a / b) * y0;
return d;
}
}
int main()
{
int a, n, x, y;
cin >> a >> n;
int d = gcd(a, n, x, y);
while (x < 0)
{
x += n;
}
cout << x;
return 0;
}