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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

const int N = 50005;
bool prime[N];
vector<int> primes;
void hurui() {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;

  for (int i = 2; i * i < N; i++) {
    for (int j = 2; i * j < N; j++) {
      prime[i * j] = false;
    }
  }
  REP(i, N + 1) {
    if (prime[i])
      primes.push_back(i);
  }
}

int main() {
  hurui();
  int m, a, b;
  while (cin >> m >> a >> b, m) {
    struct ans {
      int m, p, q;
    };
    ans t = {-1, -1, -1};
    REP(i, primes.size()) {
      if (primes[i] > m / 2)
        break;
      FOR(j, i, primes.size()) {
        if (primes[i] * primes[j] > m || a * primes[j] > b * primes[i])
          break;

        if (primes[i] * primes[j] <= m && a * primes[j] <= b * primes[i]) {
          if (primes[i] * primes[j] > t.m)
            t = {primes[i] * primes[j], primes[i], primes[j]};
        }
      }
    }
    cout << t.p << " " << t.q << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

const int N = 50005;
bool prime[N];
vector<int> primes;
void hurui() {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;

  for (int i = 2; i * i < N; i++) {
    for (int j = 2; i * j < N; j++) {
      prime[i * j] = false;
    }
  }
  REP(i, N + 1) {
    if (prime[i])
      primes.push_back(i);
  }
}

int main() {
  hurui();
  int m, a, b;
  while (cin >> m >> a >> b, m) {
    struct ans {
      int m, p, q;
    };
    ans t = {-1, -1, -1};
    REP(i, primes.size()) {
      if (primes[i] > m / 2)
        break;
      FOR(j, i, primes.size()) {
        if (primes[i] * primes[j] <= m && a * primes[j] <= b * primes[i]) {
          if (primes[i] * primes[j] > t.m)
            t = {primes[i] * primes[j], primes[i], primes[j]};
        }
      }
    }
    cout << t.p << " " << t.q << endl;
  }
}