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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define db(a) cout << a << endl
#define db2(a, b) cout << a << " " << b << endl
#define dbd(a) cout << #a << ": " << a << endl
#define dbd2(a, b) cout << #a << ": " << a << ", " << #b << ": " << b << endl
#define dbp(a) cout << a.first << " " << a.second << endl
#define adb(a)                                                                 \
  for (auto _i : a)                                                            \
    cout << _i << " ";                                                         \
  cout << endl
#define adbp(a)                                                                \
  for (auto _i : a)                                                            \
    cout << _i.first << " " << _i.second << endl;
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define mod 998244353
#define N 1000010
#define M 100010
using namespace std;

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

  long long a[n], i;
  map<long long, long long> mp;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
  }

  sort(a, a + n);
  long long ans = 0;
  bool chk[N] = {false};
  for (i = 0; i < n; i++) {
    if (!chk[a[i]] && mp[a[i]] == 1)
      ans++;
    for (int j = a[i]; j < N; j += a[i])
      chk[j] = true;
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define db(a) cout << a << endl
#define db2(a, b) cout << a << " " << b << endl
#define dbd(a) cout << #a << ": " << a << endl
#define dbd2(a, b) cout << #a << ": " << a << ", " << #b << ": " << b << endl
#define dbp(a) cout << a.first << " " << a.second << endl
#define adb(a)                                                                 \
  for (auto _i : a)                                                            \
    cout << _i << " ";                                                         \
  cout << endl
#define adbp(a)                                                                \
  for (auto _i : a)                                                            \
    cout << _i.first << " " << _i.second << endl;
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define mod 998244353
#define N 1000010
#define M 100010
using namespace std;

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

  long long a[n], i;
  map<long long, long long> mp;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
  }

  sort(a, a + n);
  long long ans = 0;
  bool chk[N] = {false};
  for (i = 0; i < n; i++) {
    if (!chk[a[i]] && mp[a[i]] == 1)
      ans++;
    if (a[i] == 1)
      break;
    if (!chk[a[i]])
      for (int j = a[i]; j < N; j += a[i])
        chk[j] = true;
  }

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

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

typedef long long ll;
typedef pair<int, int> P;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};

