Pagini recente » Cod sursa (job #3160545) | Cod sursa (job #1859330) | Cod sursa (job #2718137) | Cod sursa (job #2271817) | Cod sursa (job #735296)
Cod sursa(job #735296)
#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))*b);
}