1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 using namespace std; 5 typedef long long LL; 6 // 求x和y使得ax+by=d并且|x|+|y|最小。其中d=gcd(a,b) 7 void exgcd(LL a,LL b,LL& d,LL& x,LL& y){ 8 if(!b) d = a,x = 1,y = 0; 9 else{ 10 exgcd(b,a % b,d,y,x); 11 y -= x * (a / b); 12 } 13 } 14 int main(){ 15 LL a,b,d,x,y; 16 a = 10,b = 14; 17 exgcd(a,b,d,x,y); 18 printf("d = %lld x = %lld y = %lld ",d,x,y); 19 }