about summary refs log tree commit diff
path: root/2ndary/09/TP-HN-2014/cau1.cpp
blob: 274cef6776f8f627b3b5ba808f8259c24e6de666 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <fstream>

using namespace std;

long
gcd(long x, long y)
{
  long z;
  while (y)
    {
      z = x;
      x = y;
      y = z % x;
    }

  return x;
}

int
main()
{
  ifstream infile;
  long a, b, c, d;
  infile.open("CAU1.INP");
  infile >> a >> b >> c >> d;
  infile.close();

  long y = b * d / gcd(b, d);
  long x = a * y / b - c * y / d;
  if (!x)
    y = 1;
  else
    {
      a = gcd(x, y);
      x /= a;
      y /= a;
      if (y < 0)
        {
          x *= -1;
          y *= -1;
        }
    }

  ofstream outfile;
  outfile.open("CAU1.OUT");
  outfile << x << ' ' << y << endl;
  outfile.close();

  return 0;
}