扩展欧几里得

扩展欧几里得:已知a, b,求解一组x,y,使它们满足贝祖等式: ax+by =gcd(a, b)

ax1+by1=gcd(a,b)

bx2+(a%b)y2=gcd(b,a%b)

gcd(a,b)=gcd(b,a%b)

ax1+by1 = bx2+(a%b)y2

ax1+by1=bx2+(a-a/b*b)y2

ax1+by1=ay2+b(x2-a/b*y2)

x1=y2; y1=x2-a/b*y2

int exgcd(int a,int b,int &x,int &y){
	if(b==0){
		x=1;
		y=0;
		return a;
	}
	int gcd=exgcd(b,a%b,x,y);
	int x2=x,y2=y;
	x=y2;
	y=x2-a/b*y2;
	return gcd;
}
int exgcd(int a,int b,int &x,int &y){
	if(b==0){
		x=1;
		y=0;
		return a;
	}
	int gcd=exgcd(b,a%b,y,x);
	y-=(a/b)*x;
	return gcd;
}

 

你可能感兴趣的:(我的算法小笔记)