参考资料: 扩展欧几里得算法
讲的很清晰简单,直接附代码
#include<iostream>
using namespace std;
int x2, y2; // 记录上一层的x,y
void exgcd(int a, int b, int& x, int& y){
if(b == 0){
x2 = 1;
y2 = 0;
return ;
}
exgcd(b, a % b, x, y);
x = y2; //递归回来时,用上层的x,y更新本层的x,y
y = x2 - (a / b) * y2;
x2 = x, y2 = y;
}
int main(){
int n;
cin >> n;
while(n--){
int a, b, x, y;
cin >> a >> b;
exgcd(a, b, x, y);
cout << x << " " << y << endl;
}
return 0;
}