Problem p00443 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

long long int gcd(long long int a, long long int b) {
  return b ? gcd(b, a % b) : a;
}
long long int lcm(long long int m, long long int n) {
  if (m == 0 || n == 0)
    return 0;
  return ((m / gcd(m, n)) * n);
}

int main() {

  while (true) {

    long long int n;
    cin >> n;
    if (n == 0)
      break;

    vector<long long int> p, q, r, b;
    for (long long int i = 0; i < n; i++) {
      long long int in_p, in_q, in_r, in_b;
      cin >> in_p >> in_q >> in_r >> in_b;
      p.push_back(in_p);
      q.push_back(in_q);
      r.push_back(in_r);
      b.push_back(in_b);
    }

    for (long long int i = 0; i < n; i++) {
      for (long long int j = 0; j < n; j++) {

        if (r[j] <= 0 && b[j] <= 0) {

          if (r[j] == 0) {
            r[j] = 1;
          } else {
            r[j] = -r[j];
          }
          long long int r2 = r[j] * p[j];
          if (b[j] == 0) {
            b[j] = 1;
          } else {
            b[j] = -b[j];
          }
          long long int b2 = b[j] * q[j];
          long long int h = lcm(r2, b2) / p[j] + lcm(r2, b2) / q[j];

          r[j] = n + 2;
          b[j] = n + 2;

          for (long long int k = 0; k < n; k++) {
            if (i == n - 1) {
              cout << h << endl;
              break;
            }
            if (r[k] == j + 1) {
              r[k] = -h;
              break;
            }
            if (b[k] == j + 1) {
              b[k] = -h;
              break;
            }
          }
          break;
        }
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

long long int gcd(long long int m, long long int n) {
  if (m == 0 || n == 0)
    return 0;
  while (m != n) {
    if (m > n) {
      m -= n;
    } else {
      n -= m;
    }
  }
  return m;
}
long long int lcm(long long int m, long long int n) {
  if (m == 0 || n == 0)
    return 0;
  return ((m / gcd(m, n)) * n);
}

int main() {

  while (true) {

    long long int n;
    cin >> n;
    if (n == 0)
      break;

    vector<long long int> p, q, r, b;
    for (long long int i = 0; i < n; i++) {
      long long int in_p, in_q, in_r, in_b;
      cin >> in_p >> in_q >> in_r >> in_b;
      p.push_back(in_p);
      q.push_back(in_q);
      r.push_back(in_r);
      b.push_back(in_b);
    }

    for (long long int i = 0; i < n; i++) {
      for (long long int j = 0; j < n; j++) {

        if (r[j] <= 0 && b[j] <= 0) {

          if (r[j] == 0) {
            r[j] = 1;
          } else {
            r[j] = -r[j];
          }
          long long int r2 = r[j] * q[j];
          if (b[j] == 0) {
            b[j] = 1;
          } else {
            b[j] = -b[j];
          }
          long long int b2 = b[j] * p[j];
          long long int h = lcm(r2, b2) / p[j] + lcm(r2, b2) / q[j];

          r[j] = n + 2;
          b[j] = n + 2;

          for (long long int k = 0; k < n; k++) {
            if (r[k] == j + 1) {
              r[k] = -h;
              break;
            }
            if (b[k] == j + 1) {
              b[k] = -h;
              break;
            }
            if (k == n - 1) {
              cout << h << endl;
              break;
            }
          }
        }
      }
    }
  }

  return 0;
}