alexjercan/bugnet huggingface dataset:
Problem p00784 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define fr(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define pb push_back
#define mp make_pair

int n, ng;
char in[4][21];

int main() {
  while (scanf("%d", &n), n) {
    rep(i, 5) scanf("%s", in[i]);
    ng = 0;
    for (int i = 0; i <= n; i++)
      rep(j, 5) rep(k, n - i) if (in[j][i + k] == 'X' && in[j][k] == 'X') ng |=
          1 << i;

    set<pair<int, int>> S;
    priority_queue<pair<int, pair<int, int>>> Q;
    Q.push(mp(-n, mp(1, ng)));
    while (!Q.empty()) {
      int cc = Q.top().first, cnt = Q.top().second.first;
      int bit = Q.top().second.second;
      Q.pop();
      if (S.count(mp(cnt, bit)))
        continue;
      S.insert(mp(cnt, bit));
      if (cnt == 10) {
        printf("%d\\n", -cc);
        break;
      }
      for (int i = 1; i <= n; i++)
        if (!(bit & 1 << i))
          if (!S.count(mp(cnt + 1, bit >> i | ng)))
            Q.push(mp(cc - i, mp(cnt + 1, bit >> i | ng)));
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define fr(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define pb push_back
#define mp make_pair

int n, ng;
char in[4][21];
set<pair<int, int>> S;
void pv(int b) {
  rep(i, n) cerr << !!(b & 1 << i) << (i == n - 1 ? "\\n" : " ");
}

int main() {
  while (scanf("%d", &n), n) {
    rep(i, 5) scanf("%s", in[i]);
    ng = 0;
    for (int i = 0; i <= n; i++)
      rep(j, 5) rep(k, n - i) if (in[j][i + k] == 'X' && in[j][k] == 'X') ng |=
          1 << i;

    S.clear();
    priority_queue<pair<int, pair<int, int>>> Q;
    Q.push(mp(-n, mp(1, ng)));
    int ans = 1 << 20;
    while (!Q.empty()) {
      int cc = Q.top().first, cnt = Q.top().second.first;
      int bit = Q.top().second.second;
      Q.pop();
      if (S.count(mp(cnt, bit)))
        continue;
      S.insert(mp(cnt, bit));
      if (cnt == 10) {
        printf("%d\\n", -cc);
        break;
      }
      for (int i = 1; i <= n; i++)
        if (!(bit & 1 << i))
          if (!S.count(mp(cnt + 1, bit >> i | ng)))
            Q.push(mp(cc - i, mp(cnt + 1, bit >> i | ng)));
    }
  }
  return 0;
}
Problem p00788 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

long long gcd(long long a, long long b) { // Ååöñ
  if (a == 0 || b == 0)
    return 0;
  if (b > a)
    swap(a, b);
  long long tmp;
  while ((tmp = a % b) != 0) {
    a = b;
    b = tmp;
  }
  return b;
}

int main() {
  for (;;) {
    int p, n;
    cin >> p >> n;
    if (p == 0)
      return 0;

    int u = 0;
    int v = 1;
    int x = INT_MAX / 2;
    int y = 1;

    for (int i = 1; i <= n; ++i) { // ªê
      int j = i * sqrt(double(p)); // ªq
      if (j > n)
        break;
      if (gcd(i, j) == 1 && i * u < j * v) {
        u = j;
        v = i;
      }

      ++j;
      if (j > n)
        break;
      if (gcd(i, j) == 1 && i * x > j * y) {
        x = j;
        y = i;
      }
    }
    cout << x << '/' << y << ' ' << u << '/' << v << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

long long gcd(long long a, long long b) { // Ååöñ
  if (a == 0 || b == 0)
    return 0;
  if (b > a)
    swap(a, b);
  long long tmp;
  while ((tmp = a % b) != 0) {
    a = b;
    b = tmp;
  }
  return b;
}

int main() {
  for (;;) {
    int p, n;
    cin >> p >> n;
    if (p == 0)
      return 0;

    int u = 0;
    int v = 1;
    int x = INT_MAX / 2;
    int y = 1;

    for (int i = 1; i <= n; ++i) {   // ªq
      for (int j = 1; j <= n; ++j) { // ªê
        if (gcd(i, j) != 1)
          continue;
        if (i * i > p * j * j) {
          if (i * y < j * x) {
            x = i;
            y = j;
          }
        } else {
          if (i * v > j * u) {
            u = i;
            v = j;
          }
        }
      }
    }
    cout << x << '/' << y << ' ' << u << '/' << v << endl;
  }
}
Problem p00788 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
const int INF = 10001;

int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }

class R {
public:
  double a, b;
  R(int aa, int bb) {
    a = aa;
    b = bb;
    int r;
    while ((r = gcd(a, b)) != 1) {
      a /= r;
      b /= r;
    }
  }
  bool operator>(const R &r) const { return a / b > r.a / r.b; }
  bool operator<(const R &r) const { return a / b < r.a / r.b; }
};

int main() {
  double P;
  int N;
  while (cin >> P >> N && (P || N)) {
    P = sqrt(P);
    R lower = R(-1, 1), upper = R(INF, 1);
    FOR(i, 1, N + 1) {
      int l = 0, r = N + 1;
      while (r - l > 1) {
        double m = (r + l) / 2;
        if (m / i >= P) {
          r = m;
          upper = min(upper, R(r, i));
        } else {
          l = m;
          lower = max(lower, R(l, i));
        }
      }
    }
    cout << upper.a << "/" << upper.b << " " << lower.a << "/" << lower.b
         << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())

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

class R {
public:
  int a, b;
  R() {}
  R(int aa, int bb) {
    a = aa;
    b = bb;
    while (gcd(a, b) != 1) {
      int r = gcd(a, b);
      a /= r;
      b /= r;
    }
  }
  bool operator<(const R &r) const { return a / (double)b < r.a / (double)r.b; }
};

int P, N;

int solve(vector<R> &v) {
  REP(i, (int)v.size() - 1) {
    double a = (v[i].a * v[i].a) / (double)(v[i].b * v[i].b),
           b = (v[i + 1].a * v[i + 1].a) / (double)(v[i + 1].b * v[i + 1].b);
    if (a > P && P > b)
      return i;
  }
  return 0;
}

int main() {
  while (cin >> P >> N && (P || N)) {
    set<R> S;
    FOR(a, 1, N + 1) FOR(b, 1, N + 1) S.insert(R(a, b));
    vector<R> v(S.begin(), S.end());
    REVERSE(v);
    int idx = solve(v);
    cout << v[idx].a << "/" << v[idx].b << " " << v[idx + 1].a << "/"
         << v[idx + 1].b << endl;
  }
  return 0;
}
Problem p00788 (C++) — buggy submission, verdict: Time Limit Exceeded

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

double mysqrt(double x) {
  double l = 0, r = x;
  for (int i = 0; i < 64; i++) {
    double m = (l + r) / 2.0;
    if (m * m < x) {
      l = m;
    } else {
      r = m;
    }
  }
  return l;
}

int main() {
  int p, n;
  while (cin >> p >> n, n) {
    double sp = mysqrt(p);
    int a1 = 0, b1 = 1, a2 = p, b2 = 1;

    for (int i = 1; i <= n; i++) {

      int l = 0, r = n;

      for (int x = 0; x < 50; x++) {
        int m = (l + r + 1) / 2;
        if ((double)m / i < sp) {
          if (__gcd(m, i) == 1) {
            if (m * b1 > a1 * i) {
              a1 = m;
              b1 = i;
            }
          }
          l = m;
        } else {
          r = m - 1;
        }
      }

      l = 0, r = n;
      for (int x = 0; x < 50; x++) {
        int m = (l + r) / 2;
        if ((double)m / i > sp) {
          if (__gcd(m, i) == 1) {
            if (m * b2 < a2 * i) {
              a2 = m;
              b2 = i;
            }
          }
          r = m;
        } else {
          l = m + 1;
        }
      }
    }

    cout << a2 << "/" << b2 << " " << a1 << "/" << b1 << endl;
  }
}

Error: TLE

Fixed submission:

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

double mysqrt(double x) {
  double l = 0, r = x;
  for (int i = 0; i < 64; i++) {
    double m = (l + r) / 2.0;
    if (m * m < x) {
      l = m;
    } else {
      r = m;
    }
  }
  return l;
}

int main() {
  int p, n;
  while (cin >> p >> n, n) {
    double sp = mysqrt(p);
    int a1 = 0, b1 = 1, a2 = p, b2 = 1;

    for (int i = 1; i <= n; i++) {

      int l = 0, r = n;

      for (int x = 0; x < 64; x++) {
        int m = (l + r) / 2;
        if ((double)m / i < sp) {
          if (__gcd(m, i) == 1) {
            if (m * b1 > a1 * i) {
              a1 = m;
              b1 = i;
            }
          }
          l = m;
        } else {
          r = m - 1;
        }
      }

      l = 0, r = n;
      for (int x = 0; x < 64; x++) {
        int m = (l + r + 1) / 2;
        if ((double)m / i > sp) {
          if (__gcd(m, i) == 1) {
            if (m * b2 < a2 * i) {
              a2 = m;
              b2 = i;
            }
          }
          r = m;
        } else {
          l = m + 1;
        }
      }
    }

    cout << a2 << "/" << b2 << " " << a1 << "/" << b1 << endl;
  }
}
Problem p00788 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
typedef pair<int, int> PII;
int gcd(int x, int y) { return (x % y) ? (gcd(y, x % y)) : y; }

int main() {
  int p, n;
  while (cin >> p >> n && p) {
    double sp = sqrt(p);
    PII p1, p2;
    double val1 = INF, val2 = 0;
    for (int j = 1; j <= n; j++) {
      int i = (int)(sp * j);
      double x = (double)(i + 1) / j;
      if (gcd(i + 1, j) == 1 && i + 1 <= n && val1 > x) {
        p1 = PII(i + 1, j);
        val1 = x;
      }
      x = (double)i / j;
      if (gcd(i, j) == 1 && i <= n && val2 < x) {
        p2 = PII(i, j);
        val2 = x;
      }
    }
    printf("%d/%d %d/%d\\n", p1.first, p1.second, p2.first, p2.second);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
typedef pair<int, int> PII;
typedef pair<double, PII> S;
int gcd(int x, int y) { return (x % y) ? (gcd(y, x % y)) : y; }

int main() {
  int p, n;
  while (cin >> p >> n && p) {
    double sp = sqrt(p);
    vector<S> v;
    v.push_back(S(sp, PII(-1, -1)));
    PII p1, p2;
    double val1 = INF, val2 = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (gcd(i, j) == 1) {
          double x = (double)i / j;
          if (x > sp && val1 > x) {
            p1 = PII(i, j);
            val1 = x;
          } else if (x < sp && val2 < x) {
            p2 = PII(i, j);
            val2 = x;
          }
        }
      }
    }
    printf("%d/%d %d/%d\\n", p1.first, p1.second, p2.first, p2.second);
  }
  return 0;
}
Problem p00788 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  while (1) {
    int p, n;

    scanf(" %d %d", &p, &n);
    if (p == 0)
      break;

    int x, y, u, v;
    double rp = sqrt(p);
    double big = 10000, small = 0;

    int re = (int)rp;

    for (int i = 1; i <= n; ++i) {
      for (int j = rp * i; j <= n; ++j) {
        double a = (double)j / i;

        if (rp < a) {
          if (a < big) {
            big = a;
            x = j;
            y = i;
          }

          break;
        } else {
          if (small < a) {
            small = a;
            u = j;
            v = i;
          }
        }
      }
    }

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

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  while (1) {
    int p, n;

    scanf(" %d %d", &p, &n);
    if (p == 0)
      break;

    int x, y, u, v;
    double rp = sqrt(p);
    double big = 10000, small = 0;

    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        double a = (double)j / i;

        if (rp < a) {
          if (a < big) {
            big = a;
            x = j;
            y = i;
          }

          break;
        } else {
          if (small < a) {
            small = a;
            u = j;
            v = i;
          }
        }
      }
    }

    printf("%d/%d %d/%d\\n", x, y, u, v);
  }
}
Problem p00788 (C++) — buggy submission, verdict: Time Limit Exceeded

// 08
#include <cmath>
#include <iostream>

using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  for (int p, n; cin >> p >> n, p | n;) {
    double a, b, c, d;
    double sp = sqrt(p);
    bool l = false, h = false;
    for (double i = 1; i <= n; i++) {
      for (double j = max(int(i / sp), 1); j <= min(n, int(i / sp + 1)); j++) {
        if (gcd(i, j) == 1) {
          if (i / j < sp) {
            if (!l || i / j > a / b) {
              a = i;
              b = j;
              l = true;
            }
          } else {
            if (!h || i / j < c / d) {
              c = i;
              d = j;
              h = true;
            }
          }
        }
      }
    }
    cout << c << '/' << d << ' ' << a << '/' << b << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 08
#include <cmath>
#include <iostream>

using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  for (int p, n; cin >> p >> n, p | n;) {
    double a, b, c, d;
    bool l = false, h = false;
    for (double i = 1; i <= n; i++) {
      for (double j = 1; j <= n; j++) {
        if (gcd(i, j) == 1) {
          if (i / j < sqrt(p)) {
            if (!l || i / j > a / b) {
              a = i;
              b = j;
              l = true;
            }
          } else {
            if (!h || i / j < c / d) {
              c = i;
              d = j;
              h = true;
            }
          }
        }
      }
    }
    cout << c << '/' << d << ' ' << a << '/' << b << endl;
  }
  return 0;
}
Problem p00788 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<double, double> P;
int p, n;
int d[] = {-2, -1, 0, 1, 2};

int main() {
  while (cin >> p >> n, p | n) {
    P a = P(0, 1);
    P b = P(1200900, 1);
    double np = sqrt(p);
    // cout << "np = " << np << endl;
    for (double v = 1; v <= n; v++) {
      for (int i = 0; i < 5; i++) {
        int ux = np * v + d[i];
        if (ux > n)
          continue;
        if (__gcd(ux, (int)v) != 1)
          continue;
        // cout << ux << "/" << v << endl;
        if (ux / v < np && np - a.first / a.second > np - ux / v)
          a = P(ux, v);
        if (ux / v > np && b.first / b.second - np > ux / v - np)
          b = P(ux, v);
      }
    }

    cout << b.first << "/" << b.second << " " << a.first << "/" << a.second
         << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<double, double> P;
int p, n;
int d[] = {-2, -1, 0, 1, 2};

int main() {
  while (cin >> p >> n, p | n) {
    P a = P(0, 1);
    P b = P(1200900, 1);
    double np = sqrt(p);
    // cout << "np = " << np << endl;
    for (double v = 1; v <= n; v++) {
      for (double u = n; u > sqrt(np * v * v); u--) {
        if (np <= u / v)
          goto Next;
        if (__gcd((int)u, (int)v) != 1)
          goto Next;
        if (np - a.first / a.second > np - u / v)
          a = P(u, v);

      Next:;
        if (np >= u / v)
          continue;
        if (__gcd((int)u, (int)v) != 1)
          continue;
        if (b.first / b.second - np > u / v - np)
          b = P(u, v);
      }
    }

    cout << b.first << "/" << b.second << " " << a.first << "/" << a.second
         << endl;
  }
  return 0;
}
Problem p00788 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <utility>

using namespace std;

#define INF (1 << 28)

bool operator<(const pair<int, int> &a, const pair<int, int> &b) {
  return a.first * b.second < b.first * a.second;
}

void reduce(pair<int, int> &a) {
  int gcd = __gcd(a.first, a.second);
  a.first /= gcd;
  a.second /= gcd;
}

int main() {
  int p, n;
  while (cin >> p >> n) {
    if (p == 0 && n == 0)
      break;
    double sqrt_p = sqrt(p);

    pair<int, int> lb(0, 1), ub(INF, 1);
    for (int i = 1; i <= n; i++) {
      pair<int, int> l = make_pair(floor(sqrt_p * i), i);
      if (l.first <= n && lb < l)
        lb = l;
      pair<int, int> u = make_pair(ceil(sqrt_p * i), i);
      if (u.first <= n && u < ub)
        ub = u;
    }

    reduce(lb);
    reduce(ub);

    cout << ub.first << '/' << ub.second << ' ';
    cout << lb.first << '/' << lb.second << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <utility>
#include <vector>
using namespace std;

bool comp(const pair<int, int> &a, const pair<int, int> &b) {
  return (double)a.first / (double)a.second <
         (double)b.first / (double)b.second;
}

pair<int, int> operator*(const pair<int, int> &a, const pair<int, int> &b) {
  return make_pair(a.first * b.first, a.second * b.second);
}

double f(const pair<int, int> &a) { return (double)a.first / (double)a.second; }

void reduce(pair<int, int> &a) {
  int gcd = __gcd(a.first, a.second);
  a.first /= gcd;
  a.second /= gcd;
}

int main() {
  int p, n;
  while (cin >> p >> n) {
    if (p == 0 && n == 0)
      break;
    set<pair<int, int>> rs;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        pair<int, int> p = make_pair(i, j);
        reduce(p);
        rs.insert(p);
      }
    }
    pair<int, int> lb(0, 1), ub(n, 1);
    for (set<pair<int, int>>::iterator it = rs.begin(); it != rs.end(); it++) {
      double sq_r = f((*it) * (*it));
      if (sq_r < p && f(lb) < f(*it)) {
        lb = *it;
      } else if (p < sq_r && f(*it) < f(ub)) {
        ub = *it;
      }
    }

    cout << ub.first << '/' << ub.second << ' ' << lb.first << '/' << lb.second
         << endl;
  }
  return 0;
}
Problem p00788 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int INF = 1 << 28;
const ll MOD = 1000000007;

int n, p, s1, s2, l1, l2;
void sternBrocot(int p, int n, int pl, int ql, int pr, int qr) {
  int pm = pl + pr, qm = ql + qr;
  if (pm > n || qm > n)
    return;
  if (pm * pm > p * qm * qm) {
    l1 = pm;
    l2 = qm;
    sternBrocot(p, n, pl, ql, pm, qm);
  } else {
    s1 = pm;
    s2 = qm;
    sternBrocot(p, n, pm, qm, pr, qr);
  }
}
int main() {
  while (cin >> p >> n, p | n) {
    sternBrocot(p, n, 0, 1, 1, 0);
    cout << l1 << '/' << l2 << ' ' << s1 << '/' << s2 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int INF = 1 << 28;
const ll MOD = 1000000007;

int n;
double p, l, r;
string ansl, ansr;

void sternBrocot(int pl, int ql, int pr, int qr) {
  int pm = pl + pr, qm = ql + qr;
  double v = (double)pm / qm;
  if (pm >= n || qm >= n)
    return;
  if (p < v && v < r) {
    r = v;
    ansr = to_string(pm) + '/' + to_string(qm);
  }
  if (v < p && l < v) {
    l = v;
    ansl = to_string(pm) + '/' + to_string(qm);
  }
  sternBrocot(pl, ql, pm, qm);
  sternBrocot(pm, qm, pr, qr);
}
int main() {
  l = 0;
  r = INF;
  while (cin >> p >> n, (int)p | n) {
    p = sqrt(p);
    sternBrocot(0, 1, 1, 0);
    cout << ansr << ' ' << ansl << endl;
  }
  return 0;
}