Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, d, n) for (int i = (d); i < (n); ++i)
#define rrep(i, n) for (int i = (n - 1); i >= 0; --i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define int long long
template <typename T> using vv = vector<vector<T>>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;
using ll = long long;
constexpr ll INF = 1LL << 60;
class Prime {
public:
  int n;
  vector<int> factor, primes;
  // エラストテネスの篩
  Prime(int n = 100000) : n(n), factor(n + 1) {
    factor[0] = factor[1] = -1;
    for (int i = 2; i <= n; ++i) {
      if (factor[i]) {
        continue;
      }
      primes.push_back(i);
      factor[i] = i;
      for (int j = i * i; j <= n; j += i) {
        if (!factor[j]) {
          factor[j] = i;
        }
      }
    }
  }
  // 素数判定
  bool is_prime(int x) { return factor[x] == x; }
  // 約数列挙
  bool factorList(vi &dic, int x) {
    if (dic[x] > 1) {
      return true;
    }
    for (int i = 2; i * i <= x; ++i) {
      if (x % i == 0) {
        if (dic[i] || dic[x / i]) {
          return true;
        }
      }
    }
    return false;
  }
};

signed main() {

  int n;
  cin >> n;
  vector<int> a(n);
  vi dic(1e6 + 1);
  rep(i, n) {
    cin >> a[i];
    dic[a[i]]++;
  }

  sort(all(a));
  int ans = 0;
  vi arr(1e6 + 1);
  rep(i, n) {
    int x = a[i];
    if (dic[x] == 1 && !arr[x]) {
      ans++;
    }
    for (int i = x; i <= 1e6; i += x) {
      arr[i] = 1;
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, d, n) for (int i = (d); i < (n); ++i)
#define rrep(i, n) for (int i = (n - 1); i >= 0; --i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define int long long
template <typename T> using vv = vector<vector<T>>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;
using ll = long long;
constexpr ll INF = 1LL << 60;
class Prime {
public:
  int n;
  vector<int> factor, primes;
  // エラストテネスの篩
  Prime(int n = 100000) : n(n), factor(n + 1) {
    factor[0] = factor[1] = -1;
    for (int i = 2; i <= n; ++i) {
      if (factor[i]) {
        continue;
      }
      primes.push_back(i);
      factor[i] = i;
      for (int j = i * i; j <= n; j += i) {
        if (!factor[j]) {
          factor[j] = i;
        }
      }
    }
  }
  // 素数判定
  bool is_prime(int x) { return factor[x] == x; }
  // 約数列挙
  bool factorList(vi &dic, int x) {
    if (dic[x] > 1) {
      return true;
    }
    for (int i = 2; i * i <= x; ++i) {
      if (x % i == 0) {
        if (dic[i] || dic[x / i]) {
          return true;
        }
      }
    }
    return false;
  }
};

signed main() {

  int n;
  cin >> n;
  vector<int> a(n);
  vi dic(1e6 + 1);
  rep(i, n) {
    cin >> a[i];
    dic[a[i]]++;
  }

  sort(all(a));
  int ans = 0;
  vi arr(1e6 + 1);
  rep(i, n) {
    int x = a[i];
    if (dic[x] == 1 && !arr[x]) {
      ans++;
    }
    if (arr[x]) {
      continue;
    }
    for (int i = x; i <= 1e6; i += x) {
      arr[i] = 1;
    }
  }
  cout << ans << endl;

  return 0;
}