Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;
typedef long double LD;
using namespace std;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const ll INF64 = 3e18;
const double INFD = 1e30;
const double EPS = 1e-9;
const double PI = acos(-1);
const int MOD = 1000000007;
template <typename T> inline T read() {
  T X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
const int MAXN = 5005;
int n, m, k;
int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1};
int CASE = 1;
int dcmp(double x) {
  if (fabs(x) < EPS)
    return 0;
  return x > 0 ? 1 : -1;
}

struct Vector2 {
  double x, y;
  Vector2() {}
  Vector2(double x, double y) : x(x), y(y) {}
  Vector2(double rad) : x(cos(rad)), y(sin(rad)) {}
  friend istream &operator>>(istream &input, Vector2 &v) {
    input >> v.x >> v.y;
    return input;
  }
  friend ostream &operator<<(ostream &output, const Vector2 &v) {
    output << '(' << v.x << ',' << v.y << ')';
    return output;
  }
  static double dot(const Vector2 &a, const Vector2 &b) {
    return a.x * b.x + a.y * b.y;
  }
  static double angle(const Vector2 &a, const Vector2 &b) {
    return acos(dot(a, b) / a.length() / b.length());
  }
  static double cross(const Vector2 &a, const Vector2 &b) {
    return a.x * b.y - a.y * b.x;
  }
  static double area2(const Vector2 &a, const Vector2 &b, const Vector2 &c) {
    return cross(b - a, c - a);
  }

  double length() const { return sqrt(x * x + y * y); }
  double lengthSq() const { return x * x + y * y; }
  double getRad() const { return atan2(y, x); }
  Vector2 rotate(double rad) const {
    return Vector2(x * cos(rad) - y * sin(rad), x * sin(rad) + y * cos(rad));
  }
  Vector2 unit() const {
    double l = this->length();
    return Vector2(x / l, y / l);
  }
  Vector2 normal() const {
    double l = this->length();
    return Vector2(-y / l, x / l);
  }

  bool operator<(const Vector2 &other) const {
    return x < other.x || (x == other.x && y < other.y);
  }
  bool operator==(const Vector2 &other) const {
    return !dcmp(x - other.x) && !dcmp(y - other.y);
  }

  Vector2 &operator+=(const Vector2 &vec) {
    this->x += vec.x, this->y += vec.y;
    return *this;
  }
  Vector2 operator+(const Vector2 &vec) const {
    return Vector2(this->x + vec.x, this->y + vec.y);
  }
  Vector2 &operator-=(const Vector2 &vec) {
    this->x -= vec.x, this->y -= vec.y;
    return *this;
  }
  Vector2 operator-(const Vector2 &vec) const {
    return Vector2(this->x - vec.x, this->y - vec.y);
  }
  Vector2 &operator*=(double v) {
    this->x *= v, this->y *= v;
    return *this;
  }
  Vector2 &operator*=(const Vector2 &v) {
    this->x *= v.x, this->y *= v.y;
    return *this;
  }
  Vector2 operator*(double v) const {
    return Vector2(this->x * v, this->y * v);
  }
  Vector2 operator*(const Vector2 &vec) const {
    return Vector2(this->x * vec.x, this->y * vec.y);
  }
  Vector2 &operator/=(double v) {
    this->x /= v, this->y /= v;
    return *this;
  }
  Vector2 &operator/=(const Vector2 &vec) {
    this->x /= vec.x, this->y /= vec.y;
    return *this;
  }
  Vector2 operator/(double v) const {
    return Vector2(this->x / v, this->y / v);
  }
  Vector2 operator/(const Vector2 &vec) const {
    return Vector2(this->x / vec.x, this->y / vec.y);
  }
};

struct Line {
  Vector2 p, dir;
  Line() {}
  Line(const Vector2 &p, const Vector2 &dir) : p(p), dir(dir) {}
  static Vector2 lineIntersection(const Line &l1, const Line &l2) {
    double t =
        Vector2::cross(l2.dir, l1.p - l2.p) / Vector2::cross(l1.dir, l2.dir);
    return l1.p + l1.dir * t;
  }
  static double distanceToLine(const Vector2 &p, const Line &line) {
    return fabs(Vector2::cross(line.dir, p - line.p)) / line.dir.length();
  }

  static Vector2 lineProjection(const Vector2 &p, const Line &l) {
    return l.p +
           l.dir * Vector2::dot(l.dir, p - l.p) / Vector2::dot(l.dir, l.dir);
  }
};

