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

Buggy submission (status: Time Limit Exceeded):
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
using ll = long long;
using P = pair<ll, ll>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<int> isprime(1000001, 1);
  map<ll, ll> mp;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
  }
  for (ll i = 2; i <= 1000000; i++) {
    if (!isprime[i])
      continue;
    for (ll j = 2 * i; j <= 1000000; j += i) {
      isprime[j] = 0;
    }
    for (ll j = 1; i * j <= 1000000; j++) {
      mp[i * j] += mp[j];
    }
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    if (mp[a[i]] == 1)
      ans++;
  }
  cout << ans << endl;
}

Fixed submission:
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
using ll = long long;
using P = pair<ll, ll>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<int> isprime(1000001, 1);
  vector<ll> mp(1000001);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
  }
  for (ll i = 2; i <= 1000000; i++) {
    if (!isprime[i])
      continue;
    for (ll j = 2 * i; j <= 1000000; j += i) {
      isprime[j] = 0;
    }
    for (ll j = 1; i * j <= 1000000; j++) {
      mp[i * j] += mp[j];
    }
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    if (mp[a[i]] == 1)
      ans++;
  }
  cout << ans << endl;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define se second
#define fi first
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define plll pair<pll, ll>
#define ppll pair<pll, pll>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pb push_back
const ll llinf = 2e18 + 7;
const int inf = 2e9 + 7;
const ll mod = 1e9 + 7;
const ll N = 1e6 + 7;
const ld pi = 3.14159265358979;

void solve() {
  int n, a;
  cin >> n;
  vector<int> isprime(N, 1);
  vector<int> prime(N), arr;
  for (int i = 0; i < n; i++) {
    cin >> a;
    prime[a]++;
  }
  for (int i = 0; i < N; i++) {
    if (prime[i] >= 1)
      arr.pb(i);
  }
  // sort(arr.begin(),arr.end());
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (isprime[arr[i]]) {
      if (prime[arr[i]] == 1)
        cnt++;
      for (int j = 2 * arr[i]; j < N; j += arr[i]) {
        isprime[j] = 0;
      }
    }
  }
  cout << cnt;
}

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
    // cout<<"\n";
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define se second
#define fi first
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define plll pair<pll, ll>
#define ppll pair<pll, pll>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pb push_back
const ll llinf = 2e18 + 7;
const int inf = 2e9 + 7;
const ll mod = 1e9 + 7;
const ll N = 1e6 + 7;
const ld pi = 3.14159265358979;

