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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline long toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
inline long toLong(string s) {
  long v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (long i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

const long double EPS = 1e-20;
const long double INF = 1e18;

int main() {
  long m, a, b;
  vector<long> prime;
  for (long i = 2; i <= 100009; i++) {
    bool flag = true;
    for (long j = 2; j * j <= i; j++)
      if (i % j == 0)
        flag = false;
    if (flag)
      prime.PB(i);
  }
  while (cin >> m >> a >> b && m) {
    int resp = 0, resq = 0;
    long maxi = 0;
    for (int i = 0; prime[i] * prime[i] <= m; i++) {
      int pos = upper_bound(ALL(prime), m / prime[i]) - prime.begin();
      pos--;
      while (pos >= i) {
        long p = prime[i], q = prime[pos];
        // cout<<p<<" "<<q<<endl;
        if ((long double)p / q >= (long double)a / b) {
          if (p * q > maxi) {
            maxi = p * q;
            resp = p;
            resq = q;
          }
          break;
        }
        pos--;
      }
    }
    cout << resp << " " << resq << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline long toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
inline long toLong(string s) {
  long v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (long i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

const long double EPS = 1e-20;
const long double INF = 1e18;

int main() {
  int m, a, b;
  vector<int> prime;
  for (int i = 2; i <= 100009; i++) {
    bool flag = true;
    for (int j = 2; j * j <= i; j++)
      if (i % j == 0)
        flag = false;
    if (flag)
      prime.PB(i);
  }
  while (cin >> m >> a >> b && m) {
    int resp, resq;
    long maxi = 0;
    for (int i = 0; prime[i] < m; i++) {
      for (long j = i; prime[i] * prime[j] <= m; j++) {
        int p = prime[i], q = prime[j];
        if ((long double)p / q >= (long double)a / b && p * q > maxi) {
          maxi = p * q;
          resp = p;
          resq = q;
        }
      }
    }
    cout << resp << " " << resq << endl;
  }
  return 0;
}