Pagini recente » Cod sursa (job #2451522) | Cod sursa (job #793736) | Cod sursa (job #23586) | Cod sursa (job #380001) | Cod sursa (job #2933954)
#include <bits/stdc++.h>
using namespace std;
ifstream in("inversmodular.in");
ofstream out("inversmodular.out");
void euclid(int a,int b,int &x,int &y,int &d)
{
if (b == 0)
{
d = a;
x = 1;
y = 0;
}
else
{
int x0,y0;
euclid(b,a % b,x0,y0,d);
x = y0;
y = x0 - (a / b) * y0;
}
}
int main()
{
int a,n,x,y,d;
in >> a >> n;
euclid(a,n,x,y,d);
if (x < 0)
{
int dv = x / n;
x += (dv * n);
if (x < 0)
x += n;
}
out << x;
return 0;
}