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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repp(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for (int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP << " " <<
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define CFS                                                                    \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)

typedef long long LL;
typedef long double LD;

int main() {
  vector<bool> hurui(100000, true);
  hurui[0] = hurui[1] = false;
  repp(i, 2, 100000) {
    if (hurui[i])
      for (int j = i + i; j < 100000; j += i)
        hurui[j] = false;
  }
  vector<int> prime;
  rep(i, 100000) if (hurui[i]) prime.push_back(i);
  int n = prime.size();
  while (1) {
    int m, a, b;
    cin >> m >> a >> b;
    if (m == 0)
      return 0;
    int ma = 0;
    int ansp, ansq;
    rep(i, n) {
      if (prime[i] * prime[i] > m)
        break;
      int l = i, r = n, mid;
      while (r - l > 1) {
        mid = (l + r) / 2;
        if (prime[i] * prime[mid] <= m) {
          l = mid;
        } else {
          r = mid;
        }
      }
      int x = l;
      l = i, r = n;
      while (r - l > 1) {
        mid = (l + r) / 2;
        if (a * prime[mid] <= b * prime[i]) {
          l = mid;
        } else {
          r = mid;
        }
      }
      int y = l;
      int j = min(x, y);
      if (ma < prime[i] * prime[j]) {
        ma = prime[i] * prime[j];
        ansp = prime[i];
        ansq = prime[j];
      }
    }
    cout << ansp SP ansq << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repp(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for (int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP << " " <<
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define CFS                                                                    \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)

typedef long long LL;
typedef long double LD;

int main() {
  vector<bool> hurui(300000, true);
  hurui[0] = hurui[1] = false;
  repp(i, 2, 300000) {
    if (hurui[i])
      for (int j = i + i; j < 300000; j += i)
        hurui[j] = false;
  }
  vector<int> prime;
  rep(i, 300000) if (hurui[i]) prime.push_back(i);
  int n = prime.size();
  while (1) {
    int m, a, b;
    cin >> m >> a >> b;
    if (m == 0)
      return 0;
    int ma = 0;
    int ansp, ansq;
    rep(i, n) {
      repp(j, i, n) {
        if (a * prime[j] > b * prime[i])
          break;
        if (prime[i] * prime[j] > m)
          break;
        if (ma < prime[i] * prime[j]) {
          ma = prime[i] * prime[j];
          ansp = prime[i];
          ansq = prime[j];
        }
      }
    }
    cout << ansp SP ansq << endl;
  }
  return 0;
}