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

#define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i >= (b); i--)
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define all(a) a.begin(), a.end()
#define len(x) ((int)(x).size())
#define tmax(a, b, c) max((a), max((b), (c)))
#define tmin(a, b, c) min((a), min((b), (c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef map<int, int> Mii;
typedef vector<int> Vi;
typedef vector<vector<int>> VVi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;
typedef pair<ll, ll> Pll;

int p[300001];

int main() {
  set<int> msPrime;
  rep(i, 2, 300001) {
    int t = i % 7;
    bool f = true;
    if (t == 1 || t == 6) {
      if (p[i] == 0)
        msPrime.insert(i);
      int j = i + i;
      while (j < 300001) {
        p[j]++;
        j += i;
      }
    }
  }

  while (1) {
    int x;
    cin >> x;
    if (x == 1)
      break;
    cout << x << ":";
    for (auto a : msPrime)
      if (x % a == 0)
        cout << " " << a;
    cout << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i >= (b); i--)
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define all(a) a.begin(), a.end()
#define len(x) ((int)(x).size())
#define tmax(a, b, c) max((a), max((b), (c)))
#define tmin(a, b, c) min((a), min((b), (c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef map<int, int> Mii;
typedef vector<int> Vi;
typedef vector<vector<int>> VVi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;
typedef pair<ll, ll> Pll;

int main() {
  set<int> msPrime;
  rep(i, 2, 550) {
    int t = i % 7;
    bool f = true;
    if (t == 1 || t == 6) {
      for (auto a : msPrime) {
        if (i % a == 0) {
          // printf("%d is not msPrime by %d\\n",i,a);
          f = false;
          break;
        }
      }
      if (!f)
        continue;
      msPrime.insert(i);
    }
  }
  rep(i, 550, 300001) {
    int t = i % 7;
    bool f = true;
    if (t == 1 || t == 6) {
      for (auto a : msPrime) {
        if (a > 500)
          break;
        if (i % a == 0) {
          f = false;
          break;
        }
      }
      if (!f)
        continue;
      msPrime.insert(i);
    }
  }

  while (1) {
    int x;
    cin >> x;
    cout << x << ":";
    for (auto a : msPrime)
      if (x % a == 0)
        cout << " " << a;
    cout << endl;
  }

  return 0;
}