gggyt  
没谁离不开谁

  算ax+by=gcd(a, b)

 

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);
    }
}

 

 

 

  算ax+by=c的解

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

bool linear_equation(int a,int b,int c,int &x,int &y)
{
    int d=exgcd(a,b,x,y);
    if(c%d)
        return false;
    int k=c/d;
    x*=k; y*=k;    //求得的只是其中一组解
    return true;
}

 

ll e_gcd(ll a,ll b,ll &x,ll &y)  {
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    ll ans=e_gcd(b,a%b,x,y);
    ll temp=x;
    x=y;
    y=temp-a/b*y;
    return ans;
}

     证明参考博客:http://blog.csdn.net/acmore_xiong/article/details/47694909

posted on 2017-11-06 20:53  gggyt  阅读(134)  评论(0编辑  收藏  举报