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

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define rep(i, N) for (int i = 0; i < (N); i++)
#define rep2(i, j, N, M) rep(i, N) rep(j, M)
#define rep3(i, j, k, N, M, L) rep(i, N) rep(j, M) rep(k, L)
#define REP(i, init, N) for (int i = (init); i < (N); i++)

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

vector<bool> Prime;
vector<int> PrimeStk;
void initPrime(LL num) {
  Prime = vector<bool>(num + 1, true);
  Prime[1] = Prime[0] = false;
  LL sq_num = sqrt((double)num);
  for (LL i = 2; i <= sq_num; i++) {
    if (Prime[i]) {
      for (LL j = i + i; j <= num; j += i) {
        Prime[j] = false;
      }
    }
  }
}
void initPrimeStk(long begin) {
  PrimeStk.clear();
  for (long i = begin; i < (int)Prime.size(); i++) {
    if (Prime[i]) {
      PrimeStk.push_back(i);
    }
  }
}

int M, A, B;
double AB;

PII solve() {
  PII ans;
  int ans_pq = 0;

  VI ps(ALL(PrimeStk));
  int n = ps.size();

  rep(i, n) {
    int p = ps[i];

    if (p > M)
      break;

    REP(j, i, n) {
      int q = ps[j];

      int m = p * q;
      if (m < 0 || m > M)
        break;

      double pq = 1. * p / q;
      if (!(AB < pq + 1e-6))
        break;

      if (ans_pq < m) {
        ans_pq = m;
        ans = MP(p, q);
      }
    }
  }

  return ans;
}

int main(void) {
  initPrime(100000);
  initPrimeStk(0);

  while (cin >> M >> A >> B, A) {
    AB = 1. * A / B;
    PII ans = solve();
    cout << ans.fst << " " << ans.snd << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define rep(i, N) for (int i = 0; i < (N); i++)
#define rep2(i, j, N, M) rep(i, N) rep(j, M)
#define rep3(i, j, k, N, M, L) rep(i, N) rep(j, M) rep(k, L)
#define REP(i, init, N) for (int i = (init); i < (N); i++)

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

vector<bool> Prime;
vector<int> PrimeStk;
void initPrime(LL num) {
  Prime = vector<bool>(num + 1, true);
  Prime[1] = Prime[0] = false;
  LL sq_num = sqrt((double)num);
  for (LL i = 2; i <= sq_num; i++) {
    if (Prime[i]) {
      for (LL j = i + i; j <= num; j += i) {
        Prime[j] = false;
      }
    }
  }
}
void initPrimeStk(long begin) {
  PrimeStk.clear();
  for (long i = begin; i < (int)Prime.size(); i++) {
    if (Prime[i]) {
      PrimeStk.push_back(i);
    }
  }
}

int M, A, B;
double AB;

PII solve() {
  PII ans;
  int ans_pq = 0;

  VI ps(ALL(PrimeStk));
  int n = ps.size();

  rep(i, n) {
    int p = ps[i];
    if (p > M)
      break;
    REP(j, i, n) {
      int q = ps[j];
      double pq = 1. * p / q;
      if (!(AB < pq + 1e-6))
        break;

      int m = p * q;
      if (m > 0 && m <= M) {
        if (ans_pq < m) {
          ans_pq = m;
          ans = MP(p, q);
        }
      }
    }
  }

  return ans;
}

int main(void) {
  initPrime(100000);
  initPrimeStk(0);

  while (cin >> M >> A >> B, A) {
    AB = 1. * A / B;
    PII ans = solve();
    cout << ans.fst << " " << ans.snd << endl;
  }

  return 0;
}