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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define SORT(x) sort((x).begin(), (x).end())
#define REP(i, x, y) for (int i = (x); i < (y); ++i)
#define MP(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Sieve(bool is_prime[], int n) {
  for (int i = 0; i < n; ++i)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i * i <= n; ++i) {
    if (is_prime[i]) {
      for (int j = 2 * i; j < n; j += i)
        is_prime[j] = false;
    }
  }
}

void Solve() {
  bool is_prime[200020];
  Sieve(is_prime, 200000);
  vector<int> prime;
  REP(i, 0, 200000) if (is_prime[i]) prime.push_back(i);

  while (true) {
    int m, a, b;
    cin >> m >> a >> b;
    if (m == 0 && a == 0 && b == 0)
      break;

    int ansp = 0, ansq;
    for (int i = m; i >= 4; --i) {
      if (ansp != 0)
        break;
      REP(j, 0, prime.size()) {
        if (i % prime[j] != 0)
          continue;
        int p = prime[j], q = i / prime[j];
        if (p * p > m)
          break;
        if (!is_prime[q])
          continue;
        if ((double)a / b <= (double)p / q && (double)p / q <= 1) {
          ansp = p;
          ansq = q;
          break;
        }
      }
    }

    cout << ansp << ' ' << ansq << endl;
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define SORT(x) sort((x).begin(), (x).end())
#define REP(i, x, y) for (int i = (x); i < (y); ++i)
#define MP(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Sieve(bool is_prime[], int n) {
  for (int i = 0; i < n; ++i)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i * i <= n; ++i) {
    if (is_prime[i]) {
      for (int j = 2 * i; j < n; j += i)
        is_prime[j] = false;
    }
  }
}

bool IsPrime(int x) {
  int cnt = 0;
  for (int i = 1; i <= x; ++i) {
    if (x % i == 0)
      ++cnt;
    if (cnt > 2)
      return false;
  }

  return true;
}

void Solve() {
  bool is_prime[200020];
  Sieve(is_prime, 200000);
  vector<int> prime;
  REP(i, 0, 200000) if (is_prime[i]) prime.push_back(i);

  while (true) {
    int m, a, b;
    cin >> m >> a >> b;
    if (m == 0 && a == 0 && b == 0)
      break;

    int ansp = 0, ansq;
    for (int i = m; i >= 4; --i) {
      if (ansp != 0)
        break;
      REP(j, 0, prime.size()) {
        if (i % prime[j] != 0)
          continue;
        int p = prime[j], q = i / prime[j];
        if (p > m)
          break;
        if (!is_prime[q])
          continue;
        if ((double)a / b <= (double)p / q && (double)p / q <= 1) {
          ansp = p;
          ansq = q;
          break;
        }
      }
    }

    cout << ansp << ' ' << ansq << endl;
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}