Cod sursa(job #3344505)
| Utilizator | Data | 2 martie 2026 10:40:02 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.56 kb |
#include <fstream>
using namespace std;
ifstream in("inversmodular.in");
ofstream out("inversmodular.out");
void euclid_extins(int a, int b, int d, int& x, int& y) {
if (b == 0)
{
d = a;
x = 1;
y = 0;
return;
}
int x_, y_, q = a / b;
euclid_extins(b, a % b, d, x_, y_);
x = y_;
y = x_ - q * y_;
}
int main()
{
int a, n;
in >> a >> n;
int x, y;
euclid_extins(a, n, 1, x, y);
if (x > 0)
out << x % n;
else
out << n + x % n;
return 0;
}