#include <iostream>
using namespace std;
int gcd(int a, int b){ //欧几里得 求最大公约数
if(b == 0){
return a;
}else{
return gcd(b, a%b);
}
}
int lcm(int a,int b) //求最小公倍数
{
return a/gcd(a,b)*b;
}
void exgcd(int a, int b, int &d, int &x, int &y){ //扩展欧几里得
if(b == 0){
x = 1;
y = 0;
d = a;
}else{
exgcd(b, a%b, d, y, x);
y -= x * (a/b);
}
}
int main() //求解ax + by = c
{
int a, b, c, d, x, y;
cin >> a >> b >> c;
d = gcd(a, b);
if(c % d != 0){ //如果c % gcd(a,b) != 0,即c不是gcd的整数倍,则无解。
cout << "无解" << endl;
}else{
int t = c / d;
x = 1, y = 0;
exgcd(a, b, d, x, y);
x = t * x;
y = t * y;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
return 0;
}
详细https://blog.csdn.net/sdutstudent/article/details/78795643