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