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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int gcd(int a, int b) {
  if (a > b)
    swap(a, b);
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

int main() {
  int p, n;
  while ((p = getInt()) + (n = getInt())) {
    double pp = sqrt((double)p);
    int x, y, u, v;
    double g1 = 1e10;
    double g2 = 1e10;

    rep(i, 1, n + 1) {
      double jj = i / pp;
      int j1 = (int)(floor(jj) + 1e-10);
      int j2 = (int)(ceil(jj) + 1e-10);

      if (gcd(i, j1) == 1) {
        double t = (double)i / j1;
        double g = t - pp;
        // printf("i=%d, j1=%d, g=%f\\n",i,j1,g);
        if (g < g1) {
          g1 = g;
          x = i;
          y = j1;
        }
      }
      if (gcd(i, j2) == 1) {
        double t = (double)i / j2;
        double g = pp - t;
        // printf("i=%d, j2=%d, g=%f\\n",i,j2,g);
        if (g < g2) {
          g2 = g;
          u = i;
          v = j2;
        }
      }
    }

    printf("%d/%d %d/%d\\n", x, y, u, v);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int gcd(int a, int b) {
  if (a > b)
    swap(a, b);
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

int main() {
  int p, n;
  while ((p = getInt()) + (n = getInt())) {
    double pp = sqrt((double)p);
    int x, y, u, v;
    double g1 = 1e10;
    double g2 = 1e10;
    rep(i, 1, n + 1) rep(j, 1, n + 1) if (gcd(i, j) == 1) {
      double tmp = (double)i / j;
      double g = tmp - pp;

      if (g > 0.0) {
        if (g < g1) {
          g1 = g;
          u = i;
          v = j;
        }
      } else {
        g = -g;
        if (g < g2) {
          g2 = g;
          x = i;
          y = j;
        }
      }
    }
    printf("%d/%d %d/%d\\n", u, v, x, y);
  }
  return 0;
}