Pagini recente » Cod sursa (job #819796) | Cod sursa (job #1998531) | Cod sursa (job #203956) | Cod sursa (job #2494927) | Cod sursa (job #2008231)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
void euclid(int a, int b, int *d, int *x, int *y)
{
if (b == 0) {
*d = a;
*x = 1;
*y = 0;
} else {
int x0, y0;
euclid(b, a % b, d, &x0, &y0);
*x = y0;
*y = x0 - (a / b) * y0;
}
}
int a, n;
int main()
{
fin >> a >> n;
int x, y;
int d;
euclid( a, n, &d, &x, &y );
while( x < 0 )
x += n;
fout << x << "\n";
return 0;
}