alexjercan/bugnet huggingface dataset:
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int main() {
  bool f;
  int n, temp, ans = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &temp);

    f = true;
    if (temp % 2 == 0 && temp >= 4)
      f = false;
    for (int j = 3; j * j <= temp && f; j += 2)
      if (temp % j == 0)
        f = false;
    if (f)
      ans++;
  }
  printf("%d\\n", ans);
}

Error: 0

Fixed submission:

#include <stdio.h>
int main() {
  int n, temp, ans = 0;
  bool prime[1000001];
  for (int i = 0; i < 1000001; i++)
    prime[i] = true;
  for (int i = 4; i < 1000001; i += 2)
    prime[i] = false;
  for (int i = 3; i < 1001; i += 2) {
    if (prime[i]) {
      for (int j = i * i; j < 1000001; j += i)
        prime[j] = false;
    }
  }
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &temp);
    if (prime[temp])
      ans++;
  }
  printf("%d\\n", ans);
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using std::cin;
using std::cout;
using std::endl;

int main() {
  const int N_max = 10000;

  int N;
  int candidates[N_max];
  std::vector<int> primes;

  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> candidates[i];
  }
  int max_candidate = *std::max_element(candidates, candidates + N);
  int sqrt_max_candidate = static_cast<int>(sqrt(max_candidate));
  // cout << "max_candidate: " << max_candidate << endl;
  // cout << "sqrt_max_candidate: " << sqrt_max_candidate << endl;

  // list
  for (int cand = 2; cand <= sqrt_max_candidate; ++cand) {
    bool is_a_prime = true;
    for (const int &i : primes) {
      if (0 == cand % i) {
        is_a_prime = false;
        break;
      }
    }
    if (is_a_prime) {
      primes.push_back(cand);
    }
    // if (cand % 10000==0){
    //   cout << "checked: " << cand << endl;
    // }
  }

  // for(const int &i : primes){
  //   cout << i << endl;
  // }

  // count
  int num_of_primes = 0;
  for (int i = 0; i < N; ++i) {
    if (candidates[i] <= sqrt_max_candidate &&
        static_cast<bool>(
            std::binary_search(primes.begin(), primes.end(), candidates[i]))) {
      ++num_of_primes;
    } else if (candidates[i] > sqrt_max_candidate) {
      bool is_a_prime = true;
      for (const int &p : primes) {
        if (0 == candidates[i] % p) {
          is_a_prime = false;
          break;
        }
      }
      if (is_a_prime)
        ++num_of_primes;
    }
  }
  cout << num_of_primes << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

int main() {
  const int N_max = 10000;

  int N;
  int candidates[N_max];
  std::vector<int> primes;

  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> candidates[i];
  }
  int max_candidate = *std::max_element(candidates, candidates + N);

  // list
  for (int cand = 2; cand <= max_candidate; ++cand) {
    bool is_a_prime = true;
    for (const int &i : primes) {
      if (0 == cand % i) {
        is_a_prime = false;
        break;
      }
    }
    if (is_a_prime) {
      primes.push_back(cand);
    }
  }

  // count
  int num_of_primes = 0;
  for (int i = 0; i < N; ++i) {
    if (std::binary_search(primes.begin(), primes.end(), candidates[i])) {
      ++num_of_primes;
    }
  }
  cout << num_of_primes << endl;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long i64;
#define put(n) cout << (n) << "\\n"
#define FOR(i, num, N) for (int(i) = (num); (i) < (N); (i)++)
#define RFOR(i, num, N) for (int(i) = (num); (i) > (N); (i)--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

void Erarosthenes(vector<int> num, int N) {
  vector<bool> prime_number(1e8, false);
  FOR(i, 2, 1e8) prime_number[i] = true;
  FOR(i, 2, 1e4) {
    if (prime_number[i]) {
      for (int j = 2; i * j < 1e8; j++) {
        prime_number[i * j] = false;
      }
    }
  }

  int ans = 0;

  FOR(i, 0, N) {
    if (prime_number[num[i]])
      ans++;
  }

  put(ans);
}

