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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int m, a, b, tp[50000];
vi p;

int getPrime(int sp) {
  int k[3] = {100, 10, 1};
  int w = 0;
  rep(i, 3) {
    for (int j = w; j < (int)p.size(); j += k[i]) {
      //      printf("%d\\n", j);
      if (sp < p[j])
        break;
      if (p[j] * sp <= m)
        w = j;
      else
        break;
    }
  }
  return p[w];
}

int main() {
  tp[0] = tp[1] = 1;
  REP(i, 2, 50000) {
    if (tp[i])
      continue;
    for (int j = i + i; j < 50000; j += i)
      tp[j] = 1;
  }

  rep(i, 50000) {
    if (tp[i])
      continue;
    p.PB(i);
  }

  //  printf("%d\\n", (int)p.size());

  while (scanf("%d%d%d", &m, &a, &b) && m) {
    int resq = 0, resp = 0;
    for (int i = 0; i < p.size() && p[i] * 2 <= m; i++) {
      //    for(int i = getPrime(sqrt(m)); i * 2 <= m; i++){
      //      if(tp[i]) continue;
      int tq = p[i];
      int tp = getPrime(tq);
      //      printf("%d %d\\n", tq, tp);
      if ((double)a / (double)b > (double)tp / (double)tq)
        continue;
      if (resq * resp > tp * tq)
        continue;
      resq = tq;
      resp = tp;
    }

    printf("%d %d\\n", resp, resq);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int m, a, b, tp[50000];
// vi p;

int getPrime(int sp) {
  for (int i = sp; i >= 0; i--) {
    if (tp[i])
      continue;
    if (m < sp * i)
      continue;
    return i;
  }
  return 2;
}

int main() {
  tp[0] = tp[1] = 1;
  REP(i, 2, 50000) {
    if (tp[i])
      continue;
    for (int j = i + i; j < 50000; j += i)
      tp[j] = 1;
  }

  /*  rep(i, 50000){
    if(tp[i]) continue;
    p.PB(i);
    }*/

  while (scanf("%d%d%d", &m, &a, &b) && m) {
    int resq = 0, resp = 0;
    //    for(int i = 0; i < p.size() && p[i] * 2 <= m; i++){
    for (int i = getPrime(sqrt(m)); i * 2 <= m; i++) {
      //      printf("%d\\n", i);
      if (tp[i])
        continue;
      int tq = i;
      int tp = getPrime(tq);
      if ((double)a / (double)b > (double)tp / (double)tq)
        continue;
      if (resq * resp > tp * tq)
        continue;
      resq = tq;
      resp = tp;
    }

    printf("%d %d\\n", resp, resq);
  }
  return 0;
}