Pagini recente » Cod sursa (job #2249946) | Cod sursa (job #2222831) | Monitorul de evaluare | Cod sursa (job #1471502) | Cod sursa (job #2061041)
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
long long a,n,x,y;
void inversModular(long long a, long long b, long long &x, long long &y)
{
if (b == 0)
{
x = 1;
y = 0;
}
else
{
long long xx, yy;
inversModular(b, a%b, xx, yy);
x = yy;
y = xx-(a/b)*yy;
}
}
int main()
{
fin >> a >> n;
inversModular(a, n, x, y);
if (x < 0)
x = (x+n*((-x)/n+1))%n;
fout << x;
return 0;
}