EXGCD 的两个问题

第一个问题,用exgcd求解出来的一组x和y与b和a的关系;

void exgcd(ll a,ll b,ll&d,ll& x,ll& y)
{
    if(!b)  {d=a;x=1;y=0;}
    else  {exgcd(b,a%b,d,y,x);y-=x*(a/b);}
}

经过我的反复验证,我猜想,x和y的绝对值回比b和a要来的小。

this means exgcd(a,b,d,x,y)------>|x|<b,|y|<a;


the second q,about minium solution of equation ax+by=c;

first step:we solve the equation:ax0+by0=gcd(a,b)=m;


second step,we got x0 and y0,then

we mulitiply the primary equation by c/m;

we got:ax0*(c/m)+by0*(c/m0) = c;


third step: we can calculate the minium but nonnegative number of x,that is also the solution of the primary equation:

ax+by=c;

then x=(x0*(c/m)%b+b)%b;

and m is equal to m/g;


finaly ,the answer x = (x0*(c/m)%b+b)%b;


and some theory of tong yu equation:

1.a+b)%m=a%m+b%m;

2.mu you le...


another thing:

if ax≡b(mod c)

then we got ax+cy=b;

then we can use exgcd();

猜你喜欢

转载自blog.csdn.net/ivanzn/article/details/80539861