Cod sursa(job #2174649)
Utilizator | Sirbu Radu-Mihai radumihaisirbu | Data | 16 martie 2018 12:52:37 |
---|---|---|---|
Problema | Invers modular | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.51 kb |
#include <fstream>
using namespace std;
ifstream fin ("euclid3.in");
ofstream fout ("euclid3.out");
int gcd (int a, int b, int &x, int &y)
{
int x0, y0, d;
if (b == 0)
{
x = 1;
y = 0;
return a;
}
d = gcd (b, a % b, x0, y0);
x = y0;
y = x0 - (a / b) * y0;
return d;
}
int main()
{
int A, B, X, Y, i, n, c, D;
fin >> A >> n;
D = gcd (A, n, X, Y);
X = X * D;
while (X < 0)
X += n;
fout << X;
return 0;
}