Advertisement
andreisophie

Euclid3 Iterativ

Sep 11th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4.  
  5. ifstream f("euclid3.in");
  6. ofstream g("euclid3.out");
  7.  
  8. void cmmdcE(long long int a, long long int b, long long int &d, long long int &x, long long int &y)
  9. {
  10.     int r,r0,r1,x0,x1,y0,y1,q;
  11.     r0=a;
  12.     r1=b;
  13.     x0=1;
  14.     y0=0;
  15.     x1=0;
  16.     y1=1;
  17.     while (r1)
  18.     {
  19.         q=r0/r1;
  20.         r=r0-r1*q;
  21.         r0=r1;
  22.         r1=r;
  23.         x=x0-x1*q;
  24.         x0=x1;
  25.         x1=x;
  26.         y=y0-y1*q;
  27.         y0=y1;
  28.         y1=y;
  29.     }
  30.     d=r0;
  31.     x=x0;
  32.     y=y0;
  33. }
  34.  
  35. void Diofantic(long long int a, long long int b, long long int c)
  36. {
  37.     long long int d,x0,y0;
  38.     cmmdcE(a,b,d,x0,y0);
  39.     if (c%d==0)
  40.         g<<x0*c/d<<' '<<y0*c/d<<'\n';
  41.     else
  42.         g<<"0 0\n";
  43. }
  44.  
  45. int main()
  46. {
  47.     long long int n,a,b,c;
  48.     f>>n;
  49.     for (int i=0;i<n;i++)
  50.     {
  51.         f>>a>>b>>c;
  52.         Diofantic(a,b,c);
  53.     }
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement