Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef int itn;
const ll LINF = 1e18;
const int INF = 1e9;

// マクロ定義
#define vvint(vec, n, m, l)                                                    \
  vector<vector<int>> vec(n, vector<int>(m, l)); // lで初期化
#define vvll(vec, n, m, l) vector<vector<ll>> vec(n, vector<ll>(m, l));
#define vint vector<int>
#define pint pair<int, int>
#define rep(i, a) for (ll i = 0; i < (a); i++)
#define all(x) (x).begin(), (x).end()
#define debug system("pause") // デバッグ用
#define ret return 0

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;
}

using Graph = vector<vector<ll>>;

#define ketasitei setprecision(15) // 15桁表示

const ll MOD = 1000000007;
const double PI = 3.1415926535897932;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;
  map<ll, ll> a;
  vector<ll> b(n);

  ll tmp;
  rep(i, n) {
    cin >> tmp;
    a[tmp]++;
    b[i] = tmp;
  }

  sort(all(b));

  if (a[1] > 0) {
    if (a[1] > 1)
      cout << 0 << endl;
    else
      cout << 1 << endl;
    ret;
  }
  ll ans = 0;
  bool ok = false;

  rep(i, n) {
    if (i > 1 && b[i] == b[i - 1])
      continue;
    if (a[b[i]] > 1)
      continue;

    ll tmp = b[i];
    ok = false;
    for (ll j = 1; j * j <= b[i]; j++) {
      if (b[i] % j == 0) {
        if (a[j] > 0) {
          ok = false;
          break;
        }
        if (a[b[i] / j] > 0 && j != 1) {
          ok = false;
          break;
        }
        if (a[b[i] / j] > 1 && j == 1) {
          ok = false;
          break;
        }
      } else {
        if ((j + 1) * (j + 1) > b[i])
          ok = true;
        continue;
      }
      ok = true;
    }
    if (ok)
      ans++;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef int itn;
const ll LINF = 1e18;
const int INF = 1e9;

// マクロ定義
#define vvint(vec, n, m, l)                                                    \
  vector<vector<int>> vec(n, vector<int>(m, l)); // lで初期化
#define vvll(vec, n, m, l) vector<vector<ll>> vec(n, vector<ll>(m, l));
#define vint vector<int>
#define pint pair<int, int>
#define rep(i, a) for (ll i = 0; i < (a); i++)
#define all(x) (x).begin(), (x).end()
#define debug system("pause") // デバッグ用
#define ret return 0

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;
}

using Graph = vector<vector<ll>>;

#define ketasitei setprecision(15) // 15桁表示

const ll MOD = 1000000007;
const double PI = 3.1415926535897932;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;
  vector<ll> a(1000001);
  vector<ll> b(n);

  ll tmp;
  rep(i, n) {
    cin >> tmp;
    a[tmp]++;
    b[i] = tmp;
  }

  sort(all(b));

  if (a[1] > 0) {
    if (a[1] > 1)
      cout << 0 << endl;
    else
      cout << 1 << endl;
    ret;
  }
  ll ans = 0;
  bool ok = false;

  rep(i, n) {
    if (i > 1 && b[i] == b[i - 1])
      continue;
    if (a[b[i]] > 1)
      continue;

    ll tmp = b[i];
    ok = false;
    for (ll j = 1; j * j <= b[i]; j++) {
      if (b[i] % j == 0) {
        if (a[j] > 0) {
          ok = false;
          break;
        }
        if (a[b[i] / j] > 0 && j != 1) {
          ok = false;
          break;
        }
        if (a[b[i] / j] > 1 && j == 1) {
          ok = false;
          break;
        }
      } else {
        if ((j + 1) * (j + 1) > b[i])
          ok = true;
        continue;
      }
      ok = true;
    }
    if (ok)
      ans++;
  }
  cout << ans << endl;

  return 0;
}