Advertisement
paranid5

Диафантово уравнение

Sep 20th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. typedef long long ll
  4.  
  5. template<typename T>
  6. T gcd_ext(const T a, const T b, T& x, T& y)
  7. {
  8.     if (b == 0)
  9.     {
  10.         x = 1;
  11.         y = 0;
  12.         return a;
  13.     }
  14.    
  15.     const T d = gcd_ext(b, a % b, x, y);
  16.     x -= (a / b) * y;
  17.     std::swap(x, y);
  18.     return d;
  19. }
  20.  
  21. int main()
  22. {
  23.     ll a = 0, b = 0, x = 0, y = 0, c = 0;
  24.     std::scanf("%lld%lld%lld", &a, &b, &c);
  25.    
  26.     const ll d = gcd_ext(a, b, &x, &y);
  27.    
  28.     if (c % d != 0)
  29.         std::puts("-1");
  30.        
  31.     else
  32.     {
  33.         ll t = 0;
  34.         const ll Y = y;
  35.         const ll X = x;
  36.        
  37.         x = (c / d) * X;
  38.         y = (c / d) * Y;
  39.        
  40.         if (x == 0)
  41.             goto END;
  42.            
  43.         else if (x > 0)
  44.         {
  45.             while (x > 0)
  46.             {
  47.                 t--;
  48.                 x = (c / d) * X + t * (b / d);
  49.                 y = (c / d) * Y - t * (a / d);
  50.             }
  51.            
  52.             if (x == 0)
  53.                 goto END;
  54.                
  55.             t++; //доходим до сюда, когда x < 0
  56.             x = (c / d) * X + t * (b / d);
  57.             y = (c / d) * Y - t * (a / d);
  58.         }
  59.        
  60.         else
  61.         {
  62.             while (x < 0)
  63.             {
  64.                 t++;
  65.                 x = (c / d) * X + t * (b / d);
  66.                 y = (c / d) * Y - t * (a / d);
  67.             }
  68.         }
  69.        
  70.     END: std::printf("%lld %lld", x, y);
  71.     }
  72.    
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement