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

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define LFOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define LRFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {
  vector<bool> prime(MAXI + 1, true);
  prime[0] = false;
  prime[1] = false;
  FOR(i, 2, MAXI) {
    if (prime[i] == true) {
      for (int j = i + i; j <= MAXI; j += i) {
        prime[j] = false;
      }
    }
  }

  while (1) {
    int m, n;
    vector<bool> num(MAXI + 1, true);
    int now;

    cin >> m >> n;
    if (m == 0) {
      break;
    }

    if (m > n) {
      cout << m + n << endl;
    } else {
      now = 1;
      if (now <= n) {
        FOR(i, m, MAXI) {
          if (num[i] == true) {
            if (now - 1 == n) {
              cout << i << endl;
              break;
            }
            now++;
            for (int j = i + i; j <= MAXI; j += i) {
              num[j] = false;
            }
          }
        }
      }
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define LFOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define LRFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {
  vector<bool> prime(MAXI + 1, true);
  prime[0] = false;
  prime[1] = false;
  FOR(i, 2, MAXI) {
    if (prime[i] == true) {
      for (int j = i + i; j <= MAXI; j += i) {
        prime[j] = false;
      }
    }
  }
  FOR(i, 2, MAXI) {
    if (prime[i]) {
      // cout << i << endl;
    }
  }
  cout << "a" << endl;
  while (1) {
    int m, n;
    vector<int> num(5000002, 0);
    int now;

    cin >> m >> n;
    if (m == 0) {
      break;
    }

    if (m > n) {
      cout << m + n << endl;
    } else {
      now = 1;
      FOR(i, 1, m) {
        num[now] = m + i - 1;
        now++;
      }
      if (now <= n) {
        FOR(i, 2 * m, 7500000) {
          FOR(j, 1, now - 1) {
            if (i % num[j] == 0) {
              break;
            }
            if (j == now - 1) {
              num[now] = i;
              now++;
            }
          }
          if (now > n) {
            break;
          }
        }
      }
      FOR(i, num[now - 1] + 1, INF) {
        if (prime[i] == true) {
          cout << i << endl;
          break;
        }
      }
    }
  }

  return 0;
}