Pagini recente » Cod sursa (job #445745) | Cod sursa (job #842648) | Cod sursa (job #375958) | Cod sursa (job #409929) | Cod sursa (job #735302)
Cod sursa(job #735302)
#include <cstdio>
void euclid(int a,int b,int &d,int &x,int &y){
if(b==0){
x=1;
y=0;
d=a;} else {
int x0,y0;
euclid(b,a%b,d,x0,y0);
x=y0;
y=x0-a/b*y0; }
}
int main(){
int a,b,d,x,y;
freopen("inversmodular.in","r",stdin);
freopen("inversmodular.out","w",stdout);
scanf("%d %d",&a,&b);
euclid(a,b,d,x,y);
if(x>0)printf("%d\n",x); else printf("%d\n",x+((-x)/b+(x%b!=0))*b);
}