Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define sync                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define repp(i, a, n) for (int i = a; i < int(n); ++i)
#define sz(x) (x).size()
#define pq priority_queue
#define pb push_back
#define mp make_pair

#define dbg(x) cerr << #x " = " << x << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &input) {
  for (auto const &i : input) {
    os << i << " ";
  }
  return os;
}
template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &input) {
  os << "(" << input.first << ", " << input.second << ')';
  return os;
}

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<pi> vp;
typedef vector<ll> vl;
typedef vector<vi> vvi;

const int maxn = 1e6;

void pre() {}

int vis[maxn + 1];
int cnt[maxn + 1];

void solve() {
  int n;
  cin >> n;

  vp a(0);
  rep(i, n) {
    int x;
    cin >> x;
    a.pb(mp(x, i));
    cnt[x]++;
  }

  sort(a.begin(), a.end());

  int ans = 0;

  for (auto p : a) {
    int val = p.first;
    int i = p.second;
    if (cnt[val] == 1 && vis[val] == 0) {
      ans++;
    }
    for (int j = val; j <= maxn; j += val)
      vis[j] = 1;
  }

  cout << ans;
}

int main() {
  sync;
  int cas = 1;
  // cin >> cas;
  pre();
  rep(ca, cas) solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define sync                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define repp(i, a, n) for (int i = a; i < int(n); ++i)
#define sz(x) (x).size()
#define pq priority_queue
#define pb push_back
#define mp make_pair

#define dbg(x) cerr << #x " = " << x << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &input) {
  for (auto const &i : input) {
    os << i << " ";
  }
  return os;
}
template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &input) {
  os << "(" << input.first << ", " << input.second << ')';
  return os;
}

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<pi> vp;
typedef vector<ll> vl;
typedef vector<vi> vvi;

const int maxn = 1e6;

void pre() {}

int vis[maxn + 1];
int cnt[maxn + 1];

void solve() {
  int n;
  cin >> n;

  vp a(0);
  rep(i, n) {
    int x;
    cin >> x;
    a.pb(mp(x, i));
    cnt[x]++;
  }

  sort(a.begin(), a.end());

  int ans = 0;

  for (auto p : a) {
    int val = p.first;
    int i = p.second;
    if (cnt[val] == 1 && vis[val] == 0) {
      ans++;
    }
    if (vis[val] == 0) {
      for (int j = val; j <= maxn; j += val)
        vis[j] = 1;
    }
  }

  cout << ans;
}

int main() {
  sync;
  int cas = 1;
  // cin >> cas;
  pre();
  rep(ca, cas) solve();
  return 0;
}