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

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#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 range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

const int mmax = 100010;
bool isprime[mmax + 1];

void init() {
  rep(i, mmax + 1) isprime[i] = true;
  isprime[0] = isprime[1] = false;
  for (int i = 2; i * i <= mmax; ++i) {
    if (isprime[i])
      for (int j = i * i; j <= mmax; j += i)
        isprime[j] = false;
  }
  return;
}

pii solve(int m, int a, int b) {
  // a/b<=p/q aq<=pb
  // p/q<=1 p<=q
  ll cmax = 0;
  pii res;
  for (int p = 2; p * p <= m; ++p) {
    if (!isprime[p])
      continue;
    for (int q = p; q <= m / p; ++q) {
      if (!isprime[q])
        continue;

      if (a * q <= p * b && p * q > cmax) {
        cmax = p * q;
        res = mp(p, q);
      }
    }
  }
  return res;
}

int main(void) {
  init();
  int m, a, b;
  while (cin >> m >> a >> b) {
    if (m == 0)
      break;
    pii ans = solve(m, a, b);
    cout << ans.first << " " << ans.second << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#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 range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

const int mmax = 100010;
bool isprime[mmax + 1];

void init() {
  rep(i, mmax + 1) isprime[i] = true;
  isprime[0] = isprime[1] = false;
  for (int i = 2; i * i <= mmax; ++i) {
    if (isprime[i])
      for (int j = i * i; j <= mmax; j += i)
        isprime[j] = false;
  }
  return;
}

pii solve(int m, int a, int b) {
  // a/b<=p/q aq<=pb
  // p/q<=1 p<=q
  ll cmax = 0;
  pii res;
  for (int mm = 4; mm <= m; ++mm) {
    if (isprime[mm])
      continue;
    for (int p = 2; p * p <= mm; ++p) {
      if (mm % p == 0) {
        int q = mm / p;
        if (isprime[p] && isprime[q]) {
          if (p <= q && a * q <= p * b && mm > cmax) {
            cmax = mm;
            res = mp(p, q);
          }
        }
      }
    }
  }
  return res;
}

int main(void) {
  init();
  int m, a, b;
  while (cin >> m >> a >> b) {
    if (m == 0)
      break;
    pii ans = solve(m, a, b);
    cout << ans.first << " " << ans.second << endl;
  }
  return 0;
}