Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define LLMAX (1ll << 60) - 1
#define INTMAX (1 << 30) - 1
#define MOD 1000000007
#define NMAX 1000 * 100 + 1

#define numberOfSetBits(S)                                                     \
  __builtin_popcount(S) // __builtin_popcountl(S) __builtin_popcountll(S)
#define MSET(x, y) memset(x, y, sizeof(x))
#define gcd(a, b) __gcd(a, b)
#define all(x) x.begin(), x.end()
#define isOn(S, j) (S & (1 << j))
#define endl '\n'
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define print(x)                                                               \
  for (auto it : x)                                                            \
    cout << it << ' ';                                                         \
  cout << endl;
#define printii(x)                                                             \
  for (auto it : x)                                                            \
    cout << it.F << ' ' << it.S << '\t';                                       \
  cout << endl;
#define in(x, n)                                                               \
  for (int e = 0; e < n; e++) {                                                \
    ll y;                                                                      \
    cin >> y;                                                                  \
    x.pb(y);                                                                   \
  }

#define vi vector<ll>
#define vvi vector<vi>
#define ii pair<ll, ll>
#define pll pair<ll, ll>
#define vii vector<ii>
#define vvii vector<vii>
#define viii vector<pair<ii, ll>>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mc(a, b, c) mp(mp(a, b), c)
vi a;
map<ll, ll> ma;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, x, t, m, y, z;
  cin >> n;
  in(a, n);
  for (int i = 0; i < n; i++) {
    ma[a[i]]++;
  }
  z = 0;
  for (int i = 0; i < n; i++) {
    bool ok = true;
    for (int j = 1; j * j <= a[i]; j++) {
      if (a[i] % j == 0) {
        x = j;
        y = a[i] / j;
        if (x == a[i]) {
          if (ma[x] > 1)
            ok = false;
        } else {
          if (ma[x])
            ok = false;
        }
        if (y == a[i]) {
          if (ma[y] > 1)
            ok = false;
        } else {
          if (ma[y])
            ok = false;
        }
      }
    }
    z += ok;
  }
  cout << z;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define LLMAX (1ll << 60) - 1
#define INTMAX (1 << 30) - 1
#define MOD 1000000007
#define NMAX 1000 * 100 + 1

#define numberOfSetBits(S)                                                     \
  __builtin_popcount(S) // __builtin_popcountl(S) __builtin_popcountll(S)
#define MSET(x, y) memset(x, y, sizeof(x))
#define gcd(a, b) __gcd(a, b)
#define all(x) x.begin(), x.end()
#define isOn(S, j) (S & (1 << j))
#define endl '\n'
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define print(x)                                                               \
  for (auto it : x)                                                            \
    cout << it << ' ';                                                         \
  cout << endl;
#define printii(x)                                                             \
  for (auto it : x)                                                            \
    cout << it.F << ' ' << it.S << '\t';                                       \
  cout << endl;
#define in(x, n)                                                               \
  for (int e = 0; e < n; e++) {                                                \
    ll y;                                                                      \
    cin >> y;                                                                  \
    x.pb(y);                                                                   \
  }

#define vi vector<ll>
#define vvi vector<vi>
#define ii pair<ll, ll>
#define pll pair<ll, ll>
#define vii vector<ii>
#define vvii vector<vii>
#define viii vector<pair<ii, ll>>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mc(a, b, c) mp(mp(a, b), c)
vi a;
ll ma[1000004];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, x, t, m, y, z;
  cin >> n;
  in(a, n);
  for (int i = 0; i < n; i++) {
    ma[a[i]]++;
  }
  z = 0;
  for (int i = 0; i < n; i++) {
    bool ok = true;
    for (int j = 1; j * j <= a[i]; j++) {
      if (a[i] % j == 0) {
        x = j;
        y = a[i] / j;
        if (x == a[i]) {
          if (ma[x] > 1)
            ok = false;
        } else {
          if (ma[x])
            ok = false;
        }
        if (y == a[i]) {
          if (ma[y] > 1)
            ok = false;
        } else {
          if (ma[y])
            ok = false;
        }
      }
    }
    z += ok;
  }
  cout << z;

  return 0;
}