int main() {
  int n;
  cin >> n;
  vector<bool> m(1000005);
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i <= 1000000; i++) {
    m[i] = true;
  }
  sort(a.begin(), a.end());
  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (a[i] == a[i + 1] && a[i]) {
      m[a[i]] = false;
      for (int j = 1; j * a[i] <= 1000000; j++) {
        m[j * a[i]] = false;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (m[a[i]]) {
      ans++;
      for (int j = 1; j * a[i] <= 1000000; j++) {
        m[j * a[i]] = false;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


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

typedef long long ll;
typedef pair<int, int> P;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};

int main() {
  int n;
  cin >> n;
  vector<bool> m(1000005);
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i <= 1000000; i++) {
    m[i] = true;
  }
  sort(a.begin(), a.end());
  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (a[i] == a[i + 1] && m[a[i]]) {
      m[a[i]] = false;
      for (int j = 1; j * a[i] <= 1000000; j++) {
        m[j * a[i]] = false;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (m[a[i]]) {
      ans++;
      for (int j = 1; j * a[i] <= 1000000; j++) {
        m[j * a[i]] = false;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
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 (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define INF 1e9

void init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
}

int main() {
  int N = 1000000;
  init();
  int n;
  cin >> n;
  vector<int> data(N, 0);
  vector<int> d(n);
  rep(i, n) {
    cin >> d[i];
    for (int j = d[i]; j <= N; j += d[i]) {
      data[j]++;
    }
  }
  int ans = 0;
  rep(i, n) {
    if (data[d[i]] == 1)
      ans++;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define INF 1e9

void init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
}

int main() {
  int N = 1000000;
  init();
  int n;
  cin >> n;
  vector<int> data(N, 0);
  vector<int> d(n);
  rep(i, n) {
    cin >> d[i];
    if (data[d[i]] != 0) {
      data[d[i]] = 2;
      continue;
    }
    for (int j = d[i]; j <= N; j += d[i]) {
      data[j]++;
    }
  }
  int ans = 0;
  rep(i, n) {
    if (data[d[i]] == 1)
      ans++;
  }
  cout << ans << endl;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

#define ll long long

int main() {
  ll n, tmp, cnt = 0;
  cin >> n;
  vector<ll> a(1000000 + 1), tv(n);
  for (int i = 0; i < n; i++) {
    cin >> tv[i];
  }
  sort(tv.begin(), tv.end());
  for (int i = 0; i < n; i++) {
    bool mushi =
        ((i != 0 && tv[i] == tv[i - 1]) || (i != n - 1 && tv[i] == tv[i + 1]));
    if (a[tv[i]] == 0 && !mushi) {
      cnt++;
    }

    ll kake = 1;
    while (tv[i] * kake <= 1000000) {
      a[tv[i] * kake] = 1;
      kake++;
    }
  }
  cout << cnt << endl;
  return 0;
}

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

using namespace std;

#define ll long long

int main() {
  ll n, tmp, cnt = 0;
  cin >> n;
  vector<ll> a(1000000 + 1), tv(n);
  for (int i = 0; i < n; i++) {
    cin >> tv[i];
  }
  sort(tv.begin(), tv.end());
  for (int i = 0; i < n; i++) {
    bool mushi =
        ((i != 0 && tv[i] == tv[i - 1]) || (i != n - 1 && tv[i] == tv[i + 1]));
    if (a[tv[i]] == 0) {
      if (!mushi) {
        cnt++;
      }
      ll kake = 1;
      while (tv[i] * kake <= 1000000) {
        a[tv[i] * kake] = 1;
        kake++;
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02642 (C++)

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

  int n;
  cin >> n;
  vector<ll> ans(1000005, 0), b(n);
  rep(i, n) {
    cin >> b[i];
    ll l = 1;
    while (b[i] * l < 1000005) {
      ans[b[i] * l]++;
      l++;
    }
  }
  ll cnt = 0;
  rep(i, n) {
    if (ans[b[i]] == 1)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}

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

  int n;
  cin >> n;
  vector<ll> ans(1000005, 0), b(n);
  rep(i, n) {
    cin >> b[i];
    ll l = 1;
    if (ans[b[i]] != 0)
      ans[b[i]] = 2;
    else {
      while (b[i] * l < 1000005) {
        ans[b[i] * l]++;
        l++;
      }
    }
  }
  ll cnt = 0;
  rep(i, n) {
    if (ans[b[i]] == 1)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> cnt(n);
  for (int x : a) {
    if (cnt[x] != 0) {
      cnt[x] = 2;
      continue;
    }
    for (int i = x; i < 1000005; i += x)
      cnt[i]++;
  }
  int ans = 0;
  for (int x : a) {
    if (cnt[x] == 1)
      ans++;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> cnt(1000005);
  for (int x : a) {
    if (cnt[x] != 0) {
      cnt[x] = 2;
      continue;
    }
    for (int i = x; i < 1000005; i += x)
      cnt[i]++;
  }
  int ans = 0;
  for (int x : a) {
    if (cnt[x] == 1)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02642 (C++)

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

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;

#define REP(i, n, m) for (ll i = n; i < (ll)m; ++i)
#define IREP(i, n, m) for (ll i = n - 1; i >= m; --i)
#define rep(i, n) REP(i, 0, n)
#define irep(i, n) IREP(i, n, 0)
#define all(v) v.begin(), v.end()
#define vprint(v)                                                              \
  for (auto e : v) {                                                           \
    cout << e << " ";                                                          \
  };                                                                           \
  cout << endl;
#define vvprint(vv)                                                            \
  for (auto v : vv)                                                            \
    vprint(v);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(20);

  ll N;
  cin >> N;
  vll A(N);
  rep(i, N) cin >> A[i];
  sort(all(A));

  unordered_set<ll> s;
  ll count = 0;
  rep(i, N) {
    if (i < N - 1 && A[i] == A[i + 1]) {
      for (ll n = A[i]; n <= 1000000; n += A[i]) {
        s.insert(n);
      }
    } else {
      if (s.find(A[i]) == s.end())
        ++count;
      for (ll n = A[i]; n <= 1000000; n += A[i]) {
        s.insert(n);
      }
    }
  }
  cout << count << endl;
}


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

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;

#define REP(i, n, m) for (ll i = n; i < (ll)m; ++i)
#define IREP(i, n, m) for (ll i = n - 1; i >= m; --i)
#define rep(i, n) REP(i, 0, n)
#define irep(i, n) IREP(i, n, 0)
#define all(v) v.begin(), v.end()
#define vprint(v)                                                              \
  for (auto e : v) {                                                           \
    cout << e << " ";                                                          \
  };                                                                           \
  cout << endl;
#define vvprint(vv)                                                            \
  for (auto v : vv)                                                            \
    vprint(v);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(20);

  ll N;
  cin >> N;
  vll A(N);
  rep(i, N) cin >> A[i];
  sort(all(A));

  unordered_set<ll> s;
  ll count = 0;
  rep(i, N) {
    if (i > 0 && A[i - 1] == A[i])
      continue;
    if (i < N - 1 && A[i] == A[i + 1]) {
      for (ll n = A[i]; n <= 1000000; n += A[i]) {
        s.insert(n);
      }
    } else {
      if (s.find(A[i]) == s.end())
        ++count;
      for (ll n = A[i]; n <= 1000000; n += A[i]) {
        s.insert(n);
      }
    }
  }
  cout << count << endl;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long MOD = 998244353;

int main(void) {
  int n;
  cin >> n;
  vector<int> A(n);
  rep(i, n) cin >> A[i];
  sort(A.begin(), A.end());
  int ans = 0;
  vector<int> done(1000009, 0);
  rep(i, n) {
    if (done[A[i]] == 0) {
      if (i != n - 1) {
        if (A[i] != A[i + 1])
          ans++;
      } else
        ans++;
    }
    rep(j, 1000000) {
      if (A[i] * j > 1000000)
        break;
      done[A[i] * j] = 1;
    }
  }
  // rep(i,1000000) cout<<i<<" "<<done[i]<<endl;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long MOD = 998244353;

int main(void) {
  int n;
  cin >> n;
  vector<int> A(n);
  rep(i, n) cin >> A[i];
  sort(A.begin(), A.end());
  int ans = 0;
  vector<int> done(1000009, 0);
  rep(i, n) {
    if (done[A[i]] == 0) {
      if (i != n - 1) {
        if (A[i] != A[i + 1])
          ans++;
      } else
        ans++;
      rep(j, 1000000) {
        if (A[i] * j > 1000000)
          break;
        done[A[i] * j] = 1;
      }
    }
  }
  // rep(i,1000000) cout<<i<<" "<<done[i]<<endl;
  cout << ans << endl;
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N, ans = 0;
  cin >> N;
  map<ll, ll> M;
  vector<ll> used(200010, 0);
  vector<ll> Z(N, 0);
  for (ll i = 0; i < N; i++) {
    ll A;
    cin >> A;
    Z[i] = A;
    used[A]++;
  }
  for (ll i = 0; i < N; i++) {
    // 約数
    ll n = Z[i];
    vector<ll> B(0, 0);
    for (ll j = 1; j * j <= n; j++) {
      if (n % j == 0) {
        B.emplace_back(j);
        if (n / j == j)
          continue;
        B.emplace_back(n / j);
      }
    }
    ll bsz = B.size();
    for (ll j = 0; j < bsz; j++) {
      if (B[j] < n && used[B[j]] > 0)
        break;
      if (B[j] == n && used[B[j]] > 1)
        break;
      if (j == bsz - 1)
        ans++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N, ans = 0;
  cin >> N;
  vector<ll> used(1000010, 0);
  vector<ll> Z(N, 0);
  for (ll i = 0; i < N; i++) {
    ll A;
    cin >> A;
    Z[i] = A;
    used[A]++;
  }
  for (ll i = 0; i < N; i++) {
    // 約数
    ll n = Z[i];
    vector<ll> B(0, 0);
    for (ll j = 1; j * j <= n; j++) {
      if (n % j == 0) {
        B.emplace_back(j);
        if (n / j == j)
          continue;
        B.emplace_back(n / j);
      }
    }
    ll bsz = B.size();
    for (ll j = 0; j < bsz; j++) {
      if (B[j] < n && used[B[j]] > 0)
        break;
      if (B[j] == n && used[B[j]] > 1)
        break;
      if (j == bsz - 1)
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define ll long long
#define ull unsigned long long
#define dd double
#define oo 1001007000
#define ff float
#define ooo 4223352036854775007
#define iii pair<ll, ll>
#define vii vector<ll>
#define viii vector<iii>
#define sss pair<str, str>
#define str string
#define sii set<ll>
#define siii set<iii>
#define r0 return 0
#define pb push_back
#define sz size
using namespace std;

const char E = '\n';
const int N = 200005;
const int NN = 2005;
const ll md = 998244353;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int a, b, n, t, tmp;
  cin >> n;
  vii v, v1(N, 0);
  for (int i = 0; i < n; i++) {
    cin >> a;
    v.pb(a);
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    tmp = v[i];
    for (int j = tmp; j <= v[n - 1]; j += tmp)
      v1[j]++;
  }
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    if (v1[v[i]] == 1)
      cnt++;
  }
  cout << cnt << E;
  r0;
}

/*







*/


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define ll long long
#define ull unsigned long long
#define dd double
#define oo 1001007000
#define ff float
#define ooo 4223352036854775007
#define iii pair<ll, ll>
#define vii vector<ll>
#define viii vector<iii>
#define sss pair<str, str>
#define str string
#define sii set<ll>
#define siii set<iii>
#define r0 return 0
#define pb push_back
#define sz size
using namespace std;

const char E = '\n';
const int N = 2000005;
const int NN = 2005;
const ll md = 998244353;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int a, b, n, t, tmp;
  cin >> n;
  vii v, v1(N, 0);
  for (int i = 0; i < n; i++) {
    cin >> a;
    v.pb(a);
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    tmp = v[i];
    for (int j = tmp; j <= v[n - 1]; j += tmp)
      v1[j]++;
  }
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    if (v1[v[i]] == 1)
      cnt++;
  }
  cout << cnt << E;
  r0;
}

/*







*/
Problem p02642 (C++)

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

using namespace std;
#define all(a) a.begin(), a.end()
using ll = long long;
const int INF = 1 << 30;
const ll INFll = 1LL << 62;
const int mod = int(1e9) + 7;
using P = pair<ll, ll>;
using ld = long double;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  map<ll, int> mp;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    mp[a[i]]++;
  }
  sort(all(a));
  vector<bool> ch(n, true);
  for (int i = 0; i < n; ++i) {
    if (mp[1] > 0) {
      if (a[i] != 1) {
        ch[i] = false;
      }
    }
    for (ll j = 2; j * j <= a[i]; ++j) {
      if (a[i] % j == 0) {
        if (mp[j] > 0 || mp[a[i] / j] > 0) {
          ch[i] = false;
        }
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < n - 1; ++i) {
    if (a[i] == a[i + 1]) {
      ch[i] = ch[i + 1] = false;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (ch[i])
      ans++;
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;
#define all(a) a.begin(), a.end()
using ll = long long;
const int INF = 1 << 30;
const ll INFll = 1LL << 62;
const int mod = int(1e9) + 7;
using P = pair<ll, ll>;
using ld = long double;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  unordered_map<ll, int> mp;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    mp[a[i]]++;
  }
  sort(all(a));
  vector<bool> ch(n, true);
  for (int i = 0; i < n; ++i) {
    if (mp[1] > 0) {
      if (a[i] != 1) {
        ch[i] = false;
      }
    }
    for (ll j = 2; j * j <= a[i]; ++j) {
      if (a[i] % j == 0) {
        if (mp[j] > 0 || mp[a[i] / j] > 0) {
          ch[i] = false;
        }
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < n - 1; ++i) {
    if (a[i] == a[i + 1]) {
      ch[i] = ch[i + 1] = false;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (ch[i])
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int inf = 1e6;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  vector<int> v(inf + 1);
  rep(i, n) v[a[i]]++;

  vector<bool> p(inf + 1);
  rep(i, n) {
    if (v[a[i]] > 1)
      p[a[i]] = true;
    for (int d = 2 * a[i]; d <= inf; d += a[i]) {
      p[d] = true;
    }
  }
  int ans = 0;
  rep(i, n) if (!p[a[i]])++ ans;
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int inf = 1e6;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  vector<int> v(inf + 1);
  rep(i, n) v[a[i]]++;

  vector<bool> p(inf + 1);
  rep(i, inf + 1) {
    if (v[i] == 0)
      continue;
    if (v[i] > 1)
      p[i] = true;
    for (int d = 2 * i; d <= inf; d += i) {
      p[d] = true;
    }
  }
  int ans = 0;
  rep(i, n) if (!p[a[i]])++ ans;
  cout << ans << endl;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define yesno(flg)                                                             \
  if (flg) {                                                                   \
    cout << "YES" << endl;                                                     \
  } else {                                                                     \
    cout << "NO" << endl;                                                      \
  }
#define MAX_N 1002
#define i197 1000000007
#define Vi vector<int>
#define Vl vector<long long>
#define pb(a) push_back(a)

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P1;
typedef pair<int, int> Pi;
typedef pair<double, Pi> Pdi;
typedef pair<ll, int> Pli;
typedef pair<P1, ll> P2;
const ll INF = 10000000000000000001;
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
struct Road {
  double cost;
  int a, b;
};
struct edge {
  int to, cost;
};
struct pos {
  int x, y, cost;
};

int f[1000001] = {};
ll a[200002] = {};
ll r = 1000000;

int main() {

  ll n;
  cin >> n;
  rep(i, n) cin >> a[i];
  sort(a, a + n);

  ll ans = 0;
  rep(i, n) {
    if (f[a[i]] == 1)
      continue;
    if (a[i] == a[i + 1])
      ans--;

    ll t = 0;
    while (t < r) {
      t += a[i];
      f[t] = 1;
    }
    ans++;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define yesno(flg)                                                             \
  if (flg) {                                                                   \
    cout << "YES" << endl;                                                     \
  } else {                                                                     \
    cout << "NO" << endl;                                                      \
  }
#define MAX_N 1002
#define i197 1000000007
#define Vi vector<int>
#define Vl vector<long long>
#define pb(a) push_back(a)

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P1;
typedef pair<int, int> Pi;
typedef pair<double, Pi> Pdi;
typedef pair<ll, int> Pli;
typedef pair<P1, ll> P2;
const ll INF = 10000000000000000001;
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
struct Road {
  double cost;
  int a, b;
};
struct edge {
  int to, cost;
};
struct pos {
  int x, y, cost;
};

int f[1000001] = {};
ll a[200002] = {};
ll r = 1000000;

int main() {

  ll n;
  cin >> n;
  rep(i, n) cin >> a[i];
  sort(a, a + n);

  ll ans = 0;
  rep(i, n) {
    if (f[a[i]] == 1)
      continue;
    if (a[i] == a[i + 1])
      ans--;

    ll t = 0;
    while (t < r) {
      t += a[i];
      if (t <= r)
        f[t] = 1;
    }
    ans++;
  }
  cout << ans << endl;

  return 0;
}
Problem p02642 (C++)

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

#include <limits.h>

using namespace std;

typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<int> as;
int ns[200001];

vector<int> calc_divs(int n) {
  vector<int> ans;

  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ans.push_back(i);
      if (i * i != n) {
        ans.push_back(n / i);
      }
    }
  }

  sort(ans.begin(), ans.end());
  return ans;
}

int main(void) {
  int N;
  cin >> N;

  as.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> as[i];
    ns[as[i]]++;
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    vector<int> ds = calc_divs(as[i]);

    // cout << "divs: " << endl;
    // for (auto d : ds) {
    //     cout << d << " ";
    // }
    // cout << endl;

    bool ok = true;
    for (auto d : ds) {
      if (ns[d] > 0) {
        if (ns[d] > 1 || d != as[i]) {
          ok = false;
          break;
        }
      }
    }

    if (ok) {
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#include <limits.h>

using namespace std;

typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<int> as;
int ns[1000001];

vector<int> calc_divs(int n) {
  vector<int> ans;

  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ans.push_back(i);
      if (i * i != n) {
        ans.push_back(n / i);
      }
    }
  }

  sort(ans.begin(), ans.end());
  return ans;
}

int main(void) {
  int N;
  cin >> N;

  as.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> as[i];
    ns[as[i]]++;
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    vector<int> ds = calc_divs(as[i]);

    // cout << "divs: " << endl;
    // for (auto d : ds) {
    //     cout << d << " ";
    // }
    // cout << endl;

    bool ok = true;
    for (auto d : ds) {
      if (ns[d] > 0) {
        if (ns[d] > 1 || d != as[i]) {
          ok = false;
          break;
        }
      }
    }

    if (ok) {
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  set<int> s;
  vector<int> v;
  int sieve[100001] = {0};
  int max = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (s.find(x) == s.end()) {
      s.insert(x);
      v.push_back(x);
      if (x > max)
        max = x;
    } else {
      sieve[x] = 1;
    }
  }
  for (int i = 0; i < v.size(); i++) {
    int j = 2;
    while ((v[i] * j) <= max) {
      sieve[v[i] * j] = 1;
      j++;
    }
  }
  int count = 0;
  for (int i = 0; i < v.size(); i++) {
    if (sieve[v[i]] == 0)
      count++;
  }
  cout << count;
}

Fixed submission:
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  set<int> s;
  vector<int> v;
  int sieve[1000001] = {0};
  int max = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (s.find(x) == s.end()) {
      s.insert(x);
      v.push_back(x);
      if (x > max)
        max = x;
    } else {
      sieve[x] = 1;
    }
  }
  for (int i = 0; i < v.size(); i++) {
    int j = 2;
    while ((v[i] * j) <= max) {
      sieve[v[i] * j] = 1;
      j++;
    }
  }
  int count = 0;
  for (int i = 0; i < v.size(); i++) {
    if (sieve[v[i]] == 0)
      count++;
  }
  cout << count;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// Begin Header {{{
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, j, n) for (ll i = j; i < n; i++)
#define all(x) (x).begin(), (x).end()
constexpr int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const long double PI = acos(-1);
// }}} End Header

int main() {
  int n;
  const int M = 100000005;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> cnt(M);
  for (int x : a) {
    if (cnt[x] != 0) {
      cnt[x] = 2;
      continue;
    }
    for (int k = x; k < M; k += x)
      cnt[k]++;
  }
  int ans = 0;
  for (int x : a) {
    if (cnt[x] == 1)
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// Begin Header {{{
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, j, n) for (ll i = j; i < n; i++)
#define all(x) (x).begin(), (x).end()
constexpr int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const long double PI = acos(-1);
// }}} End Header

int main() {
  int n;
  const int M = 1000099;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> cnt(M);
  for (int x : a) {
    if (cnt[x] != 0) {
      cnt[x] = 2;
      continue;
    }
    for (int k = x; k < M; k += x)
      cnt[k]++;
  }
  int ans = 0;
  for (int x : a) {
    if (cnt[x] == 1)
      ans++;
  }
  cout << ans << endl;
  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);
typedef long long ll;
typedef long double ld;

const ll MOD = 1E9 + 7, INF = 2E18 + 5;
const double PI = 2 * acos(0.0);
const long double EPS = 1.0E-14;

ll arr[200005];
map<ll, ll> mp;

int main() {
  ll n, cnt = 0, limit, flag = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &arr[i]);
    mp[arr[i]]++;
  }
  for (int i = 0; i < n; i++) {
    flag = 0;
    mp[arr[i]]--;
    for (int j = 1; j * j <= arr[i]; j++) {
      if (arr[i] % j == 0) {
        if (mp[j] > 0 or mp[arr[i] / j] > 0) {
          flag = 1;
          break;
        }
      }
    }
    if (!flag)
      cnt++;
    mp[arr[i]]++;
  }
  cout << cnt;

  return 0;
}


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

#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
typedef long long ll;
typedef long double ld;

const ll MOD = 1E9 + 7, INF = 2E18 + 5;
const double PI = 2 * acos(0.0);
const long double EPS = 1.0E-14;

ll arr[200005];
unordered_map<ll, ll> mp;

int main() {
  ll n, cnt = 0, limit, flag = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &arr[i]);
    mp[arr[i]]++;
  }
  for (int i = 0; i < n; i++) {
    flag = 0;
    mp[arr[i]]--;
    for (int j = 1; j * j <= arr[i]; j++) {
      if (arr[i] % j == 0) {
        if (mp[j] > 0 or mp[arr[i] / j] > 0) {
          flag = 1;
          break;
        }
      }
    }
    if (!flag)
      cnt++;
    mp[arr[i]]++;
  }
  cout << cnt;

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int n;
  std::cin >> n;
  std::vector<int> A;
  std::vector<int> Done;
  int maxa = 0;
  for (int i = 0; i < n; i++) {
    int a;
    std::cin >> a;
    maxa = std::max(maxa, a);
    A.push_back(a);
  }
  std::sort(A.begin(), A.end());
  bool not_dividable[maxa + 1];
  for (int i = 0; i <= maxa; i++)
    not_dividable[i] = true;
  for (int i = 0; i < n; i++) {
    int idx = A.at(i);
    if (not_dividable[idx]) {
      if (i < n - 1 && A.at(i) == A.at(i + 1))
        not_dividable[idx] = false;
      for (int j = 2 * idx; j <= maxa; j += idx)
        not_dividable[j] = false;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (std::find(Done.begin(), Done.end(), A.at(i)) != Done.end())
      continue;
    if (not_dividable[A.at(i)]) {
      ++ans;
      Done.push_back(A.at(i));
    }
  }
  std::cout << ans << '\n';
}

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

int main() {
  int n;
  std::cin >> n;
  std::vector<int> A;
  std::vector<int> Done;
  int maxa = 0;
  for (int i = 0; i < n; i++) {
    int a;
    std::cin >> a;
    maxa = std::max(maxa, a);
    A.push_back(a);
  }
  std::sort(A.begin(), A.end());
  bool not_dividable[maxa + 1];
  for (int i = 0; i <= maxa; i++)
    not_dividable[i] = true;
  for (int i = 0; i < n; i++) {
    int idx = A.at(i);
    if (not_dividable[idx]) {
      if (i < n - 1 && A.at(i) == A.at(i + 1))
        not_dividable[idx] = false;
      for (int j = 2 * idx; j <= maxa; j += idx)
        not_dividable[j] = false;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (not_dividable[A.at(i)]) {
      ++ans;
      Done.push_back(A.at(i));
    }
  }
  std::cout << ans << '\n';
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
constexpr char ln = '\n';

const int mx = 100010;
const ll mod = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  unordered_map<int, int> mp;
  rep(i, n) {
    cin >> a[i];
    mp[a[i]]++;
  }
  sort(a.begin(), a.end());
  if (a[0] == 1) {
    if (a[1] == 1) {
      cout << 0 << ln;
      return 0;
    } else {
      cout << 1 << ln;
      return 0;
    }
  }
  vector<bool> d(1000010, true);

  int ans = 0;
  rep(i, n) {
    if (mp[a[i]] == 1 && d[a[i]]) {
      ans++;
    }
    for (int i = 1; i * a[i] < 1000001; i++) {
      d[a[i] * i] = false;
    }
  }

  cout << ans << ln;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
constexpr char ln = '\n';

const int mx = 100010;
const ll mod = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  unordered_map<int, int> mp;
  rep(i, n) {
    cin >> a[i];
    mp[a[i]]++;
  }
  sort(a.begin(), a.end());
  if (a[0] == 1) {
    if (a[1] == 1) {
      cout << 0 << ln;
      return 0;
    } else {
      cout << 1 << ln;
      return 0;
    }
  }
  vector<bool> d(1000010, true);

  int ans = 0;
  rep(i, n) {
    if (mp[a[i]] == 1 && d[a[i]]) {
      ans++;
    }
    for (int j = 1; j * a[i] < 1000001; j++) {
      d[a[i] * j] = false;
    }
  }

  cout << ans << ln;

  return 0;
}
Problem p02642 (C++)

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

void seive(vector<int> &fact, int n, vector<int> a, int r) {
  int i = 0;
  while (i < n) {
    int k = a[i];

    for (int j = k; j <= r; j += k) {
      // cout<<j<<endl;
      fact[j]++;
    }
    i++;
  }
}

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

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

  int r = a[n - 1];
  vector<int> fact(r + 1, 0);
  seive(fact, n, a, r);
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (fact[a[i]] < 2)
      cnt++;
  }
  cout << cnt << endl;

  return 0;
}


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

void seive(vector<int> &fact, int n, vector<int> a, int r) {
  int i = 0;
  while (i < n) {
    int k = a[i];

    for (int j = k; j <= r; j += k) {
      // cout<<j<<endl;
      fact[j]++;
    }
    i++;
  }
}

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

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

  sort(a.begin(), a.end());
  int r = a[n - 1];
  vector<int> fact(r + 1, 0);
  seive(fact, n, a, r);
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (fact[a[i]] < 2)
      cnt++;
  }
  cout << cnt << endl;

  return 0;
}
Problem p02642 (C++)

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

using namespace std;

int main() {

  int N;

  cin >> N;

  vector<int> A(100001, 0);
  vector<int> has(1000001, 0);

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

  sort(A.begin(), A.begin() + N);

  /*for(int i = 0; i < N; i++){
    cout << A[i] << endl;
    }*/

  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (i < N - 1 && A[i] != A[i + 1]) {
      if (has[A[i]] == 0)
        ans++;
    } else if (i == N - 1) {
      if (has[A[i]] == 0)
        ans++;
    }

    for (int t = A[i]; t <= A[N - 1]; t += A[i]) {
      has[t] = 1;
    }
  }

  cout << ans << endl;

  return 0;
}


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

using namespace std;

int main() {

  int N;

  cin >> N;

  vector<int> A(2 * 100001, 0);
  vector<int> has(1000001, 0);

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

  sort(A.begin(), A.begin() + N);

  /*for(int i = 0; i < N; i++){
    cout << A[i] << endl;
    }*/

  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (i < N - 1 && A[i] != A[i + 1]) {
      if (has[A[i]] == 0)
        ans++;
    } else if (i == N - 1) {
      if (has[A[i]] == 0)
        ans++;
    }

    for (int t = A[i]; t <= A[N - 1]; t += A[i]) {
      has[t] = 1;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02642 (C++)

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

typedef long long ll;
typedef pair<int, int> P;
#define INF = 1000000100;
#define rep(i, n) for (int i = 0; i < n; i++)

const int M = 1000005;

int main() {
  int N;
  cin >> N;
  vector<int> a;
  rep(i, N) cin >> a[i];
  vector<int> cnt(M);
  for (int x : a) {
    if (cnt[x] != 0) {
      cnt[x] = 2;
      continue;
    }
    for (int i = x; i < M; i += x) {
      cnt[i]++;
    }
  }
  int ans = 0;
  for (int x : a) {
    if (cnt[x] == 1)
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
#define INF = 1000000100;
#define rep(i, n) for (int i = 0; i < n; i++)

const int M = 1000005;

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  rep(i, N) cin >> a[i];
  vector<int> cnt(M);
  for (int x : a) {
    if (cnt[x] != 0) {
      cnt[x] = 2;
      continue;
    }
    for (int i = x; i < M; i += x) {
      cnt[i]++;
    }
  }
  int ans = 0;
  for (int x : a) {
    if (cnt[x] == 1)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02642 (Python)

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

MAX = 10**6 + 1
prime = [True] * MAX
counted = set()
for v in X:
    if v in counted:
        prime[v] = False
    for j in range(2 * v, MAX, v):
        prime[j] = False
    counted.add(v)

ans = 0
for v in X:
    ans += int(prime[v])
print(ans)


Fixed submission:
N = int(input())
X = list(map(int, input().split()))

MAX = 10**6 + 1
prime = [True] * MAX
counted = set()
for v in X:
    if v in counted:
        prime[v] = False
        continue
    for j in range(2 * v, MAX, v):
        prime[j] = False
    counted.add(v)

ans = 0
for v in X:
    ans += int(prime[v])
print(ans)
Problem p02642 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
a.sort()
flag = False
b = [False] * 1000010
for i in range(n - 1):
    if a[i] == a[i + 1]:
        for j in range(a[i], 1000010, a[i]):
            b[j] = True

ans = 0
for i in range(n):
    if not b[a[i]]:
        ans += 1
        for j in range(a[i], 1000010, a[i]):
            b[j] = True

print(ans)


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a.sort()
flag = False
b = [False] * 1000010
for i in range(n - 1):
    if a[i] == a[i + 1]:
        if b[a[i]]:
            continue
        for j in range(a[i], 1000010, a[i]):
            b[j] = True

ans = 0
for i in range(n):
    if not b[a[i]]:
        ans += 1
        for j in range(a[i], 1000010, a[i]):
            b[j] = True

print(ans)
Problem p02642 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
a = list(map(int, input().split()))

xs = [0] * (2 * (10**5) + 1)

for v in a:
    if xs[v] == 0:
        for j in range(v, len(xs), v):
            xs[j] += 1
    else:
        xs[v] += 1
print(len(list(filter(lambda v: xs[v] == 1, a))))


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

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

for v in a:
    if xs[v] == 0:
        for j in range(v, len(xs), v):
            xs[j] += 1
    else:
        xs[v] += 1
print(len(list(filter(lambda v: xs[v] == 1, a))))
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

bool divide[200005] = {};
vector<int> a;
int n;

int main(void) {

  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  for (int i = 0; i < n; i++) {
    int in;
    cin >> in;
    a.push_back(in);
  }

  sort(a.begin(), a.end());

  int count = 0;

  for (int i = 0; i < n; i++) {
    if (divide[a[i]] == false) {
      count++;
      for (int j = 1; j * a[i] <= a[n - 1]; j++) {
        divide[j * a[i]] = true;
      }
      if ((i != n - 1) && a[i + 1] == a[i])
        count--;
    }
  }

  cout << count << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

bool divide[1000005] = {};
vector<int> a;
int n;

int main(void) {

  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  for (int i = 0; i < n; i++) {
    int in;
    cin >> in;
    a.push_back(in);
  }

  sort(a.begin(), a.end());

  int count = 0;

  for (int i = 0; i < n; i++) {
    if (divide[a[i]] == false) {
      count++;
      for (int j = 1; j * a[i] <= a[n - 1]; j++) {
        divide[j * a[i]] = true;
      }
      if ((i != n - 1) && a[i + 1] == a[i])
        count--;
    }
  }

  cout << count << endl;

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 200000
#define M 100001
int n;
vector<int> u(M, 0);
vector<int> a(N);

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(void) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    u[a[i]]++;
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    bool can = true;
    auto v = divisor(a[i]);
    for (auto vi : v) {
      if (vi == a[i] && u[vi] == 1)
        continue;
      if (u[vi])
        can = false;
    }
    if (can)
      res++;
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 200000
#define M 1001001
int n;
vector<int> u(M, 0);
vector<int> a(N);

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(void) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    u[a[i]]++;
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    bool can = true;
    auto v = divisor(a[i]);
    for (auto vi : v) {
      if (vi == a[i] && u[vi] == 1)
        continue;
      if (u[vi])
        can = false;
    }
    if (can)
      res++;
  }
  cout << res << endl;
  return 0;
}
Problem p02642 (C++)

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

int a[200005], vis[200005], flag[1000005];

void slove(int x) {
  for (int i = 2;; i++) {
    int t = x * i;
    if (t > 1e6)
      return;
    flag[t] = 1;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (vis[a[i]] == 0)
      slove(a[i]);
    else
      flag[a[i]] = 1;
    vis[a[i]] = 1;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (flag[a[i]] == 0)
      ans++;
  }
  cout << ans << '\n';
  return 0;
}


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

int a[200005], vis[1000005], flag[1000005];

void slove(int x) {
  for (int i = 2;; i++) {
    int t = x * i;
    if (t > 1e6)
      return;
    flag[t] = 1;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (vis[a[i]] == 0)
      slove(a[i]);
    else
      flag[a[i]] = 1;
    vis[a[i]] = 1;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (flag[a[i]] == 0)
      ans++;
  }
  cout << ans << '\n';
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int SetInput(vector<int> &vec) {
  int size;
  cin >> size;
  for (int i = 0; i < size; ++i) {
    int value;
    cin >> value;
    vec.at(i) = value;
  }
  vec.resize(size);
  return size;
}

int Calc(vector<int> &input) {
  // ふるい
  const int max = 1000000;
  int sieve[max + 1] = {0};

  sort(input.begin(), input.end());

  for (const int &i : input) {
    // cout << "i : " <<  i << endl;
    ++sieve[i];
  }

  int count = 0;

  for (const int &i : input) {
    const int &s = sieve[i];

    if (s == 1) {
      ++count;
    }
    if (s > 0) {
      const int maxMul = max / i;
      for (int mul = 2; i * mul <= max; ++mul) {
        sieve[i * mul] = -1;
      }
    }
  }

  return count;
}

int main() {
  const int N = 200000;

  vector<int> input(N);

  SetInput(input);

  int ans = Calc(input);

  cout << ans << endl;

  return 0;
}

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

using namespace std;

int SetInput(vector<int> &vec) {
  int size;
  cin >> size;
  for (int i = 0; i < size; ++i) {
    int value;
    cin >> value;
    vec.at(i) = value;
  }
  vec.resize(size);
  return size;
}

int Calc(vector<int> &input) {
  // ふるい
  const int max = 1000000;
  int sieve[max + 1] = {0};

  sort(input.begin(), input.end());

  for (const int &i : input) {
    // cout << "i : " <<  i << endl;
    ++sieve[i];
  }

  int count = 0;

  for (const int &i : input) {
    const int &s = sieve[i];

    if (s == 1) {
      ++count;
    }
    if (s > 0) {
      const int maxMul = max / i;
      for (int mul = 1; i * mul <= max; ++mul) {
        sieve[i * mul] = -1;
      }
    }
  }

  return count;
}

int main() {
  const int N = 200000;

  vector<int> input(N);

  SetInput(input);

  int ans = Calc(input);

  cout << ans << endl;

  return 0;
}