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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

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

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

bool isprime(int n) { //?´???°????????°true
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}

vector<ll> OutDivisor(ll n) {
  vector<ll> ret;
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      ret.pb(i);
      if (i != sqrt(n))
        ret.pb(n / i);
    }
  }
  return ret;
}

vector<int> solve(int n) {
  vector<int> ans;
  vector<ll> v = OutDivisor(n);
  rep(i, 0, v.size()) {
    if (v[i] % 7 != 1 && v[i] % 7 != 6)
      continue;
    bool ok = true;
    vector<ll> t = OutDivisor(v[i]);
    rep(j, 0, t.size()) {
      if (t[j] == 1 || t[j] == v[i])
        continue;
      if (t[j] % 7 == 1 || t[j] % 7 == 6)
        ok = false;
    }
    if (ok && v[i] != 1)
      ans.pb(v[i]);
  }
  sort(ans.begin(), ans.end());
  return ans;
}

signed main() {
  int n;
  while (cin >> n, n != 1) {
    vector<int> ans = solve(n);
    printf("%lld:", n);
    rep(i, 0, ans.size()) { printf(" %lld", ans[i]); }
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

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

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int d[] = {1, 6};
signed main() {
  int n;
  while (cin >> n, n) {
    int k = 0;
    int t = 0;
    bool ng[300010] = {};
    vector<int> ans;
    while (t <= n) {
      rep(i, 0, 2) {
        t = k * 7 + d[i];
        if (t == 1)
          continue;
        if (n % t == 0 && !ng[t]) {
          ans.pb(t);
          int j = 1;
          while (t * j <= 300000) {
            ng[t * j] = true;
            j++;
          }
        }
      }
      k++;
    }
    printf("%lld:", n);
    rep(i, 0, ans.size()) { printf(" %lld", ans[i]); }
    cout << endl;
  }
  return 0;
}