Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
vint primes;
vll div(ll N) {
  vll yakusuu;
  for (ll i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      yakusuu.push_back(i);
      if (i * i != N)
        yakusuu.push_back(N / i);
    }
  }
  return yakusuu;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vint A(N);
  set<int> s;
  map<int, int> cnt;
  for (size_t i = 0; i < N; i++) {
    cin >> A[i];
    s.insert(A[i]);
    cnt[A[i]]++;
  }
  int ans = N;
  for (size_t i = 0; i < A.size(); i++) {
    if (cnt[A[i]] >= 2) {
      ans--;
      continue;
    }
    auto divs = div(A[i]);
    for (auto &&j : divs) {
      if (j != A[i] and s.count(j) == 1) {
        ans--;
        break;
      }
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
vint primes;
vll div(ll N) {
  vll yakusuu;
  for (ll i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      yakusuu.push_back(i);
      if (i * i != N)
        yakusuu.push_back(N / i);
    }
  }
  return yakusuu;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vint A(N);
  set<int> s;
  vint cnt(1e6 + 1);
  for (size_t i = 0; i < N; i++) {
    cin >> A[i];
    s.insert(A[i]);
    cnt[A[i]]++;
  }
  int ans = N;
  for (size_t i = 0; i < A.size(); i++) {
    if (cnt[A[i]] >= 2) {
      ans--;
      continue;
    }
    auto divs = div(A[i]);
    for (auto &&j : divs) {
      if (j != A[i] and s.count(j) == 1) {
        ans--;
        break;
      }
    }
  }
  cout << ans;
  return 0;
}