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


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

using namespace std;
using ll = long long;
using ull = unsigned long long;
#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 = (b)-1; i >= 0; 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 Select(collection, condition, result)                                  \\
  Foreach(i, collection) { result += condition; }
#define GetAuto(_N, vect)                                                      \\
  {                                                                            \\
    cin >> N;                                                                  \\
    vect.resize(N);                                                            \\
    REP(i, _N) { cin >> vect[i]; }                                             \\
  }
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));
}
#define beginend(vec) (vec).begin(), (vec).end()
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;
}

int main() {
  ll nn;
  map<ll, ll> ans;
  map<ll, bool> box;
  vector<ll> input, output;
  while (cin >> nn) {
    input.push_back(nn);
    box[nn] = false;
  }
  box[1] = true;
  ans[1] = 0;
  Foreach(n, input) {

    if (box[n]) {
      output.push_back(ans[n]);
      continue;
    }
    int prime;
    double sqrtmax = sqrt(n);
    vector<ll> primeVec, searchVec;
    FOR(i, 2, n + 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);
    ans[n] = (primeVec.size() + searchVec.size());
    output.push_back(ans[n]);
    box[n] = true;
  }

  ShowAll(output);
  return 0;
}

Error: TLE

Fixed submission:


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

using namespace std;
using ll = long long;
using ull = unsigned long long;
#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 = (b)-1; i >= 0; 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 Select(collection, condition, result)                                  \\
  Foreach(i, collection) { result += condition; }
#define GetAuto(_N, vect)                                                      \\
  {                                                                            \\
    cin >> N;                                                                  \\
    vect.resize(N);                                                            \\
    REP(i, _N) { cin >> vect[i]; }                                             \\
  }
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));
}
#define beginend(vec) (vec).begin(), (vec).end()
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;
}

int main() {
  ll n;
  vector<ll> ans;
  while (cin >> n) {
    if (n == 1) {
      ans.push_back(0);
      continue;
    }
    int prime;
    double sqrtmax = sqrt(n);
    vector<ll> primeVec, searchVec;
    FOR(i, 2, n + 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);
    ans.push_back(primeVec.size() + searchVec.size());
  }
  Foreach(i, ans) out(i);
  return 0;
}