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


#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using pci = pair<char, int>;
using vpci = vector<pci>;
#define out(S) cout << (S) << endl;
#define REP(i, b) for (size_t i = 0; i < (b); i++)
#define rREP(i, b) for (int i = (b)-1; i >= 0; i--)
#define FOR(i, a, b) for (size_t i = (a); i < (b); i++)
#define rFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define Foreach(item, collection) for (auto item : collection)
#define mod(i) ((i) % (ll)(1e9 + 7))
ll modpow(ll i, ll j) {
  ll tmp = 1;
  while (j) {
    if (j % 2)
      tmp = mod(tmp * i);
    i = mod(i * i);
    j /= 2;
  }
  return tmp;
}
#define divmod(a, b) (mod(a * modpow((ll)b, (ll)(1e9 + 5))))
#define Yes out("Yes")
#define No out("No")
#define NO out("NO")
#define YES out("YES")
#define INF 1e10;
#define ShowAll(collection)                                                    \\
  for (auto i : collection) {                                                  \\
    out(i);                                                                    \\
  }
#define IfOut(condition, text)                                                 \\
  if ((condition)) {                                                           \\
    out(text);                                                                 \\
    return 0;                                                                  \\
  }
#define IfeOut(condition, itext, etext)                                        \\
  if (condition) {                                                             \\
    out(itext);                                                                \\
    return 0;                                                                  \\
  } else {                                                                     \\
    out(etext);                                                                \\
    return 0;                                                                  \\
  }
#define beginend(vec) (vec).begin(), (vec).end()
#define pb(item) push_back(item)
#define removeAll(vec, deleg)                                                  \\
  auto itr = remove_if(beginend(vec), deleg);                                  \\
  vec.erase(itr, vec.end());
void removeAt(string &s, int index) { s.erase(index, 1); }
template <typename T> void removeAt(vector<T> &v, int index) {
  v.erase(v.begin() + index);
}
ll manhattanDistance(ll x1, ll y1, ll x2, ll y2) {
  return (abs(x2 - x1) + abs(y2 - y1));
}
vector<ll> sieveOfEratosthenes(int max) {
  ll prime;
  double sqrtmax = sqrt(max);
  vector<ll> primeVec, searchVec;
  FOR(i, 2, max + 1) searchVec.push_back(i);
  do {
    prime = searchVec[0];
    primeVec.push_back(prime);
    auto itr = remove_if(beginend(searchVec),
                         [=](ll x) -> bool { return x % prime == 0; });
    searchVec.erase(itr, searchVec.end());
  } while (prime < sqrtmax);
  primeVec.reserve(primeVec.size() + searchVec.size());
  primeVec.insert(primeVec.end(), beginend(searchVec));
  return primeVec;
}
bool isPrime(ll x) {
  if (x == 2)
    return true;
  if (x < 2 || x % 2 == 0)
    return false;
  ll i = 3;
  while (i <= sqrt(x)) {
    if (x % i == 0)
      return false;
    i += 2;
  }
  return true;
}

vector<ll> getudoPrimes(ll max) {
  ll prime;
  double sqmax = sqrt(max);
  vector<ll> prvec, svec;
  svec.pb(6);
  for (ll i = 7; i < max + 1; i += 7) {
    svec.pb(i + 1);
    svec.pb(i + 6);
  }
  do {
    prime = svec[0];
    prvec.pb(prime);
    removeAll(svec, [=](ll x) -> bool { return x % prime == 0; });

  } while (prime < sqmax);
  prvec.reserve(prvec.size() + svec.size());
  prvec.insert(prvec.end(), beginend(svec));
  return prvec;
}

