Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define dbg puts("It's Just Begining.Wait For The End!!!!!\n")
#define CHECK(x) cout << (#x) << " is " << (x) << "\n";
#define endl printf("\n")
typedef pair<ll, ll> pii;
#define pb push_back
#define ms(a, b) memset(a, b, sizeof(a))
#define in(x) scanf("%lld", &x)
#define out(x) printf("%lld", x)
#define X first
#define Y second
#define sps printf(" ")
#define dou(x) cout << fixed << setprecision(6) << x
#define all(v) v.begin(), v.end()
#define dis(it, it1) distance(it, it1) + 1
#define MAX 1000005
#define INF 1000000000
#define MOD 1000000007
#define print(cs) printf("Case %lld:\n", cs)
#define Yesno(ok)                                                              \
  if (ok)                                                                      \
    puts("YES");                                                               \
  else                                                                         \
    puts("NO")
#define yesno(ok)                                                              \
  if (ok)                                                                      \
    puts("Yes");                                                               \
  else                                                                         \
    puts("No")
ll lcm(ll a, ll b) { return ((a * b) / __gcd(a, b)); }
int main() {
  ll i, j, k, l, n, m, r;
  in(n);
  ll arr[n];
  map<ll, ll> visit;
  for (i = 0; i < n; i++) {
    in(arr[i]);
    visit[arr[i]]++;
  }
  ll cnt = 0;
  if (visit[1LL]) {
    if (visit[1] == 1)
      puts("1");
    else
      puts("0");
    exit(0);
  }
  for (i = 0; i < n; i++) {
    if (visit[arr[i]] > 1)
      continue;
    bool ok = false;
    for (j = 2; j * j <= arr[i]; j++) {
      if (arr[i] % j == 0 && (visit[j] || visit[arr[i] / j])) {
        ok = true;
        break;
      }
    }
    if (!ok)
      cnt++;
  }
  out(cnt);
  endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define dbg puts("It's Just Begining.Wait For The End!!!!!\n")
#define CHECK(x) cout << (#x) << " is " << (x) << "\n";
#define endl printf("\n")
typedef pair<ll, ll> pii;
#define pb push_back
#define ms(a, b) memset(a, b, sizeof(a))
#define in(x) scanf("%lld", &x)
#define out(x) printf("%lld", x)
#define X first
#define Y second
#define sps printf(" ")
#define dou(x) cout << fixed << setprecision(6) << x
#define all(v) v.begin(), v.end()
#define dis(it, it1) distance(it, it1) + 1
#define MAX 1000005
#define INF 1000000000
#define MOD 1000000007
#define print(cs) printf("Case %lld:\n", cs)
#define Yesno(ok)                                                              \
  if (ok)                                                                      \
    puts("YES");                                                               \
  else                                                                         \
    puts("NO")
#define yesno(ok)                                                              \
  if (ok)                                                                      \
    puts("Yes");                                                               \
  else                                                                         \
    puts("No")
ll lcm(ll a, ll b) { return ((a * b) / __gcd(a, b)); }
int main() {
  ll i, j, k, l, n, m, r;
  in(n);
  ll arr[n];
  unordered_map<ll, ll> visit;
  for (i = 0; i < n; i++) {
    in(arr[i]);
    visit[arr[i]]++;
  }
  ll cnt = 0;
  if (visit[1LL]) {
    if (visit[1] == 1)
      puts("1");
    else
      puts("0");
    exit(0);
  }
  for (i = 0; i < n; i++) {
    if (visit[arr[i]] > 1)
      continue;
    bool ok = false;
    for (j = 2; j * j <= arr[i]; j++) {
      if (arr[i] % j == 0 && (visit[j] || visit[arr[i] / j])) {
        ok = true;
        break;
      }
    }
    if (!ok)
      cnt++;
  }
  out(cnt);
  endl;
}