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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 60) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int m;
double a, b;
double num[100100];
P ans[100100];

vector<int> prime;
bool is_prime[MAX_N + 1];

int sieve(int n) { // n??\\???????´???°?????°?????????
  int p = 0;
  for (int i = 0; i <= n; i++)
    is_prime[i] = 1;
  is_prime[0] = is_prime[1] = 0;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime.push_back(i);
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = 0;
    }
  }
  return p;
}

int main() {
  sieve(100000);
  fill(num, num + 100100, 0);
  rep(i, 100100) ans[i] = {-1, -1};
  rep(i, prime.size()) {
    rep(j, i + 1) {
      double q = prime[i], p = prime[j];
      if (p * q <= 100000) {
        if (num[(int)(p * q)] < p / q) {
          num[(int)(p * q)] = p / q;
          ans[(int)(p * q)] = {p, q};
        }
      }
    }
  }
  while (cin >> m >> a >> b && m + a + b) {
    for (int i = m; i >= 0; i--) {
      if (a / b < num[i] + EPS) {
        cout << ans[i].first << " " << ans[i].second << endl;
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 60) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int m;
double a, b;

vector<int> prime;
bool is_prime[MAX_N + 1];

int sieve(int n) { // n??\\???????´???°?????°?????????
  int p = 0;
  for (int i = 0; i <= n; i++)
    is_prime[i] = 1;
  is_prime[0] = is_prime[1] = 0;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime.push_back(i);
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = 0;
    }
  }
  return p;
}

int main() {
  sieve(100000);
  while (cin >> m >> a >> b && m + a + b) {
    int ans = 0;
    int aa = -1, ab = -1;
    rep(i, prime.size()) {
      rep(j, i + 1) {
        double q = prime[i], p = prime[j];
        if (p * q <= m && p / q < 1 + EPS && a / b < p / q + EPS) {
          if (ans < p * q) {
            ans = p * q;
            aa = p, ab = q;
          }
        }
      }
    }
    cout << aa << " " << ab << endl;
  }
}