fork(1) download
  1. /*
  2.   Copyright 2012 Marek "p2004a" Rusinowski
  3.   Extended Euclidean algorithm
  4. */
  5. #include <cstdio>
  6.  
  7. int gcd(int a, int b, int &x, int &y) {
  8. if (b == 0) {
  9. x = 1, y = 0;
  10. return a;
  11. }
  12. int d = gcd(b, a % b, y, x);
  13. y -= x * (a / b);
  14. return d;
  15. }
  16.  
  17. int main() {
  18. int a, b, x, y;
  19. scanf("%d %d", &a, &b);
  20. int d = gcd(a, b, x, y);
  21. printf("%d * %d + %d * %d = %d\n", x, a, y, b, d);
  22. return 0;
  23. }
  24.  
Success #stdin #stdout 0.01s 2684KB
stdin
24 38
stdout
8 * 24 + -5 * 38 = 2