Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using P = pair<ll, ll>;
template <typename T> using pq = priority_queue<T>;
template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T, typename K> using ump = unordered_map<T, K>;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll e5 = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = ll(n - 1); i >= 0; i--)
#define each(i, mp) for (auto &i : mp)
#define eb emplace_back
#define F first
#define S second
#define all(obj) (obj).begin(), (obj).end()

template <class T> ostream &operator<<(ostream &out, const vector<T> &list) {
  ll n = list.size();
  rep(i, n) out << list[i] << ' ';
  return out;
}

template <class T> istream &operator>>(istream &in, vector<T> &list) {
  ll n = list.size();
  rep(i, n) in >> list[i];
  return in;
}

template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &list) {
  ll n = list.size();
  rep(i, n) out << list[i] << '\n';
  return out;
}

/* ------------- ANSWER ------------- */
/* ---------------------------------- */
// 素因数分解
map<ll, ll> prime_factor(ll n) {
  map<ll, ll> mp;
  for (ll i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      mp[i]++;
      n /= i;
    }
  }
  if (n != 1)
    mp[n]++;
  return mp;
}

// 素数判定
bool is_prime(ll n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return n != 1;
}

// 約数列挙
vector<ll> divisor(ll n) {
  vector<ll> res;
  for (ll i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      res.emplace_back(i);
      if (i != n / i)
        res.emplace_back(n / i);
    }
  }
  return res;
}

void solve() {
  ll n;
  cin >> n;
  vl a(n);
  cin >> a;

  map<ll, ll> mp;
  rep(i, n) { mp[a[i]]++; }
  ll ans = 0;
  rep(i, n) {
    vl div = divisor(a[i]);
    bool plus = true;
    each(e, div) {
      if (e == a[i] && mp[e] > 1) {
        plus = false;
        break;
      } else if (e != a[i] && mp[e] > 0) {
        plus = false;
        break;
      }
    }
    if (plus)
      ans++;
  }
  cout << ans << '\n';
}

int main() {
#ifdef MY_DEBUG
  while (true) {
#endif
    solve();
#ifdef MY_DEBUG
  }
#endif
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using P = pair<ll, ll>;
template <typename T> using pq = priority_queue<T>;
template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T, typename K> using ump = unordered_map<T, K>;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll e5 = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = ll(n - 1); i >= 0; i--)
#define each(i, mp) for (auto &i : mp)
#define eb emplace_back
#define F first
#define S second
#define all(obj) (obj).begin(), (obj).end()

template <class T> ostream &operator<<(ostream &out, const vector<T> &list) {
  ll n = list.size();
  rep(i, n) out << list[i] << ' ';
  return out;
}

template <class T> istream &operator>>(istream &in, vector<T> &list) {
  ll n = list.size();
  rep(i, n) in >> list[i];
  return in;
}

template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &list) {
  ll n = list.size();
  rep(i, n) out << list[i] << '\n';
  return out;
}

/* ------------- ANSWER ------------- */
/* ---------------------------------- */
// 素因数分解
map<ll, ll> prime_factor(ll n) {
  map<ll, ll> mp;
  for (ll i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      mp[i]++;
      n /= i;
    }
  }
  if (n != 1)
    mp[n]++;
  return mp;
}

// 素数判定
bool is_prime(ll n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return n != 1;
}

// 約数列挙
vector<ll> divisor(ll n) {
  vector<ll> res;
  for (ll i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      res.emplace_back(i);
      if (i != n / i)
        res.emplace_back(n / i);
    }
  }
  return res;
}

void solve() {
  ll n;
  cin >> n;
  vl a(n);
  cin >> a;

  unordered_map<ll, ll> mp;
  rep(i, n) { mp[a[i]]++; }
  ll ans = 0;
  rep(i, n) {
    vl div = divisor(a[i]);
    bool plus = true;
    each(e, div) {
      if (e == a[i] && mp[e] > 1) {
        plus = false;
        break;
      } else if (e != a[i] && mp[e] > 0) {
        plus = false;
        break;
      }
    }
    if (plus)
      ans++;
  }
  cout << ans << '\n';
}

int main() {
#ifdef MY_DEBUG
  while (true) {
#endif
    solve();
#ifdef MY_DEBUG
  }
#endif
  return 0;
}