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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \\
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z)                                                         \\
  (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y  \\
        << ", " << z << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

int m, a, b;

vector<int> primes;

#define MAX 50001
bool ps[MAX];

void erat() {
  fill(ps, ps + MAX, true);
  ps[0] = ps[1] = false;
  REP(i, 2, MAX) {
    if (ps[i])
      primes.push_back(i);
    for (int j = 2 * i; j < MAX; j += i)
      ps[j] = false;
  }
}

int main(void) {
  erat();
  while (cin >> m >> a >> b, m | a | b) {
    int ans = 0;
    pii ansp;
    rep(i, primes.size()) {
      int p = primes[i];
      REP(j, i, primes.size()) {
        int q = primes[j];
        if (p * q > m)
          break;
        if (!(a * q <= p * b))
          continue;
        if (!(p <= q))
          continue;
        if (ans < p * q) {
          ans = p * q;
          ansp = pii(p, q);
        }
      }
    }
    printf("%d %d\\n", ansp.first, ansp.second);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \\
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z)                                                         \\
  (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y  \\
        << ", " << z << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

int m, a, b;

vector<int> primes;

#define MAX 100001
bool ps[MAX];

void erat() {
  fill(ps, ps + MAX, true);
  ps[0] = ps[1] = false;
  REP(i, 2, MAX) {
    if (ps[i])
      primes.push_back(i);
    for (int j = 2 * i; j < MAX; j += i)
      ps[j] = false;
  }
}

int main(void) {
  erat();
  while (cin >> m >> a >> b, m | a | b) {
    int ans = 0;
    pii ansp;
    rep(i, primes.size()) {
      int p = primes[i];
      REP(j, i, primes.size()) {
        int q = primes[j];
        if (p * q > m)
          break;
        if (!(a * q <= p * b))
          continue;
        if (!(p <= q))
          continue;
        if (ans < p * q) {
          ans = p * q;
          ansp = pii(p, q);
        }
      }
    }
    printf("%d %d\\n", ansp.first, ansp.second);
  }
  return 0;
}