Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long int ll;
typedef long double ld;
const ll INF = (1LL << 62);
const ld pi = acosl((ld)-1);
const ll mod = 1000000007;
// const ll mod = 1234567;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
#define endn "\n"
#define TO_STRING(VariableName) #VariableName

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  rep(i, (int)v.size() - 1) cout << v[i] << " ";
  cout << v[(int)v.size() - 1];
  return out;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const map<T1, T2> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}
template <class T> void debag(const T &obj) { cout << obj << endl; }

bool solve() {
  int n;
  cin >> n;
  ll ma = pow(10, 6) + 10;
  vector<ll> cnt(ma, 0);
  vector<ll> p(n);
  rep(i, n) {
    cin >> p[i];
    cnt[p[i]]++;
  }

  sort(all(p));
  int ans = 0;
  rep(i, n) {
    if (cnt[p[i]] == 1)
      ans++;
    if (cnt[p[i]] == 0)
      continue;
    for (ll j = p[i] + p[i]; j < ma; j += p[i]) {
      cnt[j] = 0;
    }
  }
  cout << ans << endl;
  /*multiset<ll>a;
  rep(i,n){
          int x;a.insert(x);
  }
  int ans=0;
  while(!a.empty()){
          ll p=*a.begin();
          //cout<<1<<endl;
          a.erase(a.begin());
          int is=1;
          while(a.size()!=0 and *a.begin()==p){
                  is=0;
                  //cout<<2<<endl;
                  a.erase(a.begin());
          }
          ans+=is;
          ll x=p+p;
          while(x<pow(10,6)){
                  //cout<<3<<endl;
                  if(a.find(x)!=a.end())a.erase(x);
                  x+=p;
          }
  }
  cout<<ans<<endl;*/
  return false;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(15);
  solve();
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long int ll;
typedef long double ld;
const ll INF = (1LL << 62);
const ld pi = acosl((ld)-1);
const ll mod = 1000000007;
// const ll mod = 1234567;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
#define endn "\n"
#define TO_STRING(VariableName) #VariableName

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  rep(i, (int)v.size() - 1) cout << v[i] << " ";
  cout << v[(int)v.size() - 1];
  return out;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const map<T1, T2> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}
template <class T> void debag(const T &obj) { cout << obj << endl; }

bool solve() {
  int n;
  cin >> n;
  ll ma = pow(10, 6) + 10;
  vector<ll> cnt(ma, 0);
  vector<ll> p(n);
  rep(i, n) {
    cin >> p[i];
    cnt[p[i]]++;
  }

  sort(all(p));
  int ans = 0;
  rep(i, n) {
    if (cnt[p[i]] == 1)
      ans++;
    if (cnt[p[i]] == 0)
      continue;
    cnt[p[i]] = 0;
    for (ll j = p[i] + p[i]; j < ma; j += p[i]) {
      cnt[j] = 0;
    }
  }
  cout << ans << endl;
  /*multiset<ll>a;
  rep(i,n){
          int x;a.insert(x);
  }
  int ans=0;
  while(!a.empty()){
          ll p=*a.begin();
          //cout<<1<<endl;
          a.erase(a.begin());
          int is=1;
          while(a.size()!=0 and *a.begin()==p){
                  is=0;
                  //cout<<2<<endl;
                  a.erase(a.begin());
          }
          ans+=is;
          ll x=p+p;
          while(x<pow(10,6)){
                  //cout<<3<<endl;
                  if(a.find(x)!=a.end())a.erase(x);
                  x+=p;
          }
  }
  cout<<ans<<endl;*/
  return false;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(15);
  solve();
}