Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

std::vector<ll> divisors(ll x) {
  std::vector<ll> ret;

  for (ll i = 1; i * i <= x; ++i)
    if (x % i == 0) {
      ret.emplace_back(i);

      if (i != x / i)
        ret.emplace_back(x / i);
    }

  return ret;
}

std::map<ll, ll> prime_factor(ll x) {
  std::map<ll, ll> ret;

  for (ll i = 2; i * i <= x; ++i) {
    while (x % i == 0) {
      ++ret[i];
      x /= i;
    }
  }

  if (x != 1)
    ++ret[x];

  return ret;
}

ll N;
ll A[200010];
std::map<ll, ll> mp;
ll ans;

int main() {
  std::cin >> N;

  rep(i, N) {
    std::cin >> A[i];

    ++mp[A[i]];
  }

  rep(i, N) {
    auto ds = divisors(A[i]);

    bool fl = true;

    for (auto d : ds) {
      fl &= d == A[i] ? mp[d] == 1 : !mp[d];

      if (!fl)
        break;
    }

    if (fl)
      ++ans;
  }

  std::cout << ans << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

std::vector<ll> divisors(ll x) {
  std::vector<ll> ret;

  for (ll i = 1; i * i <= x; ++i)
    if (x % i == 0) {
      ret.emplace_back(i);

      if (i != x / i)
        ret.emplace_back(x / i);
    }

  return ret;
}

std::map<ll, ll> prime_factor(ll x) {
  std::map<ll, ll> ret;

  for (ll i = 2; i * i <= x; ++i) {
    while (x % i == 0) {
      ++ret[i];
      x /= i;
    }
  }

  if (x != 1)
    ++ret[x];

  return ret;
}

ll N;
ll A[200010];
ll mp[2000010];
ll ans;

int main() {
  std::cin >> N;

  rep(i, N) {
    std::cin >> A[i];

    ++mp[A[i]];
  }

  rep(i, N) {
    auto ds = divisors(A[i]);

    bool fl = true;

    for (auto d : ds) {
      fl &= d == A[i] ? mp[d] == 1 : !mp[d];

      if (!fl)
        break;
    }

    if (fl)
      ++ans;
  }

  std::cout << ans << std::endl;

  return 0;
}