Cod sursa(job #3250880)
Utilizator | Data | 24 octombrie 2024 03:09:48 | |
---|---|---|---|
Problema | Invers modular | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.56 kb |
#include <fstream>
using namespace std;
ifstream cin("inversmodular.in");
ofstream cout("inversmodular.out");
int gcd(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
else
{
int x0, y0, d;
d = gcd(b, a % b, x0, y0);
x = y0;
y = x0 - (a / b) * y0;
return d;
}
}
int main()
{
int a, n, x, y;
cin >> a >> n;
int d = gcd(a, n, x, y);
while (x < 0)
{
x += n;
}
cout << x;
return 0;
}