Cod sursa(job #370080)
Utilizator | Data | 30 noiembrie 2009 09:22:14 | |
---|---|---|---|
Problema | Invers modular | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.59 kb |
#include <algorithm>
using namespace std;
long long a,n,inv,ins;
void 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;
}