Cod sursa(job #359012)
Utilizator | Prodan Diana prdiana | Data | 25 octombrie 2009 14:17:15 |
---|---|---|---|
Problema | Invers modular | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.42 kb |
#include <stdio.h>
inline void gcd(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1;
y = 0;
return;
}
int d,x0,y0;
gcd(b, a % b,x0,y0);
x = y0;
y = x0 - (a/b)*y0;
}
int main()
{
int x,y,a,b;
freopen("inversmodular.in","r",stdin);
freopen("inversmodular.out","w",stdout);
scanf("%d%d",&a,&b);
gcd(a,b,x,y);
if (x < 0)
{
x = b + x % b;
}
printf("%d",x);
return 0;
}