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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()
const int INF = 1e9;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PII;

/* struct P {
        int x, y, n;
        P(int n, int x, int y):n(n), x(x), y(y){}
        P(){}
}; */

/** Problem1232 : Calling Extraterrestrial Intelligence Again **/
int main() {
  const int MAX = 1e5;
  bool isPrime[MAX];
  memset(isPrime, 1, sizeof(isPrime));
  isPrime[0] = false, isPrime[1] = false;
  for (int i = 2; i < MAX; i++) {
    if (isPrime[i]) {
      for (int j = 2; i * j < MAX; j++) {
        isPrime[i * j] = false;
      }
    }
  }

  int m, a, b;
  while (cin >> m >> a >> b, m || a || b) {
    PII ans;

    for (int i = m; i >= 0; i--) {
      for (int j = 2; j * j <= m; j++) {
        if (i % j != 0)
          continue;
        if (isPrime[j] && isPrime[i / j]) {
          if ((double)a / b <= (double)j / (i / j)) {
            cout << j << " " << i / j << endl;
            goto end;
          }
        }
      }
    }
  end:;
  }
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()
const int INF = 1e9;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PII;

/* struct P {
        int x, y, n;
        P(int n, int x, int y):n(n), x(x), y(y){}
        P(){}
}; */

/** Problem1232 : Calling Extraterrestrial Intelligence Again **/
int main() {
  const int MAX = 1e5;
  bool isPrime[MAX];
  memset(isPrime, 1, sizeof(isPrime));
  isPrime[0] = false, isPrime[1] = false;
  for (int i = 2; i < MAX; i++) {
    if (isPrime[i]) {
      for (int j = 2; i * j < MAX; j++) {
        isPrime[i * j] = false;
      }
    }
  }

  int m, a, b;
  while (cin >> m >> a >> b, m || a || b) {
    int tmp = 0;
    PII ans;

    for (int i = 1; i <= m; i++) {
      if (!isPrime[i])
        continue;

      for (int j = i; j <= m; j++) {
        if (!isPrime[j] || i * j > m)
          continue;

        if ((double)i / j >= (double)a / b && i * j > tmp) {
          tmp = i * j;
          ans.first = i, ans.second = j;
        }
      }
    }

    cout << ans.first << " " << ans.second << endl;
  }
}