Pagini recente » Cod sursa (job #2627178) | Cod sursa (job #1316770) | Cod sursa (job #2706154) | Cod sursa (job #289243) | Cod sursa (job #2376080)
#include <fstream>
std::ifstream fin("inversmodular.in");
std::ofstream fout("inversmodular.out");
int gcd(int a, int b, int& x, int& y) {
if (!b) {
x = 1;
y = 0;
return a;
}
int x0, y0;
int d = gcd(b, a % b, x0, y0);
x = y0;
y = x0 - a / b * y0;
return d;
}
int main() {
int a, n;
fin >> a >> n;
int x, y;
gcd(a, n, x, y);
if (x < 0)
x += n;
fout << x << '\n';
fout.close();
return 0;
}