扩展欧几里得

扩展欧几里德:



对于gcd(a, b) == d, 存在


a*x+b*y == gcd(a, b);


--->a*x1+b*y1 == gcd (b, a%b);


--->a*x1+b*y1 == b*x2 + a%b*y2


--->a*x1+b*y1 == b*x2 + (a-(a/b)*b)*y2


--->a*x1+b*y1 == a*y2 + b*(x2-(a/b)*y2)



则得:x1 = y2; y1 = x2 - (a/b)* y2 ;


当b==0时,x==1, y==0, 然后递归求解;


void biggcd (LL a, LL b, LL &x, LL &y)
{///扩展欧几里得
    if (!b)
    {
        x = 1;
        y = 0;
        return;
    }

    else
    {
        biggcd (b, a%b, x, y);
        LL tem = y;
        y = x - a/b * y;
        x = tem;
    }
}


你可能感兴趣的:(扩展欧几里得)