Problem p00811 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, b, n) for (int i = b; i < n; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
const int MAX_N = 100010;
int prime[MAX_N];         // i番目の素数
bool is_prime[MAX_N + 1]; // is_prime[i]がtrueならiは素数

// n以下の素数の数を返す
int sieve(int n) {
  int p = 0;
  for (int i = 0; i <= n; i++) {
    is_prime[i] = true;
  }
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return p;
}
int main() {
  sieve(100008);
  double m, a, b;
  while (cin >> m >> a >> b, m || a || b) {
    double k = a / b;
    int tmp = 0;
    int ans = 0;
    int ansx = 0;
    int ansy = 0;
    for (int i = 2; i <= sqrt(m); i++) {
      if (!is_prime[i])
        continue;
      for (int j = i; j <= m; j++) {
        if (!is_prime[j])
          continue;
        double l = (double)i / j;
        if (m >= i * j && k <= l && l <= 1 && i * j > ans) {
          ans = i * j;
          // cout <<k<<" "<<l<<endl;
          ansx = i;
          ansy = j;
        }
      }
    }
    cout << ansx << " " << ansy << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, b, n) for (int i = b; i < n; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
const int MAX_N = 100010;
int prime[MAX_N];         // i番目の素数
bool is_prime[MAX_N + 1]; // is_prime[i]がtrueならiは素数

// n以下の素数の数を返す
int sieve(int n) {
  int p = 0;
  for (int i = 0; i <= n; i++) {
    is_prime[i] = true;
  }
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return p;
}
int main() {
  sieve(100008);
  double m, a, b;
  while (cin >> m >> a >> b, m || a || b) {
    double k = a / b;
    int tmp = 0;
    int ans = 0;
    int ansx = 0;
    int ansy = 0;
    for (int i = 2; i <= sqrt(m); i++) {
      if (!is_prime[i])
        continue;
      for (int j = i; j <= m / 2; j++) {
        if (!is_prime[j])
          continue;
        double l = (double)i / j;
        if (m >= i * j && k <= l && l <= 1 && i * j > ans) {
          ans = i * j;
          // cout <<k<<" "<<l<<endl;
          ansx = i;
          ansy = j;
        }
      }
    }
    cout << ansx << " " << ansy << endl;
  }
  return 0;
}