Pagini recente » Cod sursa (job #1754741) | Cod sursa (job #648914) | Cod sursa (job #3223428) | Cod sursa (job #298445) | Cod sursa (job #370079)
Cod sursa(job #370079)
#include <algorithm>
using namespace std;
long long a,n,inv,ins;
long long euclid_extins (long long &x,long long &y,long long a,long long b)
{
long long aux;
if (!b)
{
x=1;
y=0;
}
else
{
euclid_extins (x,y,b,a%b);
aux=x;
x=y;
y=aux-y*(a/b);
}
}
int main ()
{
freopen ("inversmodular.in","r",stdin);
freopen ("inversmodular.out","w",stdout);
scanf ("%lld%lld",&a,&n);
euclid_extins (inv,ins,a,n);
if (inv<=0)
inv=n+inv%n;
printf ("%lld",inv);
return 0;
}