prithivMLmods/Coder-Stat huggingface dataset:
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int num[100005];
int cnt[1000005];
bool hobena[1000005];
bool ase[1000005];

int main() {
  int n;
  cin >> n;

  for (int i = 1; i <= n; i++)
    cin >> num[i], ase[num[i]] = 1, cnt[num[i]]++;

  for (int i = 1; i <= 1000000; i++) {
    if (!ase[i])
      continue;
    for (int j = 2 * i; j <= 1000000; j += i)
      hobena[j] = 1;
  }

  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (!hobena[num[i]] and cnt[num[i]] == 1)
      ans++;

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int num[200005];
int cnt[1000005];
bool hobena[1000005];
bool ase[1000005];

int main() {
  int n;
  cin >> n;

  for (int i = 1; i <= n; i++)
    cin >> num[i], ase[num[i]] = 1, cnt[num[i]]++;

  for (int i = 1; i <= 1000000; i++) {
    if (!ase[i])
      continue;
    for (int j = 2 * i; j <= 1000000; j += i)
      hobena[j] = 1;
  }

  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (!hobena[num[i]] and cnt[num[i]] == 1)
      ans++;

  cout << ans << endl;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

vector<long long> divisor(long long n) { // nの約数列挙
  vector<long long> res;
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.push_back(i);
      if (i != n / i)
        res.push_back(n / i);
    }
  }
  return res;
}

typedef pair<long long, long long> P;
int main() {
  long long N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  map<long long, long long> m;
  sort(A.begin(), A.end());
  vector<int> ju(N, 0);
  for (int i = 1; i < N; i++) {
    if (A[i] == A[i - 1]) {
      ju[i] = -1;
      ju[i - 1] = -1;
    }
  }
  for (int i = 0; i < N; i++) {
    if (m.count(A[i]))
      continue;
    long long t = A[i];
    long long res = 2;
    while (t * res <= 1000000) {
      m[t * res]++;
      res++;
    }
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    if (m[A[i]] == 0 && ju[i] == 0)
      ans++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

vector<long long> divisor(long long n) { // nの約数列挙
  vector<long long> res;
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.push_back(i);
      if (i != n / i)
        res.push_back(n / i);
    }
  }
  return res;
}

