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


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1232

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

#define PRIME_MAX 100000

vector<int> init_prime() {
  bool is_prime[PRIME_MAX];
  is_prime[0] = false;
  is_prime[1] = false;
  REP(i, 2, PRIME_MAX) is_prime[i] = true;
  REP(i, 2, PRIME_MAX) {
    if (is_prime[i]) {
      for (int j = i + i; j < PRIME_MAX; j += i)
        is_prime[j] = false;
    }
  }

  vector<int> prime;
  rep(i, PRIME_MAX) {
    if (is_prime[i]) {
      prime.push_back(i);
    }
  }

  return prime;
}

int main() {

  vector<int> prime = init_prime();

  int M;
  double A, B;
  while (cin >> M >> A >> B && M) {
    int p, q;
    int max_pq = 0;
    double a_b = A / B;

    rep(i, prime.size()) {

      if (prime[i] > M) {
        break;
      }

      rep(j, prime.size()) {

        if (prime[i] * prime[j] > M) {
          break;
        }

        int pq = prime[i] * prime[j];
        double p_q = (double)prime[i] / prime[j];
        if (pq > max_pq && a_b <= p_q && p_q <= 1.0) {
          max_pq = pq;
          p = prime[i];
          q = prime[j];
        }
      }
    }
    cout << p << " " << q << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1232

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

#define PRIME_MAX 100000

int main() {
  bool is_prime[PRIME_MAX];
  is_prime[0] = false;
  is_prime[1] = false;
  REP(i, 2, PRIME_MAX) is_prime[i] = true;
  REP(i, 2, PRIME_MAX) {
    if (is_prime[i]) {
      for (int j = i + i; j < PRIME_MAX; j += i)
        is_prime[j] = false;
    }
  }

  vector<int> v;
  rep(i, PRIME_MAX) {
    if (is_prime[i]) {
      v.push_back(i);
    }
  }

  int M;
  double A, B;
  while (cin >> M >> A >> B && M) {
    int pq = 0;
    int P = 0;
    double a_b = A / B;
    rep(p, v.size()) {
      REP(q, p, v.size()) {
        if (v[p] * v[q] > M) {
          break;
        }

        if (A * v[q] <= v[p] * B) {
          if (v[p] * v[q] > pq) {
            pq = v[p] * v[q];
            P = v[p];
          }
        }
      }
    }
    cout << P << " " << pq / P << endl;
  }
  return 0;
}