Pagini recente » Cod sursa (job #2343844) | Cod sursa (job #302050) | Cod sursa (job #2316706) | Cod sursa (job #1108941) | Cod sursa (job #547974)
Cod sursa(job #547974)
#include <stdio.h>
int A,N,x,y;
inline void euclid(int a,int b,int &x,int &y)
{
int x0,y0;
if(b==0)
{
x=1;
y=0;
}
else
{
euclid(b,a%b,x0,y0);
x=y0;
y=x0-(a/b)*y0;
}
}
int main()
{
freopen("inversmodular.in","r",stdin);
freopen("inversmodular.out","w",stdout);
scanf("%d%d",&A,&N);
x=0;
y=0;
euclid(A,N,x,y);
if(x>=0) printf("%d\n",x);
else
{
y=x;
x+=(y/N)*N;
if(y%N) x+=N;
printf("%d\n",x);
}
return 0;
}