void solve() {
  int n, a;
  cin >> n;
  vector<int> isprime(N, 1);
  vector<int> prime(N), arr;
  for (int i = 0; i < n; i++) {
    cin >> a;
    prime[a]++;
  }
  for (int i = 0; i < N; i++) {
    if (prime[i] >= 1)
      arr.pb(i);
  }
  // sort(arr.begin(),arr.end());
  int cnt = 0;
  for (int i = 0; i < arr.size(); i++) {
    if (isprime[arr[i]]) {
      if (prime[arr[i]] == 1)
        cnt++;
      for (int j = 2 * arr[i]; j < N; j += arr[i]) {
        isprime[j] = 0;
      }
    }
  }
  cout << cnt;
}

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
    // cout<<"\n";
  }
  return 0;
}
Problem p02642 (C++)

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repa(i, a, n) for (int i = (a); i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrepa(i, a, n) for (int i = n - 1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define vc vector

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

int main() {

  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];

  a.push_back(0);
  sort(all(a));
  a.push_back(0);

  vc<bool> tf(a[n - 1] + 1, true);
  int ans = 0;

  repa(i, 1, n + 1) {
    while (a[i] == a[i + 1])
      i++;
    if (tf[a[i]] && a[i - 1] != a[i])
      ans++;
    for (int j = 1; a[i] * j <= a[n]; j++) {
      tf[a[i] * j] = false;
    }
  }

  cout << ans << "\n";

  return 0;
}

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repa(i, a, n) for (int i = (a); i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrepa(i, a, n) for (int i = n - 1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define vc vector

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

int main() {

  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];

  a.push_back(0);
  sort(all(a));
  a.push_back(0);

  vc<bool> tf(a[n] + 1, true);
  int ans = 0;

  repa(i, 1, n + 1) {
    while (a[i] == a[i + 1])
      i++;
    if (tf[a[i]] && a[i - 1] != a[i])
      ans++;
    for (int j = 1; a[i] * j <= a[n]; j++) {
      tf[a[i] * j] = false;
    }
  }

  cout << ans << "\n";

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x)                                                            \
  for (auto elementPrintVec : x) {                                             \
    cout << elementPrintVec << " ";                                            \
  }                                                                            \
  cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<bool> d(1000005);
  vector<bool> visited(1000005);
  rep(i, n) { cin >> a[i]; }
  sort(a.begin(), a.end());
  rep(i, n) {
    int x = a[i];
    if (visited[x])
      continue;
    visited[x] = true;
    while (x <= 1000000) {
      x += a[i];
      d[x] = true;
    }
  }
  int ans = 0;
  rep(i, n) {
    if (!d[a[i]]) {
      if (i != 0 && a[i] == a[i - 1])
        continue;
      if (i != n - 1 && a[i + 1] == a[i])
        continue;
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x)                                                            \
  for (auto elementPrintVec : x) {                                             \
    cout << elementPrintVec << " ";                                            \
  }                                                                            \
  cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<bool> d(1000005);
  vector<bool> visited(1000005);
  rep(i, n) { cin >> a[i]; }
  sort(a.begin(), a.end());
  rep(i, n) {
    int x = a[i];
    if (visited[x])
      continue;
    visited[x] = true;
    while (x + a[i] <= 1000000) {
      x += a[i];
      d[x] = true;
    }
  }
  int ans = 0;
  rep(i, n) {
    if (!d[a[i]]) {
      if (i != 0 && a[i] == a[i - 1])
        continue;
      if (i != n - 1 && a[i + 1] == a[i])
        continue;
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02642 (C++)

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

int main() {
  int n;
  cin >> n;
  int arr[n];
  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    mp[arr[i]]++;
  }
  sort(arr, arr + n);
  int maxi = mp.rbegin()->first;
  // cout<<maxi<<endl;
  for (int i = 0; i < n; i++) {
    int val = arr[i];
    if (mp.find(val) == mp.end())
      continue;
    if (mp[val] > 1)
      mp.erase(val);
    maxi = mp.rbegin()->first;
    // cout<<val<<" "<<maxi<<" "<<mp.size()<<endl;
    for (int j = 2 * val; j <= maxi; j += val) {
      if (mp.find(j) != mp.end())
        mp.erase(j);
    }
  }
  cout << mp.size() << endl;
  return 0;
}


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

int main() {
  int n;
  cin >> n;
  int arr[n];
  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    mp[arr[i]]++;
  }
  sort(arr, arr + n);
  int maxi = mp.rbegin()->first;
  // cout<<maxi<<endl;
  for (int i = 0; i < n; i++) {
    int val = arr[i];
    if (mp.find(val) == mp.end())
      continue;
    if (mp[val] > 1)
      mp.erase(val);
    if (mp.size())
      maxi = mp.rbegin()->first;
    // cout<<val<<" "<<maxi<<" "<<mp.size()<<endl;
    for (int j = 2 * val; j <= maxi; j += val) {
      if (mp.find(j) != mp.end())
        mp.erase(j);
    }
  }
  cout << mp.size() << endl;
  return 0;
}
Problem p02642 (C++)

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

typedef long long ll;

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n = 0;
  cin >> n;
  vector<int> v(n, 0);
  vector<int> cnt(1e6 + 5, 0);
  int mx = -1;

  for (int i = 0; i < n; i++) {
    cin >> v[i];
    mx = max(mx, v[i]);
    cnt[v[i]]++;
  }

  vector<int> dp(mx + 1, 0);

  int ans = 0;
  for (int i = 0; i < n; i++) {
    ll temp = v[i];
    while (temp <= mx) {
      temp = temp + v[i];
      dp[temp] = 1;
    }
  }

  for (int i = 0; i < n; i++) {
    if (dp[v[i]] == 0 && cnt[v[i]] == 1)
      ans++;
  }

  cout << ans << endl;

  return 0;
}

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

typedef long long ll;

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n = 0;
  cin >> n;
  vector<int> v(n, 0);
  vector<int> cnt(1e6 + 5, 0);
  int mx = -1;

  for (int i = 0; i < n; i++) {
    cin >> v[i];
    mx = max(mx, v[i]);
    cnt[v[i]]++;
  }

  vector<int> dp(mx + 1, 0);

  int ans = 0;
  for (int i = 0; i < n; i++) {
    ll temp = v[i];
    while (temp + v[i] <= mx) {
      temp = temp + v[i];
      dp[temp] = 1;
    }
  }

  for (int i = 0; i < n; i++) {
    if (dp[v[i]] == 0 && cnt[v[i]] == 1)
      ans++;
  }

  cout << ans << endl;

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1 + (int)2e5;

int main() {
  int N;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  sort(A, A + N);
  int ans = 0;

  int AA[NMAX] = {};

  for (int i = 0; i < N; i++) {
    bool flag = true;
    if (i > 0) {
      AA[A[i - 1]]++;
    }
    if (A[i] == A[i + 1])
      continue;
    int k = (int)sqrt((double)A[i]);
    for (int j = 1; j <= k; j++) {
      if (AA[j] > 0 && A[i] % j == 0) {
        flag = false;
        // cout << "A[i] " << A[i] << " \% " << j << " =0: not counted" << endl;
      }
      if (A[i] % j == 0 && AA[A[i] / j] > 0) {
        flag = false;
        // cout << "A[i] " << A[i] << " \% " << A[i]/j << " =0: not counted" <<
        // endl;
      }
    }
    if (flag) {
      // cout << "ans A[i] " << ans << " " << A[i] << endl;
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1 + (int)1e6;

int main() {
  int N;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  sort(A, A + N);
  int ans = 0;

  int AA[NMAX] = {};

  for (int i = 0; i < N; i++) {
    bool flag = true;
    if (i > 0) {
      AA[A[i - 1]]++;
    }
    if (A[i] == A[i + 1])
      continue;
    int k = (int)sqrt((double)A[i]);
    for (int j = 1; j <= k; j++) {
      if (AA[j] > 0 && A[i] % j == 0) {
        flag = false;
        // cout << "A[i] " << A[i] << " \% " << j << " =0: not counted" << endl;
      }
      if (A[i] % j == 0 && AA[A[i] / j] > 0) {
        flag = false;
        // cout << "A[i] " << A[i] << " \% " << A[i]/j << " =0: not counted" <<
        // endl;
      }
    }
    if (flag) {
      // cout << "ans A[i] " << ans << " " << A[i] << endl;
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02642 (C++)

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

using namespace std;

using llong = long long int;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()

const static int MOD = 1000000007;
const static int INF = 1 << 30;
const static int dx[4] = {1, 0, -1, 0};
const static int dy[4] = {0, 1, 0, -1};

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> A(n);
  map<int, int> M;
  rep(i, n) {
    cin >> A[i];
    ++M[A[i]];
  }

  sort(all(A));

  int maxA = 1000000 + 1;
  vector<bool> check_table(maxA + 1, false);
  int k;
  rep(i, n) {
    k = 2;
    while (A[i] * k <= maxA) {
      check_table[A[i] * k] = true;
      ++k;
    }
  }

  A.erase(unique(all(A)), A.end());
  int ans = 0;
  rep(i, (int)A.size()) {
    if (!check_table[A[i]] && M[A[i]] == 1)
      ++ans;
  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;

using llong = long long int;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()

const static int MOD = 1000000007;
const static int INF = 1 << 30;
const static int dx[4] = {1, 0, -1, 0};
const static int dy[4] = {0, 1, 0, -1};

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> A(n);
  map<int, int> M;
  rep(i, n) {
    cin >> A[i];
    ++M[A[i]];
  }

  sort(all(A));

  int maxA = A[n - 1] + 100;
  vector<bool> check_table(maxA + 1, false);
  int k;
  rep(i, n) {
    k = 2;
    while (A[i] * k <= maxA) {
      check_table[A[i] * k] = true;
      ++k;
    }
  }

  A.erase(unique(all(A)), A.end());
  int ans = 0;
  rep(i, (int)A.size()) {
    if (!check_table[A[i]] && M[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;

const int maxn = 1e6 + 5;

int f[maxn];
int nope[maxn];
int a[maxn];

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 1; i <= n; i++)
    scanf("%d", a + i);

  for (int i = 1; i <= n; i++) {
    int k = a[i];
    f[k]++;
    for (int j = k + k; j < maxn; j += k)
      nope[j] = 1;
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (f[a[i]] == 1) and !nope[a[i]];
  }
  printf("%d\n", ans);
}

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

const int maxn = 1e6 + 5;

int f[maxn];
int nope[maxn];
int a[maxn];

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 1; i <= n; i++)
    scanf("%d", a + i);

  for (int i = 1; i <= n; i++) {
    int k = a[i];
    f[k]++;
    if (f[k] == 1)
      for (int j = k + k; j < maxn; j += k)
        nope[j] = 1;
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (f[a[i]] == 1) and !nope[a[i]];
  }
  printf("%d\n", ans);
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
const long long INF = 1LL << 60;

vector<ll> load[100005];

typedef pair<ll, ll> Pair;

int main() {

  ll N;
  vector<ll> furui(1e6);

  cin >> N;

  vector<ll> A(N);

  for (ll i = 0; i < N; ++i)
    cin >> A[i];

  for (ll i : A) {
    if (furui[i] == 1) {
      furui[i]++;
      continue;
    }
    for (ll j = i; j <= furui.size(); j += i)
      furui[j]++;
  }

  ll count = 0;

  for (ll i = 0; i < A.size(); ++i) {

    if (furui[A[i]] == 1)
      count++;
  }

  cout << count << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
const long long INF = 1LL << 60;

vector<ll> load[100005];

typedef pair<ll, ll> Pair;

int main() {

  ll N;
  vector<ll> furui(1e6);

  cin >> N;

  vector<ll> A(N);

  for (ll i = 0; i < N; ++i)
    cin >> A[i];

  for (ll i : A) {
    if (furui[i] >= 1) {
      furui[i]++;
      continue;
    }
    for (ll j = i; j <= furui.size(); j += i)
      furui[j]++;
  }

  ll count = 0;

  for (ll i = 0; i < A.size(); ++i) {

    if (furui[A[i]] == 1)
      count++;
  }

  cout << count << endl;

  return 0;
}
Problem p02642 (C++)

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

vector<int64_t> divisor(int64_t n) {
  vector<int64_t> ret;
  ret.push_back(1);
  for (int64_t i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

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

  int n;
  cin >> n;
  map<int, int> s;
  const int n_max = 1e6 + 10;
  vector<int> aru(n_max, 0);
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    aru[a[i]]++;
  }
  sort(a.begin(), a.end());
  int ans = 0;
  rep(i, n) {
    if (aru[a[i]] >= 10000000 || aru[a[i]] < 1)
      continue;
    else {
      if (aru[a[i]] == 1)
        ans++;
      for (int j = a[i] * 2; j < n_max; j += a[i]) {
        aru[j] = 1e7;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

vector<int64_t> divisor(int64_t n) {
  vector<int64_t> ret;
  ret.push_back(1);
  for (int64_t i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

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

  int n;
  cin >> n;
  map<int, int> s;
  const int n_max = 1e6 + 10;
  vector<int> aru(n_max, 0);
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    aru[a[i]]++;
  }
  sort(a.begin(), a.end());
  int ans = 0;
  if (a[0] == 1) {
    if (aru[a[0]] > 1)
      cout << 0 << endl;
    else
      cout << 1 << endl;
    return 0;
  }
  rep(i, n) {
    if (aru[a[i]] >= 10000000 || aru[a[i]] < 1)
      continue;
    else {
      if (aru[a[i]] == 1)
        ans++;
      for (int j = a[i] * 2; j < n_max; j += a[i]) {
        aru[j] = 1e7;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
// Apparat : drobucs, aleonov, vit_72
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

// #pragma warning(disable : 4996)
// #pragma GCC optimize("O3")

using namespace std;

#define nptr nullptr
#define all(x) (x).begin(), (x).end()
#define drobucs                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cout.tie(0);                                                                 \
  cin.tie(0);
#define ff first
#define ss second
#define pb push_back
#define sz(x) (int)(x).size()

typedef long long ll;

const int maxn = (int)2e5 + 13;
const ll INFL = 9223372036854775807;
const ll INF = 2147483647;
const ll MOD = (int)1e9 + 7,
         MOD2 = 998244353; // 1e9 + 21, 1e9 + 33, 1e9 + 87, 1e9 + 93
const ll base = 101, base2 = 37;

int a[maxn];
int mp[maxn];
int main() {
  drobucs;

  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i], mp[a[i]]++;
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    bool f = 1;
    for (ll j = 2; j * j <= a[i]; ++j) {
      if (a[i] % j == 0 && (mp[j] != 0 || mp[a[i] / j] != 0))
        f = 0;
    }
    if (a[i] != 1 && mp[1] != 0)
      f = 0;
    if (mp[a[i]] > 1)
      f = 0;
    ans += f;
  }
  cout << ans << "\n";
  return 0;
}

Fixed submission:
// Apparat : drobucs, aleonov, vit_72
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

// #pragma warning(disable : 4996)
// #pragma GCC optimize("O3")

using namespace std;

#define nptr nullptr
#define all(x) (x).begin(), (x).end()
#define drobucs                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cout.tie(0);                                                                 \
  cin.tie(0);
#define ff first
#define ss second
#define pb push_back
#define sz(x) (int)(x).size()

typedef long long ll;

const int maxn = (int)2e6 + 13;
const ll INFL = 9223372036854775807;
const ll INF = 2147483647;
const ll MOD = (int)1e9 + 7,
         MOD2 = 998244353; // 1e9 + 21, 1e9 + 33, 1e9 + 87, 1e9 + 93
const ll base = 101, base2 = 37;

int a[maxn];
int mp[maxn];
int main() {
  drobucs;

  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i], mp[a[i]]++;
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    bool f = 1;
    for (ll j = 2; j * j <= a[i]; ++j) {
      if (a[i] % j == 0 && (mp[j] != 0 || mp[a[i] / j] != 0))
        f = 0;
    }
    if (a[i] != 1 && mp[1] != 0)
      f = 0;
    if (mp[a[i]] > 1)
      f = 0;
    ans += f;
  }
  cout << ans << "\n";
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define ll long long int
#define pb push_back
#define mp make_pair
const int mod = 1e9 + 7;

void solve() {
  int n;
  cin >> n;
  vector<int> V(n);
  map<int, pair<int, bool>> um;
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> V[i];
    um[V[i]] = mp(i, true);
    m[V[i]]++;
  }
  if (um[1].second == true) {
    if (m[1] == 1)
      cout << 1;
    else if (m[1] > 1)
      cout << 0;
    return;
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    int x = V[i];
    for (int j = 1; j <= sqrt(x); j++) {
      if (x % j == 0) {
        if (m[j] == 1) {
          if (um[j].second == true && um[j].first != i) {
            cnt++;
            break;
          }
        } else if (m[j] > 1) {
          if (um[j].second == true) {
            cnt++;
            break;
          }
        }

        if (m[x / j] == 1) {
          if (um[x / j].second == true && um[x / j].first != i) {
            cnt++;
            break;
          }
        } else if (m[x / j] > 1) {
          if (um[x / j].second == true) {
            cnt++;
            break;
          }
        }
      }
    }
  }
  cout << n - cnt << endl;
}

int main() { fastio solve(); }


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define ll long long int
#define pb push_back
#define mp make_pair
const int mod = 1e9 + 7;

void solve() {
  int n;
  cin >> n;
  vector<int> V(n);
  unordered_map<int, pair<int, bool>> um;
  unordered_map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> V[i];
    um[V[i]] = mp(i, true);
    m[V[i]]++;
  }
  if (um[1].second == true) {
    if (m[1] == 1)
      cout << 1;
    else if (m[1] > 1)
      cout << 0;
    return;
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    int x = V[i];
    for (int j = 1; j <= sqrt(x); j++) {
      if (x % j == 0) {
        if (m[j] == 1) {
          if (um[j].second == true && um[j].first != i) {
            cnt++;
            break;
          }
        } else if (m[j] > 1) {
          if (um[j].second == true) {
            cnt++;
            break;
          }
        }

        if (m[x / j] == 1) {
          if (um[x / j].second == true && um[x / j].first != i) {
            cnt++;
            break;
          }
        } else if (m[x / j] > 1) {
          if (um[x / j].second == true) {
            cnt++;
            break;
          }
        }
      }
    }
  }
  cout << n - cnt << endl;
}

int main() { fastio solve(); }
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
vector<int> V;
vector<int> resposta;
int vis[100100];

int crivo(int limite, int ind) {
  int cont = 0;
  for (int i = 0; i < ind; i++) {
    if (vis[V[i]] == 0) {
      vis[V[i]] = 2;
      cont++;
      for (int j = 2 * V[i]; j <= limite; j += V[i])
        vis[j] = 1;
    } else if (vis[V[i]] == 2) { // Ja foi contado
      vis[V[i]] = 3;
      cont--;
    }
  }
  return cont;
}

int main() {

  int n;
  cin >> n;

  int limite = 0;
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    if (num > limite)
      limite = num;
    V.push_back(num);
  }

  sort(V.begin(), V.end());
  cout << crivo(limite, n) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
vector<int> V;
vector<int> resposta;
int vis[1001000];

int crivo(int limite, int ind) {
  int cont = 0;
  for (int i = 0; i < ind; i++) {
    if (vis[V[i]] == 0) {
      vis[V[i]] = 2;
      cont++;
      for (int j = 2 * V[i]; j <= limite; j += V[i])
        vis[j] = 1;
    } else if (vis[V[i]] == 2) { // Ja foi contado
      vis[V[i]] = 3;
      cont--;
    }
  }
  return cont;
}

int main() {

  int n;
  cin >> n;

  int limite = 0;
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    if (num > limite)
      limite = num;
    V.push_back(num);
  }

  sort(V.begin(), V.end());
  cout << crivo(limite, n) << endl;
  return 0;
}
Problem p02642 (C++)

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

#define FAST                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define show(x) cout << (x) << endl
#define REP(i, a, n) for (int i = a; i < n; i++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define UM unordered_map
#define PB push_back
#define V vector
#define EPS 1e-8
typedef unsigned long ul;
typedef long long ll;
typedef string str;
/*FIN*/
set<int> m;
map<ul, ul> f;
ul a[200001];
ul n, ans;
int main() {
  FAST;
  cin >> n;
  REP(i, 0, n) cin >> a[i];
  sort(a, a + n);
  REP(i, 0, n) {
    if (!f.count(a[i]))
      f[a[i]] = 0;
    f[a[i]]++;
  }
  ans = 0;
  REP(i, 0, n) {
    if (f[a[i]] == 1) {
      bool ff = 1;
      for (auto t : m) {
        if (a[i] % t == 0)
          ff = 0;
      }
      if (ff)
        ans++;
    }
    m.insert(a[i]);
  }
  show(ans);
  return 0;
}


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

#define FAST                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define show(x) cout << (x) << endl
#define REP(i, a, n) for (int i = a; i < n; i++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define UM unordered_map
#define PB push_back
#define V vector
#define EPS 1e-8
typedef unsigned long ul;
typedef long long ll;
typedef string str;
/*FIN*/
set<int> m;
map<ul, ul> f;
ul a[200001];
ul n, ans;
int main() {
  FAST;
  cin >> n;
  REP(i, 0, n) cin >> a[i];
  sort(a, a + n);
  REP(i, 0, n) {
    if (!f.count(a[i]))
      f[a[i]] = 0;
    f[a[i]]++;
  }
  ans = 0;
  REP(i, 0, n) {
    if (!m.count(a[i]) and f[a[i]] == 1)
      ans++;
    for (ul j = a[i]; j <= a[n - 1]; j += a[i])
      m.insert(j);
  }
  show(ans);
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

const int M = 1000005;

int main(void) {
  int N;
  cin >> N;
  vector<int> A(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  int ans(0);
  vector<int> dp(M, 0);
  int p;

  for (auto e : A) {
    if (dp[p] != 0) {
      dp[p] = 2;
      continue;
    }
    for (p = e; p < M; p += e) {
      dp.at(p)++;
    }
  }

  for (auto e : A) {
    if (dp[e] == 1)
      ans++;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

const int M = 1000005;

int main(void) {
  int N;
  cin >> N;
  vector<int> A(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  int ans(0);
  vector<int> dp(M, 0);
  int p;

  for (auto e : A) {
    if (dp[e] != 0) {
      dp[e] = 2;
      continue;
    }
    for (p = e; p < M; p += e) {
      dp.at(p)++;
    }
  }

  for (auto e : A) {
    if (dp[e] == 1)
      ans++;
  }

  cout << ans << endl;

  return 0;
}
Problem p02642 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter

n = int(input())
alist = [int(v) for v in input().split()]
dup_set = {k for k, v in Counter(alist).items() if v >= 2}
alist = sorted(set(alist))
adict = {a: False for a in alist}
a_max = alist[-1]
max_ind = -1

count = 0
if 1 in adict:
    count = 0 if 1 in dup_set else 1
else:
    for a, f in adict.items():
        if f:
            continue
        if a not in dup_set:
            count += 1
        for k in range(a, a_max + 1, a):
            if k in adict:
                adict[k] = True
                if k == a_max:
                    max_ind -= 1
                    a_max = alist[max_ind]

print(count)


Fixed submission:
from collections import Counter

n = int(input())
alist = [int(v) for v in input().split()]
dup_set = {k for k, v in Counter(alist).items() if v >= 2}
alist = sorted(set(alist))
adict = {a: False for a in alist}
a_max = alist[-1]
max_ind = -1

count = 0
if 1 in adict:
    count = 0 if 1 in dup_set else 1
else:
    for a, f in adict.items():
        if f:
            continue
        if a not in dup_set:
            count += 1
        for k in range(a, a_max + 1, a):
            if k in adict:
                adict[k] = True
                if k == a_max:
                    max_ind -= 1
                    if abs(max_ind) <= len(alist):
                        a_max = alist[max_ind]

print(count)
Problem p02642 (Python)

Buggy submission (status: Runtime Error):
from numba import njit


@njit(cache=True)
def fact(x):
    i = 1
    while i * i <= x:
        if x % i == 0:
            yield i
            yield x // i
        i += 1


def main():
    n = int(input())
    a = tuple(map(int, input().split()))

    c = [0] * (max(a) + 1)

    for e in a:
        c[e] += 1

    ans = n
    for e in a:
        c[e] -= 1
        for fct in fact(e):
            if c[fct]:
                ans -= 1
                break

        c[e] += 1

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
from numba import njit


@njit(fastmath=True)
def fact(x):
    i = 1
    while i * i <= x:
        if x % i == 0:
            yield i
            yield x // i
        i += 1


def main():
    n = int(input())
    a = tuple(map(int, input().split()))

    c = [0] * (max(a) + 1)

    for e in a:
        c[e] += 1

    ans = n
    for e in a:
        c[e] -= 1
        for fct in fact(e):
            if c[fct]:
                ans -= 1
                break

        c[e] += 1

    print(ans)


if __name__ == "__main__":
    main()
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#ifdef DEBUG

#else

#pragma GCC optimize("O3,no-stack-protector")
#pragma GCC optimize("unroll-loops")

#if __cplusplus < 201703L
#pragma GCC target("avx")
#else
#pragma GCC target("avx2")
#endif

#endif

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
typedef int64_t Int;
typedef long double Ld;

using namespace std;
using Graph = vector<vector<Int>>;

template <typename T> using vec = vector<T>;

template <typename T> using vec2 = vector<vector<T>>;

template <typename T> using vec3 = vector<vector<vector<T>>>;

const Ld pi = M_PI;
const Int MOD = 1000000007;
const Int INF = 1LL << 61;

Int Floor(Int a, Int b) { return (a - (a % b)) / b; }

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

  const int inf = 1000000;
  int n;
  cin >> n;
  vec<int> a(n), cnt(inf + 10, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  sort(a.begin(), a.end());

  int ans = 0;

  vec<bool> isok(inf + 10, true);
  for (int i = 0; i < n; i++) {
    if (isok[a[i]] && cnt[a[i]] == 1) {
      ans++;
    }

    for (int j = 1;; j++) {
      int mult = j * a[i];
      if (mult <= inf) {
        isok[mult] = false;
      } else {
        break;
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#ifdef DEBUG

#else

#pragma GCC optimize("O3,no-stack-protector")
#pragma GCC optimize("unroll-loops")

#if __cplusplus < 201703L
#pragma GCC target("avx")
#else
#pragma GCC target("avx2")
#endif

#endif

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
typedef int64_t Int;
typedef long double Ld;

using namespace std;
using Graph = vector<vector<Int>>;

template <typename T> using vec = vector<T>;

template <typename T> using vec2 = vector<vector<T>>;

template <typename T> using vec3 = vector<vector<vector<T>>>;

const Ld pi = M_PI;
const Int MOD = 1000000007;
const Int INF = 1LL << 61;

Int Floor(Int a, Int b) { return (a - (a % b)) / b; }

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

  const int inf = 1000000;
  int n;
  cin >> n;
  vec<int> a(n), cnt(inf + 10, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  sort(a.begin(), a.end());

  int ans = 0;

  vec<bool> isok(inf + 10, true);
  for (int i = 0; i < n; i++) {
    if (isok[a[i]] && cnt[a[i]] == 1) {
      ans++;
    }

    if (isok[a[i]]) {
      for (int j = 1;; j++) {
        int mult = j * a[i];
        if (mult <= inf) {
          isok[mult] = false;
        } else {
          break;
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02642 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

cand = set(a)

exist = set()
dup = set()
for item in a:
    if item in exist:
        dup.add(item)
    else:
        exist.add(item)

dame_set = set()

a.sort()
ret = 0

for val in a:
    if val in dup:
        i = 1
        cnt = 10**6 // val
        while i <= cnt:
            tmp = i * val
            if tmp in cand:
                dame_set.add(tmp)
            i += 1
    else:
        if val in dame_set:
            continue
        else:
            ret += 1
            i = 1
            cnt = 10**6 // val
            while i <= cnt:
                tmp = i * val
                if tmp in cand:
                    dame_set.add(tmp)
                i += 1

print(ret)


Fixed submission:
n = int(input())
a = list(map(int, input().split()))

cand = set(a)

exist = set()
dup = set()
for item in a:
    if item in exist:
        dup.add(item)
    else:
        exist.add(item)

dame_set = set()

a.sort()
ret = 0

for val in a:
    if val in dup:
        if val in dame_set:
            continue
        else:
            i = 1
            cnt = 10**6 // val
            while i <= cnt:
                tmp = i * val
                if tmp in cand:
                    dame_set.add(tmp)
                i += 1
    else:
        if val in dame_set:
            continue
        else:
            ret += 1
            i = 1
            cnt = 10**6 // val
            while i <= cnt:
                tmp = i * val
                if tmp in cand:
                    dame_set.add(tmp)
                i += 1

print(ret)
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
vector<long long> divisor(long long n) {
  vector<long long> res;
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.push_back(i);
      if (n / i != i)
        res.push_back(n / i);
    }
  }
  return res;
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> mp;
  rep(i, n) {
    cin >> a[i];
    mp[a[i]]++;
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    auto div = divisor(a[i]);
    bool ok = true;
    mp[a[i]]--;
    for (auto e : div)
      if (mp[e] > 0)
        ok = false;
    if (ok)
      res++;
    mp[a[i]]++;
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
vector<long long> divisor(long long n) {
  vector<long long> res;
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.push_back(i);
      if (n / i != i)
        res.push_back(n / i);
    }
  }
  return res;
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  unordered_map<int, int> mp;
  rep(i, n) {
    cin >> a[i];
    mp[a[i]]++;
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    auto div = divisor(a[i]);
    bool ok = true;
    mp[a[i]]--;
    for (auto e : div)
      if (mp[e] > 0)
        ok = false;
    if (ok)
      res++;
    mp[a[i]]++;
  }
  cout << res << endl;
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

vector<int64_t> divisor(int64_t n) {
  vector<int64_t> ret;
  for (int64_t i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

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

  int n;
  cin >> n;
  int a[200010];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  multiset<int> s;
  for (int i = 0; i < n; i++) {
    s.insert(a[i]);
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    s.erase(s.find(a[i]));

    auto div = divisor(a[i]);
    for (auto e : div) {
      if (s.find(e) != s.end()) {
        goto bad;
      }
    }
    res++;
  bad:
    s.insert(a[i]);
  }

  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

vector<int> divisor(int n) {
  vector<int> ret;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

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

  int n;
  cin >> n;
  int a[200010];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  multiset<int> s;
  for (int i = 0; i < n; i++) {
    s.insert(a[i]);
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    s.erase(s.find(a[i]));

    auto div = divisor(a[i]);
    for (auto e : div) {
      if (s.find(e) != s.end()) {
        goto bad;
      }
    }
    res++;
  bad:
    s.insert(a[i]);
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define pp pair<ll, ll>
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll MOD = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
ll mod = INF;

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1), b(2000000, 0);
  rep(i, n) { cin >> a[i]; }
  a[n] = inf;
  sort(all(a));
  int ans = 0;
  rep(i, n) {
    if (b[a[i]] == 0 && a[i] != a[i + 1])
      ans++;
    int c = a[i];
    while (true) {
      if (c > 2000000)
        break;
      b[c] = 1;
      c += a[i];
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define pp pair<ll, ll>
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll MOD = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
ll mod = INF;

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1), b(2000000, 0);
  rep(i, n) { cin >> a[i]; }
  a[n] = inf;
  sort(all(a));
  int ans = 0;
  rep(i, n) {
    if (b[a[i]] == 0 && a[i] != a[i + 1])
      ans++;
    if (b[a[i]] == 1)
      continue;
    int c = a[i];
    while (true) {
      if (c > 2000000)
        break;
      b[c] = 1;
      c += a[i];
    }
  }
  cout << ans << endl;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<P, P> PP;
const ll MOD = 998244353;
const ll MAX_N = 500010;
const ll INF = 100100100100100;

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());
  vector<bool> visited(1001001, true);
  for (int i = 0; i < n - 1; i++) {
    if (a[i] == a[i + 1])
      visited[a[i]] = false;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int num = a[i];
    if (visited[num])
      ans++;
    for (int times = 1; num * times < 1001001; times++) {
      visited[num * times] = false;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<P, P> PP;
const ll MOD = 998244353;
const ll MAX_N = 500010;
const ll INF = 100100100100100;

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());
  vector<bool> visited(1001001, true);
  for (int i = 0; i < n - 1; i++) {
    if (a[i] == a[i + 1])
      visited[a[i]] = false;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int num = a[i];
    if (i > 0 && a[i] == a[i - 1])
      continue;
    if (visited[num])
      ans++;
    for (int times = 1; num * times < 1001001; times++) {
      visited[num * times] = false;
    }
  }
  cout << ans << endl;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const ll mod = 1e9;
const int inf = 0x3f3f3f3f;

int a[200005];
int isp[1000005];
int boom[1000005];
vector<int> v;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    boom[a[i]]++;
  }
  int ans = 0;
  for (int i = 0; i <= 1000000; i++) {
    if (boom[i] == 1)
      v.push_back(i);
    else if (boom[i] >= 2) {
      int t = 1;
      while (i * t <= 1000000) {
        isp[v[i] * t] = 1;
        t++;
      }
    }
  }
  for (int i = 0; i < v.size(); i++) {
    if (isp[v[i]] == 0) {
      ans++;
      int t = 1;
      while (v[i] * t <= 1000000) {
        isp[v[i] * t] = 1;
        t++;
      }
    }
  }
  if (boom[1] > 1)
    ans = 0;
  cout << ans << endl;
}


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

int a[200005];
int isp[1000005];
int boom[1000005];
vector<int> v;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    boom[a[i]]++;
  }
  int ans = 0;
  for (int i = 0; i <= 1000000; i++) {
    if (boom[i] == 1)
      v.push_back(i);
    else if (boom[i] >= 2) {
      int t = 1;
      while (i * t <= 1000000) {
        isp[i * t] = 1;
        t++;
      }
    }
  }
  for (int i = 0; i < v.size(); i++) {
    if (isp[v[i]] == 0) {
      ans++;
      int t = 1;
      while (v[i] * t <= 1000000) {
        isp[v[i] * t] = 1;
        t++;
      }
    }
  }
  if (boom[1] > 1)
    ans = 0;
  cout << ans << endl;
}