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

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define SQ(x) ((x) * (x))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

bool sieve[100000];
int prime[10000];

int main() {
  sieve[0] = sieve[1] = true;
  int idx = 0;
  FOR(i, 2, 100000) {
    if (sieve[i])
      continue;
    prime[idx] = i;
    idx++;
    for (int j = i + i; j < 100000; j += i) {
      sieve[j] = true;
    }
  }

  int m, a, b;
  while (cin >> m >> a >> b, m | a | b) {
    int ma = 0;
    int P, Q;
    REP(i, idx) {
      ll p = prime[i];
      if (p < m) {
        int B = b * p / a;
        FOR(j, i, idx) {
          ll q = prime[j];
          if (q <= B && p * q <= m) {
            if (ma < p * q) {
              ma = p * q;
              P = p;
              Q = q;
            }
          } else {
            break;
          }
        }
      } else {
        break;
      }
    }
    printf("%d %d\\n", P, Q);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define SQ(x) ((x) * (x))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

bool sieve[100000];
int prime[10000];

int main() {
  sieve[0] = sieve[1] = true;
  int idx = 0;
  FOR(i, 2, 100000) {
    if (sieve[i])
      continue;
    prime[idx] = i;
    idx++;
    for (int j = i + i; j < 100000; j += i) {
      sieve[j] = true;
    }
  }

  int m, a, b;
  while (cin >> m >> a >> b, m | a | b) {
    int ma = 0;
    int P, Q;
    REP(i, idx) {
      int p = prime[i];
      if (p <= m) {
        REP(j, idx) {
          int q = prime[j];
          if (p * q <= m && a * q <= b * p && p <= q) {
            if (ma < p * q) {
              ma = p * q;
              P = p;
              Q = q;
            }
          }
        }
      }
    }
    printf("%d %d\\n", P, Q);
  }
}