int main() {
  vl query;
  int n;
  while (cin >> n, n != 1) {
    query.pb(n);
  }
  auto mm = max_element(beginend(query));
  auto prims = getudoPrimes(*mm);
  Foreach(i, query) {
    auto pri = prims;
    removeAll(pri, [=](ll x) { return i % x != 0; });
    cout << i << ":";
    Foreach(x, pri) cout << " " << x;
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using pci = pair<char, int>;
using vpci = vector<pci>;
#define out(S) cout << (S) << endl;
#define REP(i, b) for (size_t i = 0; i < (b); i++)
#define rREP(i, b) for (int i = (b)-1; i >= 0; i--)
#define FOR(i, a, b) for (size_t i = (a); i < (b); i++)
#define rFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define Foreach(item, collection) for (auto item : collection)
#define mod(i) ((i) % (ll)(1e9 + 7))
ll modpow(ll i, ll j) {
  ll tmp = 1;
  while (j) {
    if (j % 2)
      tmp = mod(tmp * i);
    i = mod(i * i);
    j /= 2;
  }
  return tmp;
}
#define divmod(a, b) (mod(a * modpow((ll)b, (ll)(1e9 + 5))))
#define Yes out("Yes")
#define No out("No")
#define NO out("NO")
#define YES out("YES")
#define INF 1e10;
#define ShowAll(collection)                                                    \\
  for (auto i : collection) {                                                  \\
    out(i);                                                                    \\
  }
#define IfOut(condition, text)                                                 \\
  if ((condition)) {                                                           \\
    out(text);                                                                 \\
    return 0;                                                                  \\
  }
#define IfeOut(condition, itext, etext)                                        \\
  if (condition) {                                                             \\
    out(itext);                                                                \\
    return 0;                                                                  \\
  } else {                                                                     \\
    out(etext);                                                                \\
    return 0;                                                                  \\
  }
#define beginend(vec) (vec).begin(), (vec).end()
#define pb(item) push_back(item)
#define removeAll(vec, deleg)                                                  \\
  auto itr = remove_if(beginend(vec), deleg);                                  \\
  vec.erase(itr, vec.end());
void removeAt(string &s, int index) { s.erase(index, 1); }
template <typename T> void removeAt(vector<T> &v, int index) {
  v.erase(v.begin() + index);
}
ll manhattanDistance(ll x1, ll y1, ll x2, ll y2) {
  return (abs(x2 - x1) + abs(y2 - y1));
}
vector<ll> sieveOfEratosthenes(int max) {
  ll prime;
  double sqrtmax = sqrt(max);
  vector<ll> primeVec, searchVec;
  FOR(i, 2, max + 1) searchVec.push_back(i);
  do {
    prime = searchVec[0];
    primeVec.push_back(prime);
    auto itr = remove_if(beginend(searchVec),
                         [=](ll x) -> bool { return x % prime == 0; });
    searchVec.erase(itr, searchVec.end());
  } while (prime < sqrtmax);
  primeVec.reserve(primeVec.size() + searchVec.size());
  primeVec.insert(primeVec.end(), beginend(searchVec));
  return primeVec;
}
bool isPrime(ll x) {
  if (x == 2)
    return true;
  if (x < 2 || x % 2 == 0)
    return false;
  ll i = 3;
  while (i <= sqrt(x)) {
    if (x % i == 0)
      return false;
    i += 2;
  }
  return true;
}

vector<ll> getudoPrimes(ll max) {
  ll prime;
  double sqmax = sqrt(max);
  vector<ll> prvec, svec;
  svec.pb(6);
  for (ll i = 7; i < max + 1; i += 7) {
    svec.pb(i + 1);
    svec.pb(i + 6);
  }
  do {
    prime = svec[0];
    prvec.pb(prime);
    removeAll(svec, [=](ll x) -> bool { return x % prime == 0; });

  } while (prime < sqmax);
  prvec.reserve(prvec.size() + svec.size());
  prvec.insert(prvec.end(), beginend(svec));
  removeAll(prvec, [=](ll x) { return max % x != 0; });
  return prvec;
}
int n;
void solve() {
  auto pr = getudoPrimes(n / 2);
  cout << n << ": ";
  Foreach(i, pr) cout << i << " ";
  cout << endl;
  return;
}

int main() {
  while (cin >> n, n != 1) {
    solve();
  }
  return 0;
}