Cod sursa(job #3341223)
| Utilizator | Data | 18 februarie 2026 16:28:27 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.54 kb |
#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;
}