int main() {
  int N;
  cin >> N;
  vector<int> num(N);
  FOR(i, 0, N) cin >> num[i];
  Erarosthenes(num, N);
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long i64;
#define put(n) cout << (n) << "\\n"
#define FOR(i, num, N) for (int(i) = (num); (i) < (N); (i)++)
#define RFOR(i, num, N) for (int(i) = (num); (i) > (N); (i)--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

void Erarosthenes(vector<int> num, int N) {
  vector<bool> prime_number(1e6, false);
  FOR(i, 2, 1e6) prime_number[i] = true;
  FOR(i, 2, 1e3) {
    if (prime_number[i]) {
      for (int j = 2; i * j < 1e6; j++) {
        prime_number[i * j] = false;
      }
    }
  }

  int ans = 0;

  FOR(i, 0, N) {
    if (prime_number[num[i]])
      ans++;
  }

  put(ans);
}

int main() {
  int N;
  cin >> N;
  vector<int> num(N);
  FOR(i, 0, N) cin >> num[i];
  Erarosthenes(num, N);
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

using namespace std;

bool is_prime(long long x) {
  if (2 > x)
    return false;

  if (2 == x)
    return true;

  if (0 == x % 2)
    return false;

  double sqrt_num = sqrt(x);

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

  return true;
}

int main() {
  int n, cnt = 0;
  long long tmp;

  cin >> n;

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

    if (is_prime(tmp))
      cnt++;
  }

  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

bool is_prime(long long x) {
  if (1 >= x) {
    return false;
  }

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

  return true;
}

int main() {
  int n, cnt = 0;
  long long tmp;

  cin >> n;

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

    if (is_prime(tmp)) {
      cnt++;
    }
  }

  cout << cnt << endl;

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

#include <iostream>
using namespace std;

int main() {
  int N;
  int x;
  int count = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x;
    for (int j = 2; j * j <= x; j++) {
      if (x % j == 0) {
        count++;
        break;
      }
    }
  }
  cout << N - count << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int N;
  int x;
  int count = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x;
    for (int j = 2; j < x / 2 + 1; j++) {
      if (x != 2) {
        if (x % j == 0) {
          count++;
          break;
        }
      }
    }
  }
  cout << N - count << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAX = (int)1e7;
bool prime[MAX + 1];

int main() {
  fill(prime, prime + MAX + 1, 1);
  for (int i = 2; i <= MAX; i++) {
    if (prime[i]) {
      for (int k = 2; i * k <= MAX; k++) {
        prime[i * k] = 0;
      }
    }
  }
  int n;
  int a;
  int cnt = 0;
  scanf("%d", &n);
  bool isPrime = 1;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    if (a <= (int)1e7) {
      cnt += prime[a];
    } else {
      int bd = sqrt(a) + 1;
      isPrime = 1;
      for (int i = 2; i <= bd; i++) {
        if (a % i == 0) {
          isPrime = 0;
          break;
        }
      }
      cnt += isPrime;
    }
  }
  cout << cnt << endl;
}

Error: MLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAX = (int)1e8;
bool prime[MAX + 1];

int main() {
  fill(prime, prime + MAX + 1, 1);
  for (int i = 2; i <= MAX; i++) {
    if (prime[i]) {
      for (int k = 2; i * k <= MAX; k++) {
        prime[i * k] = 0;
      }
    }
  }
  int n;
  int a;
  int cnt = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    cnt += prime[a];
  }
  cout << cnt << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

bool isPrime(int n) {
  if (n == 2) {
    return true;
  }
  if ((n & 1) == 0) {
    return false;
  }

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

  return (n != 1);
}

