Pagini recente » Cod sursa (job #1366827) | Cod sursa (job #1546788) | Cod sursa (job #476486) | Cod sursa (job #992123) | Cod sursa (job #3238766)
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
void euclid_ext(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1, y = 0;
return;
}
int px, py;
euclid_ext(b, a % b, px, py);
x = py;
y = px - (a / b) * py;
}
int main()
{
int a, b, x, y;
fin >> a >> b;
euclid_ext(a, b, x, y);
int temp = 1 / gcd(a, b) * x;
if (temp < 0)
{
int cnt = temp / b;
temp += (cnt + 1) * b;
}
fout << temp;
return 0;
}