Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define BEGIN(x) x.begin()
#define END(x) x.end()
#define ALL(x) BEGIN(x), END(x)
#define PAIR make_pair
#define VEC(type) vector<type>
#define endl '\n'
#define FIR first
#define SEC second
#define PB push_back
#define SZ(x) ((int)x.size())

#define FOR(i, A, B) for (auto i = (A); i != (B); i++)
#define FORD(i, A, B) for (auto i = (A); i != (B); i--)

#define READRANGE(begin, end) FOR(it, begin, end) cin >> *it
#define READVEC(V) READRANGE(BEGIN(V), END(V))

using namespace std;

typedef long long lint;
typedef pair<int, int> Pii;
typedef pair<int, lint> Pil;
typedef pair<lint, lint> Pll;
typedef pair<lint, int> Pli;

template <class U, class V> istream &operator>>(istream &in, pair<U, V> &pr) {
  return in >> pr.first >> pr.second;
}

template <class U, class V> ostream &operator<<(ostream &out, pair<U, V> &p) {
  return out << p.first << " " << p.second;
}

template <class T> ostream &operator<<(ostream &out, vector<T> vec) {
  for (T &e : vec)
    out << e << " ";
  return out << endl;
}

// ---- BEGIN LIBRARY CODE ----
// ---- END LIBRARY CODE ----
void io_init() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
};

void solveSingleCase(int caseNo) {
  int N;
  cin >> N;
  VEC(int) A(N);
  READVEC(A);

  const int MAXN = 1000000;
  VEC(bool) bad(MAXN + 1, false);
  map<int, int> counter;

  for (int a : A) {
    FOR(m, 2, MAXN / a + 1)
    bad[a * m] = true;
    counter[a]++;
    if (counter[a] >= 2)
      bad[a] = true;
  }

  int ret = 0;
  for (int a : A)
    if (!bad[a])
      ret++;

  cout << ret << endl;
};

int main(int argc, char **argv) {
  io_init();

  // Actual program code starts here.
  int T = 1;
  // cin >> T;
  FOR(tcase, 1, T + 1) { solveSingleCase(tcase); }
  return 0;
};


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define BEGIN(x) x.begin()
#define END(x) x.end()
#define ALL(x) BEGIN(x), END(x)
#define PAIR make_pair
#define VEC(type) vector<type>
#define endl '\n'
#define FIR first
#define SEC second
#define PB push_back
#define SZ(x) ((int)x.size())

#define FOR(i, A, B) for (auto i = (A); i != (B); i++)
#define FORD(i, A, B) for (auto i = (A); i != (B); i--)

#define READRANGE(begin, end) FOR(it, begin, end) cin >> *it
#define READVEC(V) READRANGE(BEGIN(V), END(V))

using namespace std;

typedef long long lint;
typedef pair<int, int> Pii;
typedef pair<int, lint> Pil;
typedef pair<lint, lint> Pll;
typedef pair<lint, int> Pli;

template <class U, class V> istream &operator>>(istream &in, pair<U, V> &pr) {
  return in >> pr.first >> pr.second;
}

template <class U, class V> ostream &operator<<(ostream &out, pair<U, V> &p) {
  return out << p.first << " " << p.second;
}

template <class T> ostream &operator<<(ostream &out, vector<T> vec) {
  for (T &e : vec)
    out << e << " ";
  return out << endl;
}

// ---- BEGIN LIBRARY CODE ----
// ---- END LIBRARY CODE ----
void io_init() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
};

void solveSingleCase(int caseNo) {
  int N;
  cin >> N;
  VEC(int) A(N);
  READVEC(A);

  const int MAXN = 1000000;
  VEC(bool) bad(MAXN + 1, false);
  map<int, int> counter;

  for (int a : A)
    if (!bad[a]) {
      FOR(m, 2, MAXN / a + 1)
      bad[a * m] = true;
      counter[a]++;
      if (counter[a] >= 2)
        bad[a] = true;
    }

  int ret = 0;
  for (int a : A)
    if (!bad[a])
      ret++;

  cout << ret << endl;
};

int main(int argc, char **argv) {
  io_init();

  // Actual program code starts here.
  int T = 1;
  // cin >> T;
  FOR(tcase, 1, T + 1) { solveSingleCase(tcase); }
  return 0;
};