typedef pair<long long, long long> P;
int main() {
  long long N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  map<long long, long long> m;
  sort(A.begin(), A.end());
  vector<int> ju(N, 0);
  for (int i = 1; i < N; i++) {
    if (A[i] == A[i - 1]) {
      ju[i] = -1;
      ju[i - 1] = -1;
    }
  }
  for (int i = 0; i < N; i++) {
    if (m.count(A[i]))
      continue;
    long long t = A[i];
    long long res = 2;
    while (t * res <= A[N - 1]) {
      m[t * res]++;
      res++;
    }
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    if (m[A[i]] == 0 && ju[i] == 0)
      ans++;
  }
  cout << ans << endl;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define MAX 200005
#define INF 1000005
#define endl "\n"
int arr[MAX];
int f[INF];
bool p[INF];
bool flag[MAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  for (int i = 2; i <= INF; i++) {
    if (!p[i]) {
      for (lli j = i * i; j <= INF; j += i) {
        p[j] = true;
      }
    }
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    f[arr[i]]++;
  }
  int c = 0;
  bool flag = false;
  for (int i = 0; i < n; i++) {
    flag = false;
    f[arr[i]]--;
    if (!p[arr[i]]) {
      if (f[1] || f[arr[i]]) {
      } else {
        c++;
      }
      f[arr[i]]++;
      continue;
    }
    for (int j = 1; j * j <= arr[i]; j++) {
      if (arr[i] % j == 0) {
        if (f[j] || f[arr[i] / j]) {
          flag = true;
          break;
        }
      }
    }
    if (!flag) {
      c++;
    }
    f[arr[i]]++;
  }
  cout << c << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define MAX 200005
#define INF 1000005
#define endl "\n"
int arr[MAX];
int f[INF];
bool p[INF];
bool flag[MAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  for (lli i = 2; i <= INF; i++) {
    if (!p[i]) {
      for (lli j = i * i; j <= INF; j += i) {
        p[j] = true;
      }
    }
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    f[arr[i]]++;
  }
  int c = 0;
  bool flag = false;
  for (int i = 0; i < n; i++) {
    flag = false;
    f[arr[i]]--;
    if (!p[arr[i]]) {
      if (f[1] || f[arr[i]]) {
      } else {
        c++;
      }
      f[arr[i]]++;
      continue;
    }
    for (int j = 1; j * j <= arr[i]; j++) {
      if (arr[i] % j == 0) {
        if (f[j] || f[arr[i] / j]) {
          flag = true;
          break;
        }
      }
    }
    if (!flag) {
      c++;
    }
    f[arr[i]]++;
  }
  cout << c << endl;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 1e9;

int n, res, u, mx, y, t;

int main() {
  cin >> n;
  vector<int> vec(n, 0);
  map<int, int> m;
  map<int, bool> flag;
  for (int i = 0; i < n; ++i) {
    cin >> vec[i];
    m[vec[i]]++;
    mx = max(mx, vec[i]);
    flag[vec[i]] = false;
  }
  res = 0;
  for (auto x : m) {
    if (x.second > 1)
      flag[x.first] = true;
    t = x.first;
    while (t <= mx) {
      t += x.first;
      if (m.count(t)) {
        flag[t] = true;
      }
    }
  }
  for (auto x : flag)
    if (!x.second)
      res++;
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 1e9;

int n, res, u, mx, y, t;

int main() {
  cin >> n;
  vector<int> vec(n, 0);
  map<int, int> m;
  map<int, bool> flag;
  for (int i = 0; i < n; ++i) {
    cin >> vec[i];
    m[vec[i]]++;
    mx = max(mx, vec[i]);
    flag[vec[i]] = false;
  }
  res = 0;
  for (auto x : m) {
    if (x.second > 1)
      flag[x.first] = true;
    t = x.first;
    for (int r = 2; x.first * r <= mx; r += 4) {
      t = x.first * r;
      if (m.count(t)) {
        flag[t] = true;
      }
      t = x.first * (r + 1);
      if (m.count(t)) {
        flag[t] = true;
      }
      t = x.first * (r + 2);
      if (m.count(t)) {
        flag[t] = true;
      }
      t = x.first * (r + 3);
      if (m.count(t)) {
        flag[t] = true;
      }
    }
  }
  for (auto x : flag)
    if (!x.second)
      res++;
  cout << res << endl;
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t N;
  cin >> N;
  std::vector<int64_t> A(N);
  multiset<int64_t> sets;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sets.insert(A[i]);
  }

  int64_t max = 200001;
  vector<int64_t> nums(200001, 0);

  for (int i = 0; i < N; i++) {
    int64_t hit = A[i];

    if (nums[hit] >= 1) {
      nums[hit] += 1;
      continue;
    }
    while (hit < max) {
      nums[hit] += 1;
      hit += A[i];
    }
  }

  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    if (nums[A[i]] == 1)
      ans++;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t N;
  cin >> N;
  std::vector<int64_t> A(N);
  multiset<int64_t> sets;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sets.insert(A[i]);
  }

  int64_t max = 10e6 + 1;
  vector<int64_t> nums(max, 0);

  for (int i = 0; i < N; i++) {
    int64_t hit = A[i];

    if (nums[hit] >= 1) {
      nums[hit] += 1;
      continue;
    }
    while (hit < max) {
      nums[hit] += 1;
      hit += A[i];
    }
  }

  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    if (nums[A[i]] == 1)
      ans++;
  }

  cout << ans << endl;

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int N;
  cin >> N;
  int Amax;
  vector<int> A(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    Amax = max(Amax, A[i]);
  }
  A.push_back(10000000);
  sort(A.begin(), A.end());
  vector<bool> not_divisible(Amax + 1, true);
  int ans = 0;
  for (int i = 0; i < N; i++) {
    int x = A[i];
    if (A[i] == A[i + 1]) {
      if (not_divisible[x]) {
        while (x <= Amax) {
          not_divisible[x] = false;
          x += A[i];
        }
      }
      while (A[i] == A[i + 1]) {
        i++;
      }
    } else {
      if (not_divisible[x]) {
        while (x <= Amax) {
          not_divisible[x] = false;
          x += A[i];
        }
        ans++;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int N;
  cin >> N;
  int Amax;
  vector<int> A(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    Amax = max(Amax, A[i]);
  }
  A.push_back(10000000);
  sort(A.begin(), A.end());
  vector<bool> not_divisible(Amax + 1, true);
  int ans = 0;
  for (int i = 0; i < N; i++) {
    int x = A[i];
    if (A[i] == A[i + 1]) {
      if (not_divisible[x]) {
        while (x <= Amax) {
          not_divisible[x] = false;
          x += A[i];
        }
      }
      while (A[i] == A[i + 1]) {
        i++;
      }
    } else {
      if (not_divisible[x]) {
        while (x <= Amax) {
          not_divisible[x] = false;
          x += A[i];
        }
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 1001001001;
using P = pair<int, int>;

const int Max = 1000005;
int main(void) {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> memo(Max);
  rep(i, n) {
    if (memo[a[i]] != 0) {
      memo[a[i]] = 2;
    } else {
      for (int x = a[i]; x < Max; x + x)
        memo[x]++;
    }
  }
  int ans = 0;
  rep(i, n) if (memo[a[i]] == 1) ans++;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 1001001001;
using P = pair<int, int>;

const int Max = 1000005;
int main(void) {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> memo(Max);
  rep(i, n) {
    if (memo[a[i]] != 0) {
      memo[a[i]] = 2;
    } else {
      for (int x = a[i]; x < Max; x += a[i])
        memo[x]++;
    }
  }
  int ans = 0;
  rep(i, n) if (memo[a[i]] == 1) ans++;
  cout << ans << endl;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  vector<int> cnt(1100000);
  rep(i, n) {
    for (int j = a[i]; j <= 1100000; j += a[i]) {
      cnt[j]++;
    }
  }
  int sum = n;
  rep(i, n) {
    if (cnt[a[i]] >= 2)
      sum--;
  }
  cout << sum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  vector<int> cnt(1100000);
  rep(i, n) {
    if (cnt[a[i]] != 0) {
      cnt[a[i]] = 2;
      continue;
    }
    for (int j = a[i]; j <= 1100000; j += a[i]) {
      cnt[j]++;
    }
  }
  int sum = n;
  rep(i, n) {
    if (cnt[a[i]] >= 2)
      sum--;
  }
  cout << sum << endl;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  int i;
  long long j;
  long long k;
  long long p;

  cin >> n;

  long long a[n + 1]; // n個の整数
  int b[1000010]{0};

  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    p = a[i];
    k = 1000010;
    for (j = p; j < k; j += p)
      b[j]++;
  }
  p = 0;
  for (i = 0; i < n; i++) {
    if (b[a[i]] == 1)
      p++;
  }
  cout << p << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  int i;
  long long j;
  long long k;
  long long p;

  cin >> n;

  long long a[n + 1]; // n個の整数
  int b[1000010]{0};

  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    p = a[i];
    if (b[p] > 0) {
      b[p]++;
      continue;
    }
    k = 1000010;
    for (j = p; j < k; j += p)
      b[j]++;
  }
  p = 0;
  for (i = 0; i < n; i++) {
    if (b[a[i]] == 1)
      p++;
  }
  cout << p << endl;
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
/* Author: $%U%$
 * Time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
 **/
#include <bits/stdc++.h>
using namespace std;
#define fore(i, a, b) for (int i = a, to = b; i < to; i++)
#define foref(i, a, b) for (int i = b - 1, to = a; i >= to; i--)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SZ(v) (int)v.size()
#define pb push_back
typedef long long Long;
typedef pair<int, int> Pair;
const int MX = (1e6);
Long vis[MX + 2];
void update(Long x) {
  for (Long i = x; i <= MX; i += x) {
    vis[i]++;
  }
}
void solve() {
  int n;
  cin >> n;
  vector<int> v(n);
  fore(i, 0, n) cin >> v[i];
  fore(i, 0, n) update(v[i]);
  int ans = 0;
  fore(i, 0, n) {
    if (vis[v[i]] == 1) {
      ans++;
    }
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}

Fixed submission:
/* Author: $%U%$
 * Time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
 **/
#include <bits/stdc++.h>
using namespace std;
#define fore(i, a, b) for (int i = a, to = b; i < to; i++)
#define foref(i, a, b) for (int i = b - 1, to = a; i >= to; i--)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SZ(v) (int)v.size()
#define pb push_back
typedef long long Long;
typedef pair<int, int> Pair;
const int MX = (1e6);
Long vis[MX + 2];
void update(Long x) {
  if (vis[x] > 2)
    return;
  for (Long i = x; i <= MX; i += x) {
    vis[i]++;
  }
}
void solve() {
  int n;
  cin >> n;
  vector<int> v(n);
  fore(i, 0, n) cin >> v[i];
  fore(i, 0, n) update(v[i]);
  int ans = 0;
  fore(i, 0, n) {
    if (vis[v[i]] == 1) {
      ans++;
    }
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long int ll;
#define MAX_N 200001

bool divisible[MAX_N];
bool visited[MAX_N];
void init() {
  rep(i, MAX_N) {
    divisible[i] = false;
    visited[i] = false;
  }
}

int A[MAX_N];

int main() {
  init();
  int n;
  cin >> n;
  rep(i, n) { cin >> A[i]; }
  sort(A, A + n);
  for (int i = 0; i < n; i++) {
    if (divisible[A[i]] == false && visited[A[i]] == true) {
      divisible[A[i]] = true;
    } else if (divisible[A[i]] == false && visited[A[i]] == false) {
      visited[A[i]] = true;
      for (int j = 2; A[i] * j <= MAX_N; j++) {
        divisible[A[i] * j] = true;
      }
    }
  }
  int ans = 0;
  rep(i, n) {
    if (divisible[A[i]] == false)
      ans++;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long int ll;
#define MAX_N 1000001

bool divisible[MAX_N];
bool visited[MAX_N];
void init() {
  rep(i, MAX_N) {
    divisible[i] = false;
    visited[i] = false;
  }
}

int A[MAX_N];

int main() {
  init();
  int n;
  cin >> n;
  rep(i, n) { cin >> A[i]; }
  sort(A, A + n);
  for (int i = 0; i < n; i++) {
    if (divisible[A[i]] == false && visited[A[i]] == true) {
      divisible[A[i]] = true;
    } else if (divisible[A[i]] == false && visited[A[i]] == false) {
      visited[A[i]] = true;
      for (int j = 2; A[i] * j <= MAX_N; j++) {
        divisible[A[i] * j] = true;
      }
    }
  }
  int ans = 0;
  rep(i, n) {
    if (divisible[A[i]] == false)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {
  long long int n, x, i, j;
  cin >> n;
  map<long long int, long long int> mp;
  vector<bool> b(1000100, 0);
  for (i = 0; i < n; i++) {
    cin >> x;
    mp[x]++;
  }
  long long int cnt = n;
  for (auto it = mp.begin(); it != mp.end(); it++) {
    int p = it->first;
    if (mp.find(p) != mp.end() && mp[p] > 1 && b[j] == 0) {
      cnt = cnt - mp[p];
    }
    for (j = 2 * p; j <= 1000010; j += p) {
      if (mp.find(j) != mp.end() && mp[j] > 0 && b[j] == 0) {
        cnt = cnt - mp[j];
        b[j] = 1;
      }
    }
  }
  cout << cnt << "\n";
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {
  long long int n, x, i, j;
  cin >> n;
  map<long long int, long long int> mp;
  vector<bool> b(1000100, 0);
  for (i = 0; i < n; i++) {
    cin >> x;
    mp[x]++;
  }
  long long int cnt = n;
  for (auto it = mp.begin(); it != mp.end(); it++) {
    int p = it->first;
    if (mp.find(p) != mp.end() && mp[p] > 1 && b[p] == 0) {
      cnt = cnt - mp[p];
    }
    for (j = 2 * p; j <= 1000010; j += p) {
      if (mp.find(j) != mp.end() && mp[j] > 0 && b[j] == 0) {
        cnt = cnt - mp[j];
        b[j] = 1;
      }
    }
  }
  cout << cnt << "\n";
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr long long MOD = 1000000007;
constexpr long long INF = 1LL << 60;
const long double PI = acosl(-1.0);
constexpr long double EPS = 1e-11;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  map<ll, ll> mp;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  for (ll i = 0; i < n; i++)
    mp[a[i]]++;
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    bool flag = true;
    mp[a[i]]--;
    for (ll j = 1; j * j <= a[i]; j++) {
      if (a[i] % j == 0) {
        if (mp[j])
          flag = false;
        if (j * j != a[i] && mp[a[i] / j])
          flag = false;
      }
    }
    if (flag)
      ans++;
    mp[a[i]]++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr long long MOD = 1000000007;
constexpr long long INF = 1LL << 60;
const long double PI = acosl(-1.0);
constexpr long double EPS = 1e-11;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> mp(1e6 + 1);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  for (ll i = 0; i < n; i++)
    mp[a[i]]++;
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    bool flag = true;
    mp[a[i]]--;
    for (ll j = 1; j * j <= a[i]; j++) {
      if (a[i] % j == 0) {
        if (mp[j])
          flag = false;
        if (j * j != a[i] && mp[a[i] / j])
          flag = false;
      }
    }
    if (flag)
      ans++;
    mp[a[i]]++;
  }
  cout << ans << endl;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int counter[1000006];
bool divided[1000005];
int main() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  sort(arr.begin(), arr.end());
  for (int i = 0; i < n; i++) {
    counter[arr[i]]++;
    if (!divided[arr[i]]) {
      for (int j = 2 * arr[i]; j <= 1000000; j += arr[i])
        divided[j] = true;
    }
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] == 1) {
      if (counter[1] == 1)
        ret++;
    } else {
      if (counter[1])
        continue;
      if (divided[arr[i]] || counter[arr[i]] > 1)
        continue;
      ret++;
    }
  }
  cout << ret;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int counter[1000006];
bool divided[1000005];
int main() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  sort(arr.begin(), arr.end());
  for (int i = 0; i < n; i++) {
    counter[arr[i]]++;
    if (!divided[arr[i]] && counter[arr[i]] == 1) {
      for (int j = 2 * arr[i]; j <= 1000000; j += arr[i])
        divided[j] = true;
    }
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] == 1) {
      if (counter[1] == 1)
        ret++;
    } else {
      if (counter[1])
        continue;
      if (divided[arr[i]] || counter[arr[i]] > 1)
        continue;
      ret++;
    }
  }
  cout << ret;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int MAXA = 1.01e6;
const int MAXN = 1.01e5;
int ar[MAXN], c[MAXA];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  memset(c, 0, sizeof c);

  for (int i = 0; i < n; ++i) {
    cin >> ar[i];
    c[ar[i]]++;
  }

  int res = 0;
  for (int i = 0; i < n; ++i) {
    c[ar[i]]--;
    bool ok = true;
    for (int x = 1; x <= ar[i] / x; ++x) {
      if (ar[i] % x)
        continue;
      if (c[x])
        ok = false;
      if (ar[i] / x != x && c[ar[i] / x])
        ok = false;
    }
    res += int(ok);
    c[ar[i]]++;
  }

  cout << res << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int MAXA = 1.01e6;
const int MAXN = 2.01e5;
int ar[MAXN], c[MAXA];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  memset(c, 0, sizeof c);

  for (int i = 0; i < n; ++i) {
    cin >> ar[i];
    c[ar[i]]++;
  }

  int res = 0;
  for (int i = 0; i < n; ++i) {
    c[ar[i]]--;
    bool ok = true;
    for (int x = 1; x <= ar[i] / x; ++x) {
      if (ar[i] % x)
        continue;
      if (c[x])
        ok = false;
      if (ar[i] / x != x && c[ar[i] / x])
        ok = false;
    }
    res += int(ok);
    c[ar[i]]++;
  }

  cout << res << '\n';

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fill fill_n
#define pf push_front
#define pb push_back
#define FOR(i, l, r) for (int i = l; i < r; i++)
#define ROF(i, r, l) for (int i = r; i >= l; i--)
#define comp(v) (v).begin(), (v).end()
#define ff first
#define ss second
#define infile                                                                 \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
typedef vector<int> vi;
typedef vector<ll> vli;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvli;
const int M = 1e9 + 7;
const int N = 1e6;
ll po(int, int);
bool dp[N + 5] = {0};
void solve() {
  int n;
  cin >> n;
  int a[n];
  map<int, int> m;
  FOR(i, 0, n) {
    cin >> a[i];
    m[a[i]]++;
  }
  sort(a, a + n);
  FOR(i, 0, n) {
    for (int j = (m[a[i]] == 1) ? 2 * a[i] : a[i]; j <= N; j += a[i]) {
      dp[j] = 1;
    }
  }
  int ans = 0;
  FOR(i, 0, n) {
    if (!dp[a[i]])
      ans++;
  }
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}

ll po(int a, int b) {
  if (b == 0)
    return 1;
  ll ans = 1;
  if (b % 2 == 0) {
    ans = po(a, b / 2) % M;
    ans = (ans % M * ans % M) % M;
  } else {
    ans = po(a, (b - 1) / 2) % M;
    ans = (ans % M * ans % M * a) % M;
  }
  return ans % M;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fill fill_n
#define pf push_front
#define pb push_back
#define FOR(i, l, r) for (int i = l; i < r; i++)
#define ROF(i, r, l) for (int i = r; i >= l; i--)
#define comp(v) (v).begin(), (v).end()
#define ff first
#define ss second
#define infile                                                                 \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
typedef vector<int> vi;
typedef vector<ll> vli;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvli;
const int M = 1e9 + 7;
const int N = 1e6;
ll po(int, int);
bool dp[N + 5] = {0};
void solve() {
  int n;
  cin >> n;
  int a[n];
  map<int, int> m;
  FOR(i, 0, n) {
    cin >> a[i];
    m[a[i]]++;
  }
  sort(a, a + n);
  FOR(i, 0, n) {
    for (int j = (m[a[i]] == 1) ? 2 * a[i] : a[i]; j <= a[n - 1]; j += a[i]) {
      dp[j] = 1;
    }
  }
  int ans = 0;
  FOR(i, 0, n) {
    if (!dp[a[i]])
      ans++;
  }
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}

ll po(int a, int b) {
  if (b == 0)
    return 1;
  ll ans = 1;
  if (b % 2 == 0) {
    ans = po(a, b / 2) % M;
    ans = (ans % M * ans % M) % M;
  } else {
    ans = po(a, (b - 1) / 2) % M;
    ans = (ans % M * ans % M * a) % M;
  }
  return ans % M;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define INF 1000000000
using namespace std;

int main() {
  int n;
  cin >> n;
  map<int, int> m;
  vector<int> a(n);
  int flag[1000010] = {0};
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
  }

  sort(a.begin(), a.end());
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (flag[a[i]] == 0 && m[a[i]] == 1)
      sum++;
    for (int j = 1; j * a[i] < 1000010; j++) {
      flag[j * a[i]] = 1;
    }
  }

  cout << sum << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define INF 1000000000
using namespace std;

int main() {
  int n;
  cin >> n;
  map<int, int> m;
  vector<int> a(n);
  int flag[1000010] = {0};
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
  }

  sort(a.begin(), a.end());
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (flag[a[i]] == 0 && m[a[i]] == 1)
      sum++;
    if (flag[a[i]] == 0) {
      for (int j = 1; j * a[i] < 1000010; j++) {
        flag[j * a[i]] = 1;
      }
    }
  }

  cout << sum << endl;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());

  int M = a[n - 1];
  int d[20000] = {0};

  for (int i = 0; i < n; i++) {
    for (int j = 1; j * a[i] <= M; j++) {
      d[a[i] * j]++;
    }
  }

  /*
  for(int i = 1;i <= M;i++){
      cout << d[i] << " ";
  }

  cout << endl;
  */

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (d[a[i]] == 1) {
      ans += 1;
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());

  int M = a[n - 1];
  int d[1000001] = {0};

  for (int i = 0; i < n; i++) {
    for (int j = 1; j * a[i] <= M; j++) {
      d[a[i] * j]++;
    }
  }

  /*
  for(int i = 1;i <= M;i++){
      cout << d[i] << " ";
  }

  cout << endl;
  */

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (d[a[i]] == 1) {
      ans += 1;
    }
  }

  cout << ans << endl;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java,
PHP, Ruby, Perl, C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly,
HTML, CSS, JS, SQLite, Prolog. Code, Compile, Run and Debug online from anywhere
in world.

*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e6 + 5;
const int maxn2 = 1e5 + 5;
int a[maxn2], b[maxn], vis[maxn];

int main() {
  int n;
  cin >> n;
  memset(b, 0, sizeof b);
  memset(vis, 0, sizeof vis);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    if (vis[a[i]]) {
      b[a[i]] = 1;
      continue;
    }
    for (int j = 2; j * a[i] <= 1000000; j++) {
      b[j * a[i]] = 1;
    }
    vis[a[i]] = 1;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += b[a[i]] == 0;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java,
PHP, Ruby, Perl, C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly,
HTML, CSS, JS, SQLite, Prolog. Code, Compile, Run and Debug online from anywhere
in world.

*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e6 + 5;
const int maxn2 = 1e5 + 5;
int a[maxn], b[maxn], vis[maxn];

int main() {
  int n;
  cin >> n;
  memset(b, 0, sizeof b);
  memset(vis, 0, sizeof vis);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    if (vis[a[i]]) {
      b[a[i]] = 1;
      continue;
    }
    for (int j = 2; j * a[i] <= 1000000; j++) {
      b[j * a[i]] = 1;
    }
    vis[a[i]] = 1;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += b[a[i]] == 0;
  }
  cout << ans << endl;
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = a; i > b; i--)
#define hahashad ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
typedef long long ll;
#define VL vector<ll>
#define IN(inp)                                                                \
  ll inp;                                                                      \
  cin >> inp;
#define pb push_back
#define what(A) cout << #A << " is " << A << endl;
ll isprime[1000005];
ll ans;
int divisors() {
  FOR(i, 1, 1e6 + 1) {
    if (isprime[i] == 0)
      continue;
    if (isprime[i] == 1) {
      ans++;
    }
    for (ll j = 2 * i; j <= 1e6; j += i) {
      isprime[j] = 0;
    }
  }
}

int main() {
  ll n;
  cin >> n;
  FOR(i, 0, n) {
    IN(inp)
    isprime[inp]++;
  }
  divisors();
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = a; i > b; i--)
#define hahashad ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
typedef long long ll;
#define VL vector<ll>
#define IN(inp)                                                                \
  ll inp;                                                                      \
  cin >> inp;
#define pb push_back
#define what(A) cout << #A << " is " << A << endl;
ll isprime[1000005];
ll ans;
void divisors() {
  FOR(i, 1, 1e6 + 1) {
    if (isprime[i] == 0)
      continue;
    if (isprime[i] == 1) {
      ans++;
    }
    for (ll j = 2 * i; j <= 1e6; j += i) {
      isprime[j] = 0;
    }
  }
}

int main() {
  ll n;
  cin >> n;
  FOR(i, 0, n) {
    IN(inp)
    isprime[inp]++;
  }
  divisors();
  cout << ans;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define eb emplace_back
const int MAX = 1000000;
ll m[MAX + 10];

int main() {
  ll n;

  cin >> n;

  ll a[n + 2];

  for (ll i = 0; i < n; i++)
    cin >> a[i];

  sort(a, a + n);

  for (ll i = 0; i < n; i++) {
    for (ll j = a[i]; j <= MAX; j += a[i]) {
      m[j]++;
    }
  }

  ll cnt = 0;

  for (ll i = 0; i < n; i++) {
    if (m[a[i]] == 1)
      cnt++;
  }

  cout << cnt << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define eb emplace_back
const int MAX = 1000000;
ll m[MAX + 10];

int main() {
  ll n;

  cin >> n;

  ll a[n + 2];

  for (ll i = 0; i < n; i++)
    cin >> a[i];

  sort(a, a + n);

  for (ll i = 0; i < n; i++) {
    if (i > 0 && a[i] == a[i - 1]) {
      m[a[i]]++;
      continue;
    }

    for (ll j = a[i]; j <= MAX; j += a[i]) {
      m[j]++;
    }
  }

  ll cnt = 0;

  for (ll i = 0; i < n; i++) {
    if (m[a[i]] == 1)
      cnt++;
  }

  cout << cnt << endl;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define size_of_array(array) (sizeof(array) / sizeof(array[0]))
#define DEBUG 0
#define LIMIT (LONG_MAX / 10)
#define POSITIVE_LAST_D (LONG_MAX % 10)
using ll = long long;
using namespace std;
using Graph = vector<vector<int>>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(1000005);
  rep(i, n) cin >> a[i];
  rep(i, n) {
    ll k = 1;
    ll j = k * a[i];
    while (j <= 1000000) {
      ++b[j];
      ++k;
      j = k * a[i];
    }
  }
  ll ans = 0;
  rep(i, n) {
    if (b[a[i]] == 1)
      ans++;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define size_of_array(array) (sizeof(array) / sizeof(array[0]))
#define DEBUG 0
#define LIMIT (LONG_MAX / 10)
#define POSITIVE_LAST_D (LONG_MAX % 10)
using ll = long long;
using namespace std;
using Graph = vector<vector<int>>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(1000005);
  rep(i, n) cin >> a[i];
  rep(i, n) {
    ll k = 1;
    ll j = k * a[i];
    if (b[j] != 0) {
      b[j] = 2;
      continue;
    }
    while (j <= 1000000) {
      ++b[j];
      ++k;
      j = k * a[i];
    }
  }
  ll ans = 0;
  rep(i, n) {
    if (b[a[i]] == 1)
      ans++;
  }
  cout << ans << endl;

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
using ll = long long;

vector<ll> printDivisors(ll n) {
  vector<ll> k;
  // Note that this loop runs till square root
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      // If divisors are equal, print only one
      if (n / i == i)
        k.push_back(i);

      else // Otherwise print both
      {
        k.push_back(i);
        k.push_back(n / i);
      }
    }
  }
  return k;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a;
  map<ll, ll> m;
  for (ll i = 0; i < n; i++) {
    ll temp;
    cin >> temp;
    a.push_back(temp);

    if (!m.count(temp))
      m[temp] = 1;
    else
      m[temp]++;
  }
  ll cnt = n;
  for (auto x : m) {
    ll p = x.first;
    ll q = x.second;
    if (q > 1)
      cnt -= q;

    else {
      vector<ll> k = printDivisors(p);
      for (auto y : k) {
        if (m.count(y) && y != p) {
          cnt--;
          break;
        }
      }
    }
  }

  cout << cnt;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
using ll = long long;

vector<ll> printDivisors(ll n) {
  vector<ll> k;
  // Note that this loop runs till square root
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      // If divisors are equal, print only one
      if (n / i == i)
        k.push_back(i);

      else // Otherwise print both
      {
        k.push_back(i);
        k.push_back(n / i);
      }
    }
  }
  return k;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a;
  map<ll, ll> m;
  for (ll i = 0; i < n; i++) {
    ll temp;
    cin >> temp;
    a.push_back(temp);

    if (!m.count(temp))
      m[temp] = 1;
    else
      m[temp]++;
  }
  ll cnt = n;
  for (auto x : m) {
    ll p = x.first;
    ll q = x.second;
    if (q > 1)
      cnt -= q;

    else if (q == 1) {
      vector<ll> k = printDivisors(p);
      for (auto y : k) {
        if (m.count(y) && y != p) {
          cnt--;
          break;
        }
      }
    }
  }

  cout << cnt;
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;

int main() {
  ll n;
  cin >> n;
  vl a(n, 0);
  rep(i, n) { cin >> a[i]; }
  sort(a.begin(), a.end());
  ll amax = a[n - 1];
  vector<ll> dp(amax, 0);
  rep(i, n) {
    if (dp[a[i]] != 0) {
      dp[a[i]] = 2;
      continue;
    }
    for (ll j = 1; a[i] * j <= amax; j++) {
      dp[a[i] * j]++;
    }
  }
  ll ans = 0;
  rep(i, n) {
    if (dp[a[i]] == 1) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;

int main() {
  ll n;
  cin >> n;
  vl a(n, 0);
  rep(i, n) { cin >> a[i]; }
  sort(a.begin(), a.end());
  ll amax = a[n - 1] + 1;
  vector<ll> dp(amax, 0);
  rep(i, n) {
    if (dp[a[i]] != 0) {
      dp[a[i]] = 2;
      continue;
    }
    for (ll j = 1; a[i] * j <= amax; j++) {
      dp[a[i] * j]++;
    }
  }
  ll ans = 0;
  rep(i, n) {
    if (dp[a[i]] == 1) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
// mt19937 mrand(random_device{}());
const ll mod = 1000000007;
// int rnd(int x) { return mrand() % x;}
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head
const int maxx = 1e6 + 10;
int num[maxx];
int fl[maxx];
int main() {
  int n;
  scanf("%d", &n);
  rep(i, 0, n) { scanf("%d", &num[i]); }
  sort(num, num + n);
  int cnt = 0;
  memset(fl, 1, sizeof(fl));
  rep(i, 0, n) {
    if (fl[num[i]]) {
      if (num[i] != num[i + 1])
        cnt++;
    }
    for (int j = 1; j * num[i] < maxx; j++) {
      fl[j * num[i]] = 0;
    }
  }

  cout << cnt;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
// mt19937 mrand(random_device{}());
const ll mod = 1000000007;
// int rnd(int x) { return mrand() % x;}
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head
const int maxx = 1e6 + 10;
int num[maxx];
int fl[maxx];
int main() {
  int n;
  scanf("%d", &n);
  rep(i, 0, n) { scanf("%d", &num[i]); }
  sort(num, num + n);
  int cnt = 0;
  memset(fl, 1, sizeof(fl));
  rep(i, 0, n) {
    if (fl[num[i]]) {
      if (num[i] != num[i + 1])
        cnt++;
      for (int j = 1; j * num[i] < maxx; j++) {
        fl[j * num[i]] = 0;
      }
    }
  }

  cout << cnt;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

// Some constants don't work
#ifndef INT_MAX
#define INT_MAX 2147483647
#endif
#ifndef INT_MIN
#define INT_MIN -2147483648
#endif
// short hand tricks
typedef long long ll;

using namespace std;

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> v(N);
  // keep track of all numbers
  vector<int> freq(1e5 + 1, 0);
  for (int i = 0; i < N; i++) {
    cin >> v[i];
    freq[v[i]]++;
  }
  int res = N;
  // see if number has any divisor that's another number excluding itself
  for (int i = 0; i < N; i++) {
    // check every divisor
    int num = v[i];
    freq[num]--;
    // O(sqrt(n))
    for (int i = 1; i * i <= num; i++) {
      if (num % i == 0) {
        if (freq[i] > 0 || freq[num / i] > 0) {
          // divisor is another number
          res--;
          break;
        }
      }
    }
    freq[num]++;
  }
  cout << res << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

// Some constants don't work
#ifndef INT_MAX
#define INT_MAX 2147483647
#endif
#ifndef INT_MIN
#define INT_MIN -2147483648
#endif
// short hand tricks
typedef long long ll;

using namespace std;

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> v(N);
  // keep track of all numbers
  vector<int> freq(1e6 + 1, 0);
  for (int i = 0; i < N; i++) {
    cin >> v[i];
    freq[v[i]]++;
  }
  int res = N;
  // see if number has any divisor that's another number excluding itself
  for (int i = 0; i < N; i++) {
    // check every divisor
    int num = v[i];
    freq[num]--;
    // O(sqrt(n))
    for (int i = 1; i * i <= num; i++) {
      if (num % i == 0) {
        if (freq[i] > 0 || freq[num / i] > 0) {
          // divisor is another number
          res--;
          break;
        }
      }
    }
    freq[num]++;
  }
  cout << res << "\n";

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef int64_t Int;
#define all(x) (x).begin(), (x).end()

const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9 + 7;

bool print_space_enable = false;
void print() {
  cout << endl;
  print_space_enable = false;
}

template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  if (print_space_enable)
    cout << " ";
  cout << fixed << setprecision(15) << head;
  print_space_enable = true;
  print(std::forward<Tail>(tail)...);
}

template <typename T> void print(vector<T> v) {
  for (size_t i = 0; i < v.size(); i++) {
    if (i > 0)
      std::cout << " ";
    std::cout << v[i];
  }
  std::cout << std::endl;
}

int main() {
  Int n;
  cin >> n;
  vector<Int> a(n);
  for (Int i = 0; i < (Int)n; i++) {
    cin >> a[i];
  }
  sort(all(a));
  vector<bool> b(1000001, true);
  for (Int i = 0; i < (Int)n; i++) {
    if (b[a[i]]) {
      for (Int j = 2; a[i] * j < (Int)1000001; j++) {
        b[a[i] * j] = false;
      }
    }
  }
  Int res = 0;
  for (Int i = 0; i < (Int)n; i++) {
    if (0 < i and a[i - 1] == a[i])
      continue;
    if (i < n - 1 and a[i] == a[i + 1])
      continue;
    if (b[a[i]]) {
      res++;
    }
  }
  print(res);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef int64_t Int;
#define all(x) (x).begin(), (x).end()

const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9 + 7;

bool print_space_enable = false;
void print() {
  cout << endl;
  print_space_enable = false;
}

template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  if (print_space_enable)
    cout << " ";
  cout << fixed << setprecision(15) << head;
  print_space_enable = true;
  print(std::forward<Tail>(tail)...);
}

template <typename T> void print(vector<T> v) {
  for (size_t i = 0; i < v.size(); i++) {
    if (i > 0)
      std::cout << " ";
    std::cout << v[i];
  }
  std::cout << std::endl;
}

int main() {
  Int n;
  cin >> n;
  vector<Int> a(n);
  for (Int i = 0; i < (Int)n; i++) {
    cin >> a[i];
  }
  sort(all(a));
  vector<bool> b(1000001, true);
  for (Int i = 0; i < (Int)n; i++) {
    if (0 < i and a[i - 1] == a[i])
      continue;
    if (b[a[i]]) {
      for (Int j = 2; a[i] * j < (Int)1000001; j++) {
        b[a[i] * j] = false;
      }
    }
  }
  Int res = 0;
  for (Int i = 0; i < (Int)n; i++) {
    if (0 < i and a[i - 1] == a[i])
      continue;
    if (i < n - 1 and a[i] == a[i + 1])
      continue;
    if (b[a[i]]) {
      res++;
    }
  }
  print(res);
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

// #define int long long
// signed main(){
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> a(n);
  int upb = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    upb = max(upb, a[i]);
  }
  sort(a.begin(), a.end());
  vector<int> chk(upb, 0);
  for (int i = 0; i < n; i++) {
    if (a[i] == a[i + 1] && i < n - 1)
      chk[a[i]]++;
    int cg = 2 * a[i];
    while (cg <= upb) {
      chk[cg]++;
      cg += a[i];
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    //        cout << a[i] << " " << chk[a[i]] << "\n";
    if (chk[a[i]] == 0)
      cnt++;
  }
  cout << cnt << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

// #define int long long
// signed main(){
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> a(n);
  int upb = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    upb = max(upb, a[i]);
  }
  sort(a.begin(), a.end());
  vector<int> chk(upb + 5, 0);
  for (int i = 0; i < n; i++) {
    if (a[i] == a[i + 1] && i < n - 1)
      chk[a[i]]++;
    int cg = 2 * a[i];
    while (cg <= upb) {
      chk[cg]++;
      cg += a[i];
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    //        cout << a[i] << " " << chk[a[i]] << "\n";
    if (chk[a[i]] == 0)
      cnt++;
  }
  cout << cnt << "\n";
  return 0;
}