Pagini recente » kk-kk | Cod sursa (job #2699339) | Cod sursa (job #477447) | Cod sursa (job #743483) | Cod sursa (job #1745186)
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
void euclid(int a, int b, int* d, long long* x, long long* y);
int main()
{
int a, n, d;
long long x, y;
fin >> a >> n;
euclid(a, n, &d, &x, &y);
if (x <= 0)
x = n + x % n;
fout << x << '\n';
fin.close();
fout.close();
return 0;
}
void euclid(int a, int b, int* d, long long* x, long long* y)
{
if (b == 0)
{
*d = a;
*x = 1;
*y = 0;
}
else
{
long long x0, y0;
euclid(b, a % b, d, &x0, &y0);
*x = y0;
*y = x0 - (a / b) * y0;
}
}