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

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

using namespace std;

#define REP(i, s, e) for (int i = int(s); i <= int(e); i++)
#define rep(i, n) for (int i = 0; i < int(n); i++)

int g(int a, int b) {

  int r = max(a, b) % min(a, b);
  if (r == 0)
    return min(a, b);
  else
    return g(min(a, b), r);
}

int main() {

  int p, n;
  while (true) {

    cin >> p >> n;
    if (p == 0 && n == 0)
      break;

    int s1, s2, t1, t2;
    double d1 = 10000;
    double d2 = 10000;

    REP(i, 1, n) {

      int j = floor(i * sqrt((double)p));

      if (j <= n) {
        if (d1 > -(double)j / i + sqrt((double)p) &&
            -(double)j / i + sqrt((double)p) > 0) {

          d1 = -(double)j / i + sqrt((double)p);
          t1 = i;
          t2 = j;
        }
      }

      int k = ceil(i * sqrt((double)p));

      if (k <= n) {
        if (d2 > (double)k / i - sqrt((double)p) &&
            (double)k / i - sqrt((double)p) > 0) {

          d2 = (double)k / i - sqrt((double)p);
          s1 = i;
          s2 = k;
        }
      }
    }

    int g1 = g(s1, s2);
    int g2 = g(t1, t2);

    cout << s2 / g1 << "/" << s1 / g1 << " " << t2 / g2 << "/" << t1 / g2
         << endl;

    s1 = 0;
    s2 = 0;
    t1 = 0;
    t2 = 0;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define REP(i, s, e) for (int i = int(s); i <= int(e); i++)
#define rep(i, n) for (int i = 0; i < int(n); i++)

int g(int a, int b) {

  int r = max(a, b) % min(a, b);
  if (r == 0)
    return min(a, b);
  else
    return g(min(a, b), r);
}

int main() {

  int p, n;
  while (true) {

    cin >> p >> n;
    if (p == 0 && n == 0)
      break;

    int s1, s2, t1, t2;
    double d1 = 10000;
    double d2 = 10000;

    int a = floor(sqrt((double)p));
    int f = floor((double)n / (a + 1));
    int c = ceil((double)n / a);

    REP(j, n - c + 1, n - f) {
      REP(i, 1, j - (n - c)) {

        if (d1 > (double)j / i - sqrt((double)p) &&
            (double)j / i - sqrt((double)p) > 0) {

          d1 = (double)j / i - sqrt((double)p);
          s1 = i;
          s2 = j;
        }

        if (d2 > (double)-j / i + sqrt((double)p) &&
            -(double)j / i + sqrt((double)p) > 0) {

          d2 = -(double)j / i + sqrt((double)p);
          t1 = i;
          t2 = j;
        }
      }
    }

    REP(j, n - f + 1, n) {
      REP(i, j - (n - f), c) {

        if (d1 > (double)j / i - sqrt((double)p) &&
            (double)j / i - sqrt((double)p) > 0) {

          d1 = (double)j / i - sqrt((double)p);
          s1 = i;
          s2 = j;
        }

        if (d2 > (double)-j / i + sqrt((double)p) &&
            -(double)j / i + sqrt((double)p) > 0) {

          d2 = -(double)j / i + sqrt((double)p);
          t1 = i;
          t2 = j;
        }
      }
    }

    int g1 = g(s1, s2);
    int g2 = g(t1, t2);

    cout << s2 / g1 << "/" << s1 / g1 << " " << t2 / g2 << "/" << t1 / g2
         << endl;

    s1 = 0;
    s2 = 0;
    t1 = 0;
    t2 = 0;
  }

  return 0;
}