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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int i = 0; i < (n); i++)                                              \\
      cerr << v[i] << ' ';                                                     \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int a = 0; a < i; a++) {                                              \\
      for (int b = 0; b < j; b++)                                              \\
        cerr << v[a][b] << ' ';                                                \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

#define MMAX 7368795

int main() {
  int m, n;
  while (cin >> m >> n, m | n) {
    vector<int> v;
    int memo[MMAX] = {};
    bool flag = false;
    int i, Mcount = 0;
    for (i = m; i < MMAX; i++) {
      if (memo[i] == 0) {
        if (Mcount == n)
          flag = true;

        for (int j = i; j < MMAX; j += i) {
          memo[j] = 1;
        }
        Mcount++;
      }
      if (flag)
        break;
    }

    cout << i << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int i = 0; i < (n); i++)                                              \\
      cerr << v[i] << ' ';                                                     \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int a = 0; a < i; a++) {                                              \\
      for (int b = 0; b < j; b++)                                              \\
        cerr << v[a][b] << ' ';                                                \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

#define MMAX 8000000
int memo[MMAX];

int main() {
  int m, n;
  while (cin >> m >> n, m | n) {
    REP(a, MMAX) memo[a] = 0;
    bool flag = false;
    int i, Mcount = 0;
    for (i = m; i < MMAX; i++) {
      if (memo[i] == 0) {
        if (Mcount == n)
          flag = true;

        for (int j = i; j < MMAX; j += i) {
          memo[j] = 1;
        }
        Mcount++;
      }
      if (flag)
        break;
    }

    cout << i << endl;
  }
  return 0;
}