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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)

int main(void) {
  int a, b, c;
  while (cin >> a >> b >> c) {
    if (a == 0)
      break;
    bool possible = false;
    FOR(r, 1, a + 1) {
      int p = a / r;
      if (a % r != 0)
        continue;
      if (r * r > a)
        break;
      for (int q = 1; q <= abs(c); q++) {
        if (abs(c) % q != 0)
          continue;
        int s = c / q;
        if (p * s + q * r == b && p * r == a && q * s == c) {
          if (p == r && q < s)
            swap(q, s);
          printf("%d %d %d %d\\n", p, q, r, s);
          possible = true;
          goto END;
        }
        q *= -1;
        s *= -1;
        if (p * s + q * r == b && p * r == a && q * s == c) {
          if (p == r && q < s)
            swap(q, s);
          printf("%d %d %d %d\\n", p, q, r, s);
          possible = true;
          goto END;
        }
        q *= -1;
      }
    }
    if (c == 0) {
      int p = a;
      int r = 1;
      int q = b;
      int s = 0;
      if (a == 1 && q < s)
        swap(q, s);
      printf("%d %d %d %d\\n", p, q, r, s);
      possible = true;
    }
  END:;
    if (!possible) {
      cout << "Impossible" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)

int main(void) {
  int a, b, c;
  while (cin >> a >> b >> c) {
    if (a == 0)
      break;
    bool possible = false;
    FOR(r, 1, a) {
      if (abs(a) % r != 0)
        continue;
      if (r * r > abs(a))
        break;
      int p = a / r;
      for (int q = 1; q <= c; q++) {
        int s = c / q;
        if (p * s + q * r == b) {
          if (p == r && q < s)
            swap(q, s);
          printf("%d %d %d %d\\n", p, q, r, s);
          possible = true;
        }
        q *= -1;
        s *= -1;
        if (p * s + q * r == b) {
          if (p == r && q < s)
            swap(q, s);
          printf("%d %d %d %d\\n", p, q, r, s);
          possible = true;
        }
        q *= -1;
      }
    }
    if (c == 0) {
      int p = a;
      int r = 1;
      int q = b;
      int s = 0;
      printf("%d %d %d %d\\n", p, q, r, s);
      possible = true;
    }
    if (!possible) {
      cout << "Impossible" << endl;
    }
  }
  return 0;
}