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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define chmin(a, b) ((a) = min((a), (b)))
#define chmax(a, b) ((a) = max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
const double eps = 1e-10;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

const int MAX_N = 10000010;

bool isPrime[MAX_N];
void eratosthenes(int n) {
  for (int i = 0; i <= n; i++)
    isPrime[i] = true;
  isPrime[0] = false, isPrime[1] = false;

  for (int i = 2; i * i <= n; i++) {
    if (isPrime[i]) {
      for (int j = 2; i * j <= n; j++) {
        isPrime[i * j] = false;
      }
    }
  }
}

int main() {
  eratosthenes(10000000);

  while (1) {
    int m, n;
    cin >> m >> n;
    if (m == 0)
      return 0;

    int cnt = 0;
    for (int i = m;; i++) {
      int num = i;

      bool canCount = true;
      if (!isPrime[num]) {
        for (int j = 2; j * j <= num; j++) {
          if (num % j == 0) {
            if (m <= num / j) {
              canCount = false;
            }

            break;
          }
        }
      }

      if (cnt < n && canCount) {
        cnt++;
      } else if (cnt == n && canCount) {
        cout << num << endl;
        break;
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define chmin(a, b) ((a) = min((a), (b)))
#define chmax(a, b) ((a) = max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
const double eps = 1e-10;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

int main() {
  while (1) {
    int m, n;
    cin >> m >> n;
    if (m == 0)
      return 0;

    int cnt = 0;
    for (int i = m;; i++) {
      int num = i;

      bool canCount = true;
      for (int j = 2; j * j <= num; j++) {
        if (num % j == 0) {
          if (m <= num / j) {
            canCount = false;
          }

          break;
        }
      }

      if (cnt < n && canCount) {
        cnt++;
      } else if (cnt == n && canCount) {
        cout << num << endl;
        break;
      }
    }
  }
}