Pagini recente » Cod sursa (job #581226) | Cod sursa (job #620657) | Cod sursa (job #2878390) | Cod sursa (job #2609302) | Cod sursa (job #1460952)
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int A, N;
void EuclidExtins( long long& x, long long& y, int a, int b );
int main()
{
long long x, y;
fin >> A >> N;
EuclidExtins( x, y, A, N );
if ( x <= 0 )
x = N + x % N;
fout << x << '\n';
fin.close();
fout.close();
return 0;
}
void EuclidExtins( long long& x, long long& y, int a, int b )
{
if ( b == 0 )
x = 1, y = 0;
else
{
EuclidExtins( x, y, b, a % b );
long long aux = x;
x = y;
y = aux - y * ( a / b );
}
}