Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
// #include <random>
#include <map>
// #include <unordered_map>
#include <cstdlib>
#include <functional>
#include <time.h>
// #include <chrono>
#include <iomanip>
#include <iostream>
#include <limits>
#include <numeric>
#include <sstream>
// #include <type_traits>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define MOD 1000000007
#define SORT(s) sort(s.begin(), s.end());
#define REVERSE(s) reverse(s.begin(), s.end());
#define SUM(s) accumulate(s.begin(), s.end(), 0LL);
#define P pair<int, int>
#define mp make_pair
#define valid(x, y, h, w) (0 <= x && x < h && 0 <= y && y < w)
// #define int ll
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int ndx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int ndy[8] = {1, 0, -1, 0, 1, -1, -1, 1};
double pi = 3.141592653589793;
using namespace std;
int POW(int x, int y) { return int(pow(double(x), double(y))); }
double log(double a, double b) { return log(b) / log(a); }

int main() {

  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }

  vector<int> x(200004);
  int c1 = 0;
  rep(i, n) {
    x[a[i]]++;
    if (a[i] == 1) {
      c1++;
    }
  }

  if (c1 > 1) {
    cout << 0 << endl;
    return 0;
  }

  if (c1 == 1) {
    cout << 1 << endl;
    return 0;
  }

  int ans = 0;
  rep(i, n) {
    int t = a[i];

    vector<int> s;

    for (int k = 2; k * k <= a[i]; k++) {
      if (t % k == 0) {
        s.push_back(k);
        if (k * k != t) {
          s.push_back(t / k);
        }
      }
    }

    int flg = 1;
    rep(j, s.size()) {
      if (x[s[j]]) {
        flg = 0;
        break;
      }
    }
    if (x[a[i]] > 1) {
      flg = 0;
    }

    if (flg) {
      ans++;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
// #include <random>
#include <map>
// #include <unordered_map>
#include <cstdlib>
#include <functional>
#include <time.h>
// #include <chrono>
#include <iomanip>
#include <iostream>
#include <limits>
#include <numeric>
#include <sstream>
// #include <type_traits>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define MOD 1000000007
#define SORT(s) sort(s.begin(), s.end());
#define REVERSE(s) reverse(s.begin(), s.end());
#define SUM(s) accumulate(s.begin(), s.end(), 0LL);
#define P pair<int, int>
#define mp make_pair
#define valid(x, y, h, w) (0 <= x && x < h && 0 <= y && y < w)
// #define int ll
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int ndx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int ndy[8] = {1, 0, -1, 0, 1, -1, -1, 1};
double pi = 3.141592653589793;
using namespace std;
int POW(int x, int y) { return int(pow(double(x), double(y))); }
double log(double a, double b) { return log(b) / log(a); }

int main() {

  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }

  vector<int> x(1000005);
  int c1 = 0;
  rep(i, n) {
    x[a[i]]++;
    if (a[i] == 1) {
      c1++;
    }
  }

  if (c1 > 1) {
    cout << 0 << endl;
    return 0;
  }

  if (c1 == 1) {
    cout << 1 << endl;
    return 0;
  }

  int ans = 0;
  rep(i, n) {
    int t = a[i];

    vector<int> s;

    for (int k = 2; k * k <= a[i]; k++) {
      if (t % k == 0) {
        s.push_back(k);
        if (k * k != t) {
          s.push_back(t / k);
        }
      }
    }

    int flg = 1;
    rep(j, s.size()) {
      if (x[s[j]]) {
        flg = 0;
        break;
      }
    }
    if (x[a[i]] > 1) {
      flg = 0;
    }

    if (flg) {
      ans++;
    }
  }
  cout << ans << endl;
}