int main(void) {
  int n, count = 0;

  cin >> n;

  for (int i = 0; i < n; ++i) {
    int p;

    cin >> p;

    if (isPrime(p)) {
      ++count;
    }
  }

  cout << count << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

bool setPrime(int size, bool isPrime[]) {
  isPrime[0] = isPrime[1] = false;

  for (int i = 2; i < size; ++i) {
    isPrime[i] = true;
  }

  for (int i = 2; (i * i) < size; ++i) {
    if (!isPrime[i]) {
      continue;
    }

    for (int j = i + i; j < size; j += i) {
      isPrime[j] = false;
    }
  }
}

int main(void) {
  const int size = 100000001;
  int n, count = 0;
  bool *isPrime = new bool[size];

  setPrime(size, isPrime);

  cin >> n;

  for (int i = 0; i < n; ++i) {
    int p;

    cin >> p;

    if (isPrime[p]) {
      ++count;
    }
  }

  cout << count << endl;

  delete[] isPrime;

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

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

using namespace std;

bool isPrime(int x);

int main() {
  int n = 0;
  int a[10000] = {};
  int z = 0; // output

  cin >> n;
  for (int i = 0; i < 10000; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    if (isPrime(a[i]) == true) {
      z++;
    }
  }
  cout << z << endl;
}
bool isPrime(int x) {
  if (x == 2)
    return true;
  if (x < 2 || x % 2 == 0)
    return false;
  int i = 3;
  while (i <= sqrt(x)) {
    if (x % i == 0)
      return false;
    i = i + 2;
  }
  return true;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

bool isPrime(int x);

int main() {
  int n = 0;
  int a[10000] = {};
  int z = 0; // output

  cin >> n;
  for (int i = 0; i < 10000; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    if (isPrime(a[i]) == true) {
      z++;
    }
  }
  cout << z << endl;
}
bool isPrime(int x) {
  if (x <= 1)
    return false;
  for (int i = 2; i < x - 1; i++) {
    if (x == 2 || x == 3) {
      return true;
    }
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\\  =  /O
               ____/`---'\\____
             .'  \\\\|     |//  `.
            /  \\\\|||  :  |||//  \\
           /  _||||| -:- |||||-  \\
           |   | \\\\\\  -  /// |   |
           | \\_|  ''\\---/''  |   |
           \\  .-\\__  `-`  ___/-. /
         ___`. .'  /--.--\\  `. . __
      ."" '<  `.___\\_<|>_/___.'  >'"".
     | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |
     \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /
======`-.____`-.___\\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          fozubaoyou    no BUGs alive
*/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

bool isPrime(int x) {
  if (x == 2)
    return true;
  if (x < 2 || x % 2 == 0)
    return false;
  int i = 3;
  while (i <= (int)sqrt(double(x))) {
    if (x % i == 0)
      return false;
    i++;
  }
  return true;
}

int main() {
  int n, r = 0;
  cin >> n;
  REP(i, n) {
    int x;
    cin >> x;
    if (isPrime(x))
      r++;
  }
  cout << r << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, n) FOR(i, 0, n)
using namespace std;
typedef vector<int> vi;

int main() {
  int n;
  cin >> n;
  vi p(n);
  REP(i, n) cin >> p[i];
  int cnt = 0;
  REP(i, n) {
    bool flg = false;
    FOR(j, 2, p[i]) {
      if (p[i] % j == 0) {
        flg = true;
        break;
      }
    }
    if (!flg)
      cnt++;
  }
  cout << cnt << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <math.h>
using namespace std;

int main() {
  int n, a, total = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (a % 2 == 1 && a > 0) {
      int k = sqrt(a);
      for (int j = 3; j <= k; j++)
        if (a % j == 0) {
          k = -1;
          break;
        }
      if (k != -1)
        total++;
    } else if (a == 2)
      total++;
  }
  cout << total << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int main() {
  int n, a[100000000], total = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] % 2 == 1 && a[i] > 0) {
      for (int j = 0; j < sqrt(a[i]); j++)
        if (a[i] % j == 0) {
          total++;
          break;
        }
    }
  }
  cout << total << endl;

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define fr first
#define sc second
#define INF 100000001
using namespace std;
bitset<INF> Prime;
int main() {
  Prime[1] = true;
  for (int i = 3; i <= sqrt(INF); i += 2) {
    if (!Prime[i]) {
      for (int j = 3; i * j < INF; j += 2) {
        Prime[j * i] = true;
      }
    }
  }
  int n, a, cnt = 0;
  cin >> n;
  while (n--) {
    cin >> a;
    if (a == 2)
      cnt++;
    else if (a % 2 == 1 && !Prime[a])
      cnt++;
  }
  cout << cnt << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define fr first
#define sc second
#define INF 100000001
using namespace std;
bool Prime[INF] = {true, true};
int main() {
  for (int i = 3; i <= sqrt(INF); i += 2) {
    if (!Prime[i]) {
      for (int j = 3; i * j < INF; j += 2) {
        Prime[j * i] = true;
      }
    }
  }
  int n, a, cnt = 0;
  cin >> n;
  while (n--) {
    cin >> a;
    if (a == 2)
      cnt++;
    else if (a % 2 == 1 && !Prime[a])
      cnt++;
  }
  cout << cnt << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

void MakePrimeArray(bool A[], int n) {
  A[0] = A[1] = false;
  for (int i = 2; i < n; i++) {
    A[i] = true;
  }

  for (int i = 2; i * i < n; i++) {
    if (A[i]) {
      for (int j = i * i; j < n; j += i) {
        A[j] = false;
      }
    }
  }
}

int main() {
  /*
  long int n=10000000;

  bool *primearray = new bool[n];

        MakePrimeArray(primearray,n);
*/
  int num;
  cin >> num;
  int input = 0;
  int solve = 0;
  for (int i = 0; i < num; i++) {
    cin >> input;
    /*if(primearray[input]){
      solve++;
    }*/
    if (input != 1) {
      for (int j = 2; j * j <= input; j++) {
        if (input % j == 0) {
          solve--;
          break;
        }
      }
      solve++;
    }
  }
  cout << solve << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

void MakePrimeArray(bool A[], int n) {
  A[0] = A[1] = false;
  for (int i = 2; i < n; i++) {
    A[i] = true;
  }

  for (int i = 2; i * i < n; i++) {
    if (A[i]) {
      for (int j = i * i; j < n; j += i) {
        A[j] = false;
      }
    }
  }
}

int main() {
  long int n = 10000000;

  bool *primearray = new bool[n];

  MakePrimeArray(primearray, n);

  int num;
  cin >> num;
  int input = 0;
  int solve = 0;
  for (int i = 0; i < num; i++) {
    cin >> input;
    if (primearray[input]) {
      solve++;
    }
  }
  cout << solve << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <bitset>
#include <iostream>
using namespace std;
int main() {
  const int TMAX = 10000;
  std::bitset<TMAX> flg;
  int n;
  int primes = 0;
  for (int i = 2; i < TMAX; i++) {
    if (!flg.test(i)) {
      for (int j = 2; i * j < TMAX; ++j) {
        flg.set(i * j);
      }
    }
  }
  cin >> n;
  while (n-- > 0) {
    int x;
    cin >> x;
    if (x <= TMAX) {
      if (!flg.test(x)) {
        primes++;
      }
    } else {
      bool isnotprime = false;
      if ((x % 2) != 0) {
        for (int i = 3; i < TMAX; i += 2) {
          if (!flg.test(i)) {
            if ((x % i) == 0) {
              isnotprime = true;
              break;
            }
          }
        }
        if (!isnotprime) {
          primes++;
        }
      }
    }
  }
  cout << primes << endl;
}

Error: 0

Fixed submission:

#include <bitset>
#include <iostream>
using namespace std;
int main() {
  std::bitset<10000> flg;
  int n;
  int primes = 0;
  for (int i = 2; i < 10000; i++) {
    if (!flg.test(i)) {
      for (int j = 2; i * j < 10000; ++j) {
        flg.set(i * j);
      }
    }
  }
  cin >> n;
  while (n-- > 0) {
    int x;
    cin >> x;
    if (!flg.test(x)) {
      primes++;
    }
  }
  cout << primes << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int arr[10005];

int tot = 0;
bool isprime(int);
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  for (int i = 0; i < n; i++) {
    if (isprime(arr[i]))
      tot++;
  }

  cout << tot << endl;

  return 0;
}

bool isprime(int num) {
  if (num % 2 == 0 && num != 2)
    return false;
  for (int i = 3; i <= int(sqrt(double(num))); i += 2) {
    if (num % i == 0)
      return false;
  }
  return true;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int arr[10005];

int tot = 0;
bool isprime(int);
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  for (int i = 0; i < n; i++) {
    if (isprime(arr[i]))
      tot++;
  }

  cout << tot << endl;

  return 0;
}

bool isprime(int num) {
  if (num % 2 == 0 && num != 2)
    return false;
  for (int i = 3; i <= num / 2; i += 2) {
    if (num % i == 0)
      return false;
  }
  return true;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

bool isprime(long n) {
  if (n == 2)
    return true;
  if (n < 2 || n % 2 == 0)
    return false;
  double root_n = std::sqrt((double)n);
  for (int i = 3; i <= root_n; i += 2)
    if (n % i == 0)
      return false;
  return true;
}

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

  int n, a, cnt = 0;
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    std::cin >> a;
    if (isprime(a))
      ++cnt;
  }
  std::cout << cnt << std::endl;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

bool isprime(long n) {
  if (n == 2)
    return true;
  if (n < 2)
    return false;
  double root_n = std::sqrt((double)n);
  for (int i = 2; i <= root_n; ++i)
    for (int j = i * i; j <= n; j += i)
      if (n % j == 0)
        return false;
  return true;
}

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

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

#include <cmath>
#include <iostream>
using namespace std;
int main() {

  int n, a, b, c = 0, d, e;

  cin >> n;

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

    cin >> a;

    b = 0;
    e = a % 2;

    if (e == 0 && a != 2)
      continue;

    if (a == 2) {
      c++;
    } else {

      for (int j = 1; j <= sqrt(a); j += 2) {

        d = a % j;

        if (d == 0) {
          b++;
        }
        if (b == 2)
          break;
      }

      if (b == 1) {
        c++;
      }
    }
  }

  cout << c << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {

  int n, a, b, c = 0, d, e;

  cin >> n;

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

    cin >> a;

    b = 0;
    e = a % 2;

    if (e == 0 && a != 2)
      continue;

    if (a == 2) {
      c++;
    } else {

      for (int j = 1; j <= a / 2; j += 2) {

        d = a % j;

        if (d == 0) {
          b++;
        }
        if (b == 2)
          break;
      }

      if (b == 1) {
        c++;
      }
    }
  }

  cout << c << endl;

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

#include <stdio.h>
#define N 10000
// 1 mean prime 0 not
// prime number: 2,3,5,7
int isprime(int target) {
  if (target % 2 == 0) {
    return 0;
  }
  for (int i = 3; i * i <= target; i++) {
    if (target % i == 0) {
      return 0;
    }
  }

  return 1;
}

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

  int res = 0;
  int index;
  for (int i = 0; i < n; i++) {
    scanf("%d", &index);
    if (isprime(index))
      res++;
    if (index == 2)
      res++;
  }
  printf("%d\\n", res);
  return 0;
}


Error: MLE

Fixed submission:

#include <stdio.h>
#define N 100000000
int a[N + 1]; // 1 mean prime 0 not
// prime number: 2,3,5,7
int isprime(int a[], int target, int n) {
  if (a[target] == 1)
    return 1;
  if (a[target] == 0)
    return 0;
  if (target % 2 == 0) {
    a[target] = 0;
    return 0;
  }
  for (int i = 3; i * i <= target; i++) {
    if (target % i == 0) {
      a[target] = 0;
      return 0;
    }
  }
  a[target] = 1;
  return 1;
}
void print_array(int a[], int n) {
  int i = 0;
  for (i = 0; i < n - 1; i++)
    printf("%d ", a[i]);
  printf("%d\\n", a[i]);
}
int main() {
  int n = 0;
  scanf("%d", &n);

  a[0] = 0;
  a[1] = 0;
  for (int i = 2; i <= N; i++) {
    a[i] = -1; // not checking
  }

  for (int i = 2; i <= N; i++) {

    if (isprime(a, i, N)) {
      for (int j = 2; j * i <= N; j++) {
        a[j * i] = 0;
      }
    }
  }
  // print_array(a,n);
  int res = 0;
  int index;
  for (int i = 0; i < n; i++) {
    scanf("%d", &index);
    res += a[index];
  }
  printf("%d\\n", res);
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// returns 1 instead of true
// also 0 - false
inline int isPrime(const int &n) {
  if (n == 2) {
    return 1;
  }

  if (!(n & 1)) {
    return 0;
  }

  float root = std::sqrt(n);
  for (int div = 3; div <= root; div += 2) {
    if (!(n % div)) {
      return 0;
    }
  }
  return 1;
}

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

  std::vector<int> vec(n);
  for (auto &e : vec) {
    std::cin >> e;
  }

  int numOfPrime = 0;
  for (const auto &e : vec) {
    numOfPrime += isPrime(e);
  }

  std::cout << numOfPrime << '\\n';
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

// returns 1 instead of true
// also 0 - false
inline int isPrime(const int &n) {
  if (n == 2) {
    return 1;
  }

  if (!(n & 1)) {
    return 0;
  }

  int half = n / 2;
  for (int div = 3; div <= half; div += 2) {
    if (!(n % div)) {
      return 0;
    }
  }
  return 1;
}

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

  std::vector<int> vec(n);
  for (auto &e : vec) {
    std::cin >> e;
  }

  int numOfPrime = 0;
  for (const auto &e : vec) {
    numOfPrime += isPrime(e);
  }

  std::cout << numOfPrime << '\\n';
}