alexjercan/bugnet huggingface dataset:
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define F(i, j, k) fill(i[0], i[0] + j * j, k)
#define P(p) cout << (p) << endl;
#define EXIST(s, e) ((s).find(e) != (s).end())
#define INF 1 << 25
#define pb push_back
using namespace std;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef long long ll;

bool *isprime;

bool isPrime(int n) {
  if (n == 1) {
    return false;
  }
  if (n == 2) {
    return true;
  }
  if (n > 2 && n % 2 == 0) {
    return false;
  }
  int i = 3;
  while (i <= (int)sqrt(n)) {
    if (n % i == 0) {
      return false;
    }
    i += 2;
  }
  return true;
}

int main() {
  int count = 0;
  int n, num;
  cin >> n;
  rep(i, n) {
    cin >> num;
    if (isPrime(num)) {
      count++;
    }
  }
  P(count);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define F(i, j, k) fill(i[0], i[0] + j * j, k)
#define P(p) cout << (p) << endl;
#define EXIST(s, e) ((s).find(e) != (s).end())
#define INF 1 << 25
#define pb push_back
using namespace std;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef long long ll;

bool isPrime(int n) {
  REP(i, 2, n) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

int main() {
  int count = 0;
  int n, num;
  cin >> n;
  rep(i, n) {
    cin >> num;
    if (isPrime(num)) {
      count++;
    }
  }
  P(count);
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;

bool isPrime(int n) {
  if (n <= 1)
    return false;
  for (int i = 2, l = (int)sqrt(n); i <= l; ++i) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}
int main() {
  int n;
  cin >> n;
  int num = 0;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    num += isPrime(x);
  }
  cout << num << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

bool *sieve;
int limit;
void makeSieve() {
  fill_n(sieve, limit + 1, true);
  sieve[0] = sieve[1] = false;
  for (int i = 4; i < limit + 1; i += 2) {
    sieve[i] = false;
  }
  for (int i = 3; i < sqrt(limit + 1); i += 2) {
    if (!sieve[i])
      continue;
    for (int j = i * i; j < limit + 1; j += i) {
      sieve[j] = false;
    }
  }
}
int main() {
  int n;
  cin >> n;

  vector<int> elements;
  limit = 0;
  while (n--) {
    int tmp;
    cin >> tmp;
    elements.push_back(tmp);
    limit = max(limit, tmp);
  }

  sieve = new bool[limit + 1];
  makeSieve();

  int num = 0;
  for (int i = 0; i < (int)elements.size(); ++i) {
    num += (int)sieve[elements[i]];
  }
  cout << num << endl;

  delete[] sieve;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const double EPS = 1e-10;
const ll MOD = 7 + (1e+9);

int main() {
  ll N = 100000000;
  vector<ll> v(10000000, 0);
  vector<bool> is_prime(N + 1, true);
  ll ii = 0;
  for (ll p = 2; p <= N; p++) {
    if (!is_prime[p])
      continue;
    // p の倍数を全て消す。
    for (ll k = p * 2; k <= N; k += p)
      is_prime[k] = false;
    v[ii] = p;
    ii++;
  }
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ll z;
    cin >> z;
    if (is_prime[z])
      ans++;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define int ll
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (ll i = (ll)(b)-1; i >= a; --i)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define eb(val) emplace_back(val)
const double PI = acos(-1);
const double EPS = 1e-10;
const ll MOD = 1e9 + 7;
void cioacc() { // accelerate cin/cout
  cin.tie(0);
  ios::sync_with_stdio(false);
}
pair<vl, vb> eratosthenes_sieve(ll n) {
  vb is_prime(n + 1, true);
  vl prime;
  is_prime[0] = false;
  is_prime[1] = false;
  FOR(i, 2, n + 1) {
    if (!is_prime[i])
      continue;
    for (ll j = 2 * i; j <= n; j += i)
      is_prime[j] = false;
    prime.eb(i);
  }
  return {prime, is_prime};
}
signed main() {
  int n;
  cin >> n;
  vb isp = eratosthenes_sieve(1e8).second;
  int ans = 0;
  REP(i, n) {
    int a;
    cin >> a;
    if (isp[a])
      ans++;
  }
  cout << ans << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

const int P_MAX = 1e6;

int main() {
  vector<bool> isPrime(P_MAX + 1, true);
  vector<int> primes(P_MAX / 2, 0);
  isPrime[0] = isPrime[1] = 0;

  for (int i = 4; i <= P_MAX; i += 2) {
    isPrime[i] = false;
  }
  int limit = sqrt(P_MAX);
  for (int i = 3; i <= limit; i += 2) {
    if (!isPrime[i]) {
      continue;
    }
    for (int j = i * 2; j <= P_MAX; j += i) {
      isPrime[j] = false;
    }
  }

  primes[0] = 2;
  int n_p = 1;
  for (int i = 3; i <= P_MAX; i += 2) {
    if (isPrime[i]) {
      primes[n_p] = i;
      n_p++;
    }
  }

  int n, m;
  scanf("%d", &n);
  int count = 0;

  for (int i = 0; i < n; i++) {
    scanf("%d", &m);
    if (m <= P_MAX) {
      count += isPrime[m] ? 1 : 0;
    } else {
      int limit = sqrt(m);
      ;
      bool ok = true;
      for (int j = 0; j < n_p; j++) {
        if (primes[j] > limit) {
          break;
        }
        if (m % primes[j] == 0) {
          ok = false;
          break;
        }
      }
      if (ok) {
        count++;
      }
    }
  }
  printf("%d\\n", count);
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

const int P_MAX = 1e8;

int main() {
  vector<int> isPrime(P_MAX + 1, 1);
  isPrime[0] = isPrime[1] = 0;

  for (int i = 4; i <= P_MAX; i += 2) {
    isPrime[i] = 0;
  }
  int limit = sqrt(P_MAX);
  for (int i = 3; i <= limit; i += 2) {
    if (!isPrime[i]) {
      continue;
    }
    for (int j = i * 2; j <= P_MAX; j += i) {
      isPrime[j] = 0;
    }
  }

  int n, m;
  scanf("%d", &n);
  int count = 0;

  for (int i = 0; i < n; i++) {
    scanf("%d", &m);
    count += isPrime[m];
  }
  printf("%d\\n", count);
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <math.h>

using namespace std;

class PrimeNumbers {
public:
  int solve(int n, int a[]);
  bool isPrime(int x);
};

int PrimeNumbers::solve(int n, int a[]) {
  int result = 0;

  for (int i = 0; i < n; i++) {
    if (isPrime(a[i]))
      result++;
  }
  return result;
}

bool PrimeNumbers::isPrime(int x) {
  if (x == 2)
    return true;

  for (int i = 2; i <= sqrt(x); i++) {
    if ((x % i) == 0)
      return false;
  }
  return true;
}

int main(void) {
  int n;
  int a[10000];

  PrimeNumbers *pn = new PrimeNumbers();

  cin >> n;

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

  cout << pn->solve(n, a) << endl;

  delete pn;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

class PrimeNumbers {
public:
  int solve(int n, int a[]);
  bool isPrime(int x);
};

int PrimeNumbers::solve(int n, int a[]) {
  int result = 0;

  for (int i = 0; i < n; i++) {
    if (isPrime(a[i]))
      result++;
  }
  return result;
}

bool PrimeNumbers::isPrime(int x) {
  if (x == 2)
    return true;

  for (int i = 2; i < x; i++) {
    if ((x % i) == 0)
      return false;
  }
  return true;
}

int main(void) {
  int n;
  int a[10000];

  PrimeNumbers *pn = new PrimeNumbers();

  cin >> n;

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

  cout << pn->solve(n, a) << endl;

  delete pn;

  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string.h>
#include <string>
#include <vector>

#define N 1000000

using namespace std;

bool isPrime(int x) {
  if (x == 2)
    return true;
  if (x % 2 == 0)
    return false;

  for (int i = 3; i * i <= x; i += 2) {
    if (x % i == 0)
      return false;
  }

  return true;
}

int main() {
  int n, cnt = 0, input;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> input;

    if (isPrime(input))
      cnt++;
  }

  cout << cnt << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string.h>
#include <string>
#include <vector>

#define N 1000000

using namespace std;

bool arr[N];
int cnt = 0;

void Eratosthenes(int n);

int main() {
  int n, input;
  cin >> n;
  Eratosthenes(N);
  for (int i = 0; i < n; i++) {
    cin >> input;
    if (arr[input])
      cnt++;
  }

  cout << cnt << endl;

  return 0;
}

void Eratosthenes(int n) {
  for (int i = 0; i < n; i++) {
    arr[i] = 1;
  }
  for (int i = 2; i < sqrt(n); i++) {
    if (arr[i]) {
      for (int j = 0; i * (j + 2) < n; j++) {
        arr[i * (j + 2)] = 0;
      }
    }
  }
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define N 10010
vector<int> prime_list;

void seive() {
  int prime[N];
  memset(prime, 1, sizeof(prime));
  prime_list.clear();
  prime[0] = 0;
  prime[1] = 0;
  for (int i = 0; i * i <= N; i++) {
    if (prime[i]) {
      for (int j = 2 * i; j < N; j += i)
        prime[j] = 0;
    }
  }
  for (int i = 0; i < N; ++i)
    if (prime[i])
      prime_list.push_back(i);
}

int check_prime(int num) {
  if (num == 2)
    return 1;
  if (!num & 1)
    return 0;

  int state = 1;
  int size = prime_list.size();
  for (int i = 0; i < size && prime_list[i] * prime_list[i] <= num; ++i) {
    if (!(num % prime_list[i])) {
      return 0;
    }
  }
  return 1;
}

int main() {
  // FILE *inp = freopen("in.txt", "r", stdin);
  int n, num;
  int prime_cnt = 0;
  seive();
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> num;
    if (check_prime(num)) {
      prime_cnt++;
    }
  }
  cout << prime_cnt << endl;
  // fclose(inp);
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define N 100000002
int prime[N];

void seive() {
  memset(prime, 1, sizeof(prime));
  prime[0] = 0;
  prime[1] = 0;
  for (int i = 0; i * i <= N; i++) {
    if (prime[i]) {
      for (int j = 2 * i; j < N; j += i)
        prime[j] = 0;
    }
  }
}

int check_prime(int num) {
  if (num == 2)
    return 1;
  if (!num & 1)
    return 0;
  else
    return prime[num];
}

int main() {
  // FILE *inp = freopen("in.txt", "r", stdin);
  int n;
  cin >> n;
  int num[n];
  int prime_cnt = 0;
  seive();
  for (int i = 0; i < n; ++i) {
    cin >> num[i];
    if (check_prime(num[i]))
      prime_cnt++;
  }
  cout << prime_cnt << endl;
  // fclose(inp);
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;

int main() {
  int n, count = 0;
  cin >> n;
  while (n--) {
    bool prime = true;
    int k, endk;
    cin >> k;
    endk = (int)sqrt(k);
    for (int i = 2; i <= endk; i++) {
      if (k % i == 0) {
        prime = false;
        break;
      }
    }
    if (prime)
      count++;
  }
  cout << count << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;

bool prime[100000001];

int main() {
  int endi = (int)sqrt(100000000);
  for (int i = 2; i <= endi; i++) {
    if (!prime[i]) {
      for (int j = i + i; j <= 100000000; j += i) {
        prime[j] = true;
      }
    }
  }
  int n, count = 0;
  cin >> n;
  while (n--) {
    int k;
    cin >> k;
    if (!prime[k])
      count++;
  }
  cout << count << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

int main() {
  int n;
  std::cin >> n;
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    std::cin >> a;
    bool f = true;
    for (int j = 2; j * j <= a; ++j) {
      if (a % j == 0)
        f = false;
    }
    if (f)
      ans++;
  }
  std::cout << ans << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

int num[10000000] = {0};

void func(int n) {
  for (int i = 2; i <= n; ++i) {
    bool f = true;
    for (int j = 2; j * j <= i; ++j) {
      if (i % j == 0)
        f = false;
    }
    if (f)
      num[i] = 1;
    else
      num[i] = 0;
  }
}

int main() {
  int n;
  std::cin >> n;
  int ans = 0;
  func(10000000);
  for (int i = 0; i < n; ++i) {
    int a;
    std::cin >> a;
    if (num[a] == 1) {
      ans++;
    }
    // std::cout << num[i] << std::endl;
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <vector>

bool is_prime(int a);

int main() {
  int n;
  int cnt = 0;
  std::cin >> n;
  std::vector<int> a(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    bool flag = is_prime(a[i]);
    if (flag)
      cnt++;
  }
  std::cout << cnt << '\\n';
  return 0;
}

bool is_prime(int a) {
  if (a == 2) {
    return true;
  } else if (a == 0 || a == 1) {
    return false;
  }
  for (int i = 2; i <= std::sqrt(a); i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}


Error: -8

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

bool is_prime(int a);

int main() {
  int n;
  int cnt = 0;
  std::cin >> n;
  std::vector<int> a(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    bool flag = is_prime(a[i]);
    if (flag)
      cnt++;
  }
  std::cout << cnt << '\\n';
  return 0;
}

bool is_prime(int a) {
  for (int i = 0; i < std::sqrt(a); i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

bool is_prime(int n) {
  // naive program
  int p = 2;
  while (p * p <= n) {
    if (n % p == 0) {
      return false;
    }
    p++;
  }
  return true;
}

int main() {
  int N;
  int cnt = 0;

  cin >> N;
  for (int i = 0; i < N; i++) {
    int n;
    cin >> n;
    if (is_prime(n)) {
      cnt++;
    }
  }
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

bool is_prime(int n) {
  // naive program
  int p = 2;
  while (p < n) {
    if (n % p == 0) {
      return false;
    }
    p++;
  }
  return true;
}

int main() {
  int N;
  int cnt = 0;

  cin >> N;
  for (int i = 0; i < N; i++) {
    int n;
    cin >> n;
    if (is_prime(n)) {
      cnt++;
    }
  }
  cout << cnt << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

using ll = long long;
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

bool isPrime(int x) {
  if (x <= 1)
    return false;
  if (x % 2 == 0 && x != 2)
    return false;
  for (int i = 3; i * i <= x; i += 2) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}

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

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (isPrime(x)) {
      cnt++;
    }
  }

  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

using ll = long long;
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

bool isPrime(int x) {
  if (x <= 1)
    return false;
  if (x % 2 == 0 && x != 2)
    return false;
  for (int i = 3; i <= x / 2; i += 2) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (isPrime(x)) {
      cnt++;
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

using ll = long long;
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

bool isPrime(int x) {
  if (x == 1)
    return false;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (isPrime(x)) {
      cnt++;
    }
  }

  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

using ll = long long;
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

bool isPrime(int x) {
  if (x == 1)
    return false;
  for (int i = 2; i < x; i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (isPrime(x)) {
      cnt++;
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {

  int n, ans = 0;
  cin >> n;
  while (n--) {
    int a;
    cin >> a;
    int i = 2;
    while (i * i <= a && a % i)
      i++;
    if (a % i || a == 2)
      ans++;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
bool prime[20001];

int main() {
  prime[0] = prime[1] = 1;
  for (int i = 2; i * i <= 10000; i++)
    for (int j = 2; j <= 10000 / i && prime[i] == 0; j++)
      prime[j * i] = 1;

  int n, ans = 0;
  cin >> n;
  while (n--) {
    int a;
    cin >> a;
    if (!prime[a])
      ans++;
  }
  cout << ans << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>

using namespace std;
bool f(int a) {
  bool F = 1;
  for (int i = 2; i < sqrt(a * 1.0) + 1; i++) {
    if (a % i == 0 && a != 2)
      F = 0;
  }
  return F;
};

int main() {
  int n, x = 0;
  int a[10000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    if (f(a[i]))
      x++;
  }
  cout << x << endl;
  return 0;
}


Error: WA

Fixed submission:


#include <iostream>

using namespace std;
int main() {
  int n, x = 0;
  int a[10000], b[10000] = {0};
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 2; i <= n; i++) {
    if (b[i - 1] == 0) {
      for (int j = i * 2; j <= n; j += i) {
        if (b[j - 1] == 0)
          b[j - 1] = 1;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (b[a[i] - 1] == 0)
      x++;
  }
  cout << x << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int a, b[10000] = {0}, B[10000] = {0}, c = 0;
  cin >> a;
  for (int i = 0; i < a; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < a; i++) {
    if (b[i] == 2 || b[i] == 3 || b[i] == 5 || b[i] == 7 || b[i] == 11 ||
        b[i] == 13 || b[i] == 17 || b[i] == 19 || b[i] == 23 || b[i] == 29 ||
        b[i] == 31 || b[i] == 37 || b[i] == 41 || b[i] == 43 || b[i] == 47 ||
        b[i] == 53) {
      continue;
    } else if (b[i] % 2 == 0 || b[i] % 3 == 0 || b[i] % 5 == 0 ||
               b[i] % 7 == 0 || b[i] % 11 == 0 || b[i] % 13 == 0 ||
               b[i] % 17 == 0 || b[i] % 19 == 0 || b[i] % 23 == 0 ||
               b[i] % 29 == 0 || b[i] % 31 == 0 || b[i] % 37 == 0 ||
               b[i] % 41 == 0 || b[i] % 43 == 0 || b[i] % 47 == 0 ||
               b[i] % 53 == 0) {
      c++;
    } else {
      for (int j = 13; j < b[i] / 53; j++) {
        if (b[i] % j == 0) {
          c++;
          break;
        } else {
          j++;
        }
      }
    }
  }
  c = a - c;
  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int a, b[10000] = {0}, B[10000] = {0}, c = 0;
  cin >> a;
  for (int i = 0; i < a; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < a; i++) {
    if (b[i] == 2 || b[i] == 3 || b[i] == 5 || b[i] == 7 || b[i] == 11 ||
        b[i] == 13 || b[i] == 17 || b[i] == 19 || b[i] == 23) {
      continue;
    } else if (b[i] % 2 == 0 || b[i] % 3 == 0 || b[i] % 5 == 0 ||
               b[i] % 7 == 0 || b[i] % 11 == 0 || b[i] % 13 == 0 ||
               b[i] % 17 == 0 || b[i] % 19 == 0 || b[i] % 23 == 0) {
      c++;
    } else {
      for (int j = 13; j < b[i] / 23; j++) {
        if (b[i] % j == 0) {
          c++;
          break;
        } else {
          j++;
        }
      }
    }
  }
  c = a - c;
  cout << c << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

int IsPrime(int n) {
  int i;

  if (n < 2)
    return 0;
  else if (n == 2)
    return 1;

  if (n % 2 == 0)
    return 0;

  for (i = 3; i * i <= n; i += 2)
    if (n % i == 0)
      return 0;
  return 1;
}

int main() {
  int n, count = 0;
  vector<int> N;

  cin >> n;

  for (int i = 0; i < n; ++i) {
    int j;
    cin >> j;

    bool flag = true;
    for (int k = 0; k < N.size(); ++k) {
      if (j == N[k]) {
        flag = false;
        break;
      }
    }

    if (flag)
      N.push_back(j);
  }

  for (int i = 0; i < n; ++i) {
    if (IsPrime(N[i]))
      ++count;
  }

  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, count = 0;
  vector<int> N;

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

  for (int i = 0; i < n; ++i) {
    bool flag = true;

    for (int j = 2; j < N[i]; ++j) {
      if (N[i] % j == 0) {
        flag = false;
      }
    }

    if (flag)
      ++count;
  }

  cout << count << endl;

  return 0;
}