struct Segment {
  Vector2 p1, p2;
  Segment() {}
  Segment(const Vector2 &p1, const Vector2 &p2) : p1(p1), p2(p2) {}
  static double distanceToSegment(const Vector2 &p, const Segment &seg) {
    if (seg.p1 == seg.p2)
      return (p - seg.p1).length();
    Vector2 ab = seg.p2 - seg.p1, ap = p - seg.p1, bp = p - seg.p2;
    if (dcmp(Vector2::dot(ab, ap)) < 0)
      return ap.length();
    else if (dcmp(Vector2::dot(ab, bp)) > 0)
      return bp.length();
    else
      return fabs(Vector2::cross(ab, ap) / ab.length());
  }
  static bool properIntersection(const Segment &s1, const Segment &s2) {
    double c1 = Vector2::cross(s1.p2 - s1.p1, s2.p1 - s1.p1),
           c2 = Vector2::cross(s1.p2 - s1.p1, s2.p2 - s1.p1),
           c3 = Vector2::cross(s2.p2 - s2.p1, s1.p1 - s2.p1),
           c4 = Vector2::cross(s2.p2 - s2.p1, s1.p2 - s2.p1);
    return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
  }
  static bool onSegment(const Vector2 &p, const Segment &s) {
    return !dcmp(Vector2::cross(s.p1 - p, s.p2 - p)) &&
           dcmp(Vector2::dot(s.p1 - p, s.p2 - p)) < 0;
  }
};
int arr[MAXN];
int main() {
#ifdef LOCALLL
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  scanf("%d", &n);
  map<int, int> cntt;
  for (int i = 0; i < n; i++) {
    cntt[arr[i] = read<int>()]++;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int x = arr[i];
    cntt[x]--;
    bool flag = true;
    for (int j = 1; j * j <= x; j++) {
      if (x % j == 0) {
        if (cntt.count(j) && cntt[j] > 0) {
          flag = false;
          break;
        }
        if (cntt.count(x / j) && cntt[x / j] > 0) {
          flag = false;
          break;
        }
      }
    }
    if (flag) {
      ans++;
    }
    cntt[x]++;
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;
typedef long double LD;
using namespace std;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const ll INF64 = 3e18;
const double INFD = 1e30;
const double EPS = 1e-9;
const double PI = acos(-1);
const int MOD = 1000000007;
template <typename T> inline T read() {
  T X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
const int MAXN = 200005;
int n, m, k;
int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1};
int CASE = 1;
int dcmp(double x) {
  if (fabs(x) < EPS)
    return 0;
  return x > 0 ? 1 : -1;
}

struct Vector2 {
  double x, y;
  Vector2() {}
  Vector2(double x, double y) : x(x), y(y) {}
  Vector2(double rad) : x(cos(rad)), y(sin(rad)) {}
  friend istream &operator>>(istream &input, Vector2 &v) {
    input >> v.x >> v.y;
    return input;
  }
  friend ostream &operator<<(ostream &output, const Vector2 &v) {
    output << '(' << v.x << ',' << v.y << ')';
    return output;
  }
  static double dot(const Vector2 &a, const Vector2 &b) {
    return a.x * b.x + a.y * b.y;
  }
  static double angle(const Vector2 &a, const Vector2 &b) {
    return acos(dot(a, b) / a.length() / b.length());
  }
  static double cross(const Vector2 &a, const Vector2 &b) {
    return a.x * b.y - a.y * b.x;
  }
  static double area2(const Vector2 &a, const Vector2 &b, const Vector2 &c) {
    return cross(b - a, c - a);
  }

  double length() const { return sqrt(x * x + y * y); }
  double lengthSq() const { return x * x + y * y; }
  double getRad() const { return atan2(y, x); }
  Vector2 rotate(double rad) const {
    return Vector2(x * cos(rad) - y * sin(rad), x * sin(rad) + y * cos(rad));
  }
  Vector2 unit() const {
    double l = this->length();
    return Vector2(x / l, y / l);
  }
  Vector2 normal() const {
    double l = this->length();
    return Vector2(-y / l, x / l);
  }

  bool operator<(const Vector2 &other) const {
    return x < other.x || (x == other.x && y < other.y);
  }
  bool operator==(const Vector2 &other) const {
    return !dcmp(x - other.x) && !dcmp(y - other.y);
  }

  Vector2 &operator+=(const Vector2 &vec) {
    this->x += vec.x, this->y += vec.y;
    return *this;
  }
  Vector2 operator+(const Vector2 &vec) const {
    return Vector2(this->x + vec.x, this->y + vec.y);
  }
  Vector2 &operator-=(const Vector2 &vec) {
    this->x -= vec.x, this->y -= vec.y;
    return *this;
  }
  Vector2 operator-(const Vector2 &vec) const {
    return Vector2(this->x - vec.x, this->y - vec.y);
  }
  Vector2 &operator*=(double v) {
    this->x *= v, this->y *= v;
    return *this;
  }
  Vector2 &operator*=(const Vector2 &v) {
    this->x *= v.x, this->y *= v.y;
    return *this;
  }
  Vector2 operator*(double v) const {
    return Vector2(this->x * v, this->y * v);
  }
  Vector2 operator*(const Vector2 &vec) const {
    return Vector2(this->x * vec.x, this->y * vec.y);
  }
  Vector2 &operator/=(double v) {
    this->x /= v, this->y /= v;
    return *this;
  }
  Vector2 &operator/=(const Vector2 &vec) {
    this->x /= vec.x, this->y /= vec.y;
    return *this;
  }
  Vector2 operator/(double v) const {
    return Vector2(this->x / v, this->y / v);
  }
  Vector2 operator/(const Vector2 &vec) const {
    return Vector2(this->x / vec.x, this->y / vec.y);
  }
};

struct Line {
  Vector2 p, dir;
  Line() {}
  Line(const Vector2 &p, const Vector2 &dir) : p(p), dir(dir) {}
  static Vector2 lineIntersection(const Line &l1, const Line &l2) {
    double t =
        Vector2::cross(l2.dir, l1.p - l2.p) / Vector2::cross(l1.dir, l2.dir);
    return l1.p + l1.dir * t;
  }
  static double distanceToLine(const Vector2 &p, const Line &line) {
    return fabs(Vector2::cross(line.dir, p - line.p)) / line.dir.length();
  }

  static Vector2 lineProjection(const Vector2 &p, const Line &l) {
    return l.p +
           l.dir * Vector2::dot(l.dir, p - l.p) / Vector2::dot(l.dir, l.dir);
  }
};

struct Segment {
  Vector2 p1, p2;
  Segment() {}
  Segment(const Vector2 &p1, const Vector2 &p2) : p1(p1), p2(p2) {}
  static double distanceToSegment(const Vector2 &p, const Segment &seg) {
    if (seg.p1 == seg.p2)
      return (p - seg.p1).length();
    Vector2 ab = seg.p2 - seg.p1, ap = p - seg.p1, bp = p - seg.p2;
    if (dcmp(Vector2::dot(ab, ap)) < 0)
      return ap.length();
    else if (dcmp(Vector2::dot(ab, bp)) > 0)
      return bp.length();
    else
      return fabs(Vector2::cross(ab, ap) / ab.length());
  }
  static bool properIntersection(const Segment &s1, const Segment &s2) {
    double c1 = Vector2::cross(s1.p2 - s1.p1, s2.p1 - s1.p1),
           c2 = Vector2::cross(s1.p2 - s1.p1, s2.p2 - s1.p1),
           c3 = Vector2::cross(s2.p2 - s2.p1, s1.p1 - s2.p1),
           c4 = Vector2::cross(s2.p2 - s2.p1, s1.p2 - s2.p1);
    return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
  }
  static bool onSegment(const Vector2 &p, const Segment &s) {
    return !dcmp(Vector2::cross(s.p1 - p, s.p2 - p)) &&
           dcmp(Vector2::dot(s.p1 - p, s.p2 - p)) < 0;
  }
};
int arr[MAXN];
int main() {
#ifdef LOCALLL
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  scanf("%d", &n);
  map<int, int> cntt;
  for (int i = 0; i < n; i++) {
    cntt[arr[i] = read<int>()]++;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int x = arr[i];
    cntt[x]--;
    bool flag = true;
    for (int j = 1; j * j <= x; j++) {
      if (x % j == 0) {
        if (cntt.count(j) && cntt[j] > 0) {
          flag = false;
          break;
        }
        if (cntt.count(x / j) && cntt[x / j] > 0) {
          flag = false;
          break;
        }
      }
    }
    if (flag) {
      ans++;
    }
    cntt[x]++;
  }
  printf("%d\n", ans);
  return 0;
}