Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
// bismillahir rahmanir rahim            //Author:Fayed Anik

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

// #define         ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define lf double
#define pb(x) push_back(x)
#define ull unsigned long long
#define sfl(a) scanf("%lld", &a)
#define sf(a) scanf("%d", &a)
#define pf(a) printf("%d\n", a)
#define pfl(a) printf("%lld\n", a)
#define FOR(x, n) for (ll x = 1; x <= n; ++x)
#define pii pair<ll, ll>
#define mp(a, b) make_pair(a, b)
#define mod 1000000007
#define INF 2e18
#define EPS 1e-15
#define f1 first
#define f2 second
#define all(v) v.begin(), v.end()
#define PI acos(-1)
#define printminusone printf("-1\n")
#define bug printf("bug")
#define FILEIN freopen("in.txt", "r", stdin)
#define FILEOUT freopen("out.txt", "w", stdout)

// ll SET(ll mask,ll pos){ return mask = (mask | (1ll<<pos)); }
// ll RESET(ll mask,ll pos){ return mask = mask & ~(1ll<<pos); }
// bool CHECK(ll mask,ll pos) { return (bool) (mask & (1ll<<pos)); }
// priority_queue <ll, vector<ll>, greater<ll> > pq;

#define lim 2000005

ll a[lim], b[lim], cnt[lim];

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  ll n;
  sfl(n);
  FOR(i, n) {
    sfl(a[i]);
    cnt[a[i]]++;
  }
  sort(a + 1, a + n + 1);
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    if (!b[a[i]] and cnt[a[i]] == 1) {
      ans++;
    }
    for (ll j = a[i]; j <= 1e6; j += a[i]) {
      b[j] = 1;
    }
  }

  pfl(ans);
  return 0;
}


Fixed submission:
// bismillahir rahmanir rahim            //Author:Fayed Anik

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

// #define         ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define lf double
#define pb(x) push_back(x)
#define ull unsigned long long
#define sfl(a) scanf("%lld", &a)
#define sf(a) scanf("%d", &a)
#define pf(a) printf("%d\n", a)
#define pfl(a) printf("%lld\n", a)
#define FOR(x, n) for (ll x = 1; x <= n; ++x)
#define pii pair<ll, ll>
#define mp(a, b) make_pair(a, b)
#define mod 1000000007
#define INF 2e18
#define EPS 1e-15
#define f1 first
#define f2 second
#define all(v) v.begin(), v.end()
#define PI acos(-1)
#define printminusone printf("-1\n")
#define bug printf("bug")
#define FILEIN freopen("in.txt", "r", stdin)
#define FILEOUT freopen("out.txt", "w", stdout)

// ll SET(ll mask,ll pos){ return mask = (mask | (1ll<<pos)); }
// ll RESET(ll mask,ll pos){ return mask = mask & ~(1ll<<pos); }
// bool CHECK(ll mask,ll pos) { return (bool) (mask & (1ll<<pos)); }
// priority_queue <ll, vector<ll>, greater<ll> > pq;

#define lim 2000005

ll a[lim], b[lim], cnt[lim];

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  ll n;
  sfl(n);
  FOR(i, n) {
    sfl(a[i]);
    cnt[a[i]]++;
  }
  sort(a + 1, a + n + 1);
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    if (a[i] == a[i - 1])
      continue;
    if (!b[a[i]] and cnt[a[i]] == 1) {
      ans++;
    }
    for (ll j = a[i]; j <= 1e6; j += a[i]) {
      b[j] = 1;
    }
  }

  pfl(ans);
  return 0;
}