Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define Int int32_t
#define all(c) c.begin(), c.end()
#define FAST ios_base::sync_with_stdio(false), cin.tie(NULL);
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define bitcnt(n) __builtin_popcountll(n)
#define setpre(n) cout << fixed << setprecision(n)
#define tr(c)                                                                  \
  for (auto x : c)                                                             \
    cout << x << " ";
#define ol(c, s, e)                                                            \
  for (int pos = s; pos < e; pos++)                                            \
    cout << c[pos] << " ";
#define PI acos(-1l)
const int M = 1000000007;
const int N = 3e5 + 5;
const int INF = 2e18 + 5;
// #define rohit
#ifdef rohit
template <typename... T> void bug(T... __VA_ARGS__) {
  ((cerr << __VA_ARGS__ << " "), ...);
}
#define debug(...)                                                             \
  cerr << "[" << #__VA_ARGS__ << "]:", bug(__VA_ARGS__), cerr << endl;
#endif

void solve() {
  int n;
  cin >> n;
  int a[n], mx = 0;
  map<int, int> mp;
  map<int, bool> used;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mx = max(a[i], mx);
    mp[a[i]]++;
    used[a[i]] = true;
  }
  for (auto x : mp) {
    if (x.second > 1)
      used[x.first] = false;
    for (int i = x.first; i <= mx; i += x.first) {
      if (i != x.first && mp.count(i)) {
        used[i] = false;
      }
    }
  }
  int ans = 0;
  for (auto x : mp) {
    if (used[x.first])
      ans++;
  }
  cout << ans;
}

signed main() {
  FAST;
  // TODO: check test cases.
  int tc = 1;
  //    cin >> tc;
  for (int t = 1; t <= tc; t++) {
    solve();
  }
}
/* Extra Cares
 * 1)In graphs or dp problems sometimes long long creates MLE.
 * 2)Check whether int to long long typecasting is commented or not?
 * 3)Check overflows.
 */

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define Int int32_t
#define all(c) c.begin(), c.end()
#define FAST ios_base::sync_with_stdio(false), cin.tie(NULL);
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define bitcnt(n) __builtin_popcountll(n)
#define setpre(n) cout << fixed << setprecision(n)
#define tr(c)                                                                  \
  for (auto x : c)                                                             \
    cout << x << " ";
#define ol(c, s, e)                                                            \
  for (int pos = s; pos < e; pos++)                                            \
    cout << c[pos] << " ";
#define PI acos(-1l)
const int M = 1000000007;
const int N = 3e5 + 5;
const int INF = 2e18 + 5;
// #define rohit
#ifdef rohit
template <typename... T> void bug(T... __VA_ARGS__) {
  ((cerr << __VA_ARGS__ << " "), ...);
}
#define debug(...)                                                             \
  cerr << "[" << #__VA_ARGS__ << "]:", bug(__VA_ARGS__), cerr << endl;
#endif

void solve() {
  int n;
  cin >> n;
  int a[n], mx = 0;
  map<int, int> mp;
  map<int, bool> used;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mx = max(a[i], mx);
    mp[a[i]]++;
    used[a[i]] = true;
  }
  if (mp.count(1)) {
    if (mp[1] == 1) {
      cout << 1;
    } else {
      cout << 0;
    }
    return;
  }
  for (auto x : mp) {
    if (x.second > 1)
      used[x.first] = false;
    for (int i = x.first; i <= mx; i += x.first) {
      if (i != x.first && mp.count(i)) {
        used[i] = false;
      }
    }
  }
  int ans = 0;
  for (auto x : mp) {
    if (used[x.first])
      ans++;
  }
  cout << ans;
}

signed main() {
  FAST;
  // TODO: check test cases.
  int tc = 1;
  //    cin >> tc;
  for (int t = 1; t <= tc; t++) {
    solve();
  }
}
/* Extra Cares
 * 1)In graphs or dp problems sometimes long long creates MLE.
 * 2)Check whether int to long long typecasting is commented or not?
 * 3)Check overflows.
 */