Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;

// 定義
typedef long long ll;
typedef pair<ll, ll> P;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (ll i = 0; i < (ll)n; ++i)
#define REPN(i, m, n) for (ll i = m; i < (ll)n; ++i)
#define REVREP(i, n) for (ll i = n - 1; i >= 0; i--)
#define VL vector<ll>
#define VVL vector<vector<ll>>
#define VVVL vector<vector<vector<ll>>>
#define VC vector<char>
#define VVC vector<vector<char>>
#define INF (ll)2e9
#define INF_LL 1LL << 60
// #define MOD             998244353
#define MOD 1000000007

ll Ceil(ll val, ll div) { return (val + div - 1) / div; }
ll CeilN(ll val, ll div) { return Ceil(val, div) * div; }
ll FloorN(ll x, ll n) { return (x - x % n); }
bool IsOdd(ll x) { return ((x & 1) == 1); }
bool IsEven(ll x) { return ((x & 1) == 0); }
template <class T> bool chmax(T &a, T b) {
  if (a <= b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a >= b) {
    a = b;
    return 1;
  }
  return 0;
}

void Solve() {
  ll N;
  cin >> N;
  VL A(N);
  REP(i, N) cin >> A[i];
  sort(ALL(A));

  VL cnt(1000 * 1000 + 5, 0);
  for (auto one : A) {
    cnt[one]++;
  }
  for (auto one : A) {
    if (cnt[one] == INF)
      continue;
    ll add = one + one;
    while (add < cnt.size()) {
      if (cnt[add]) {
        cnt[add] = INF;
      }
      add += one;
    }
  }

  ll ans = 0;
  for (auto one : cnt) {
    if (one == 1)
      ans++;
  }
  cout << ans << endl;
}

// メイン
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Solve();
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;

// 定義
typedef long long ll;
typedef pair<ll, ll> P;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (ll i = 0; i < (ll)n; ++i)
#define REPN(i, m, n) for (ll i = m; i < (ll)n; ++i)
#define REVREP(i, n) for (ll i = n - 1; i >= 0; i--)
#define VL vector<ll>
#define VVL vector<vector<ll>>
#define VVVL vector<vector<vector<ll>>>
#define VC vector<char>
#define VVC vector<vector<char>>
#define INF (ll)2e9
#define INF_LL 1LL << 60
// #define MOD             998244353
#define MOD 1000000007

ll Ceil(ll val, ll div) { return (val + div - 1) / div; }
ll CeilN(ll val, ll div) { return Ceil(val, div) * div; }
ll FloorN(ll x, ll n) { return (x - x % n); }
bool IsOdd(ll x) { return ((x & 1) == 1); }
bool IsEven(ll x) { return ((x & 1) == 0); }
template <class T> bool chmax(T &a, T b) {
  if (a <= b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a >= b) {
    a = b;
    return 1;
  }
  return 0;
}

void Solve() {
  ll N;
  cin >> N;
  VL A(N);
  REP(i, N) cin >> A[i];
  sort(ALL(A));

  VL cnt(1000 * 1000 + 5, 0);
  for (auto one : A) {
    cnt[one]++;
  }
  for (auto one : A) {
    if (cnt[one] == INF)
      continue;
    ll add = one + one;
    while (add <= A.back()) {
      if (cnt[add]) {
        cnt[add] = INF;
      }
      add += one;
    }
  }

  ll ans = 0;
  for (auto one : cnt) {
    if (one == 1)
      ans++;
  }
  cout << ans << endl;
}

// メイン
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Solve();
  return 0;
}