Advertisement
Guest User

Алгоритм Диофантовы уравнения

a guest
May 28th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int gcd(int a, int b, int & x, int & y) {
  5.     if (a == 0)
  6.     {
  7.         x = 0, y = 1;
  8.         return b;
  9.     }
  10.     int x1, y1;
  11.     int g = gcd(b % a, a, x1, y1);
  12.     x = y1 - (b / a) * x1;
  13.     y = x1;
  14.     return g;
  15. }
  16.  
  17. bool find_any_solutions(int a, int b, int c, int & x0, int & y0, int & g) {
  18.     g = gcd (abs(a), abs(b), x0, y0);
  19.     if (c % g != 0)
  20.         return false;
  21.     x0 *= c / g;
  22.     y0 *= c / g;
  23.     if (a < 0) x0 *= -1;
  24.     if (b < 0) y0 *= -1;
  25.     return true;
  26. }
  27. int main()
  28. {
  29.     int a, b, x1, y1, g, c;
  30.     cin >> a >> b >> c;
  31.     if (a > b) swap(a, b);
  32.     if (find_any_solutions(a, b, c, x1, y1, g))
  33.         cout << ' ' << x1 << ' ' << y1;
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement