首页 > 代码库 > 7C-扩展欧几里德解线性方程

7C-扩展欧几里德解线性方程

Ax+By+C=0,给你A,B,C求x,y

#include<iostream>#include<cstdio>#include<cstring>using namespace std;long long INF = 5 * 1e18;void gcd(long long a, long long b, long long& d,long long& x, long long& y){    if(!b) {d = a; x = 1; y = 0;}    else    {        gcd(b, a % b, d, y, x);        y -= x * (a / b);    }}int main(int argc, char *argv[]){    long long a, b, c, d, x, y;    cin >> a >> b >> c;    gcd(a, b, d, x, y);    if(c % d != 0)        puts("-1");    else        cout << -x * (c / d) << " " << -y * (c / d) << endl;    return 0;}

 

7C-扩展欧几里德解线性方程