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

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

#define intmax 1299710
int isprime[intmax];
void makeprime() {
  REP(i, intmax) if (i % 2 != 0) isprime[i] = true;
  isprime[1] = false;
  isprime[2] = true;
  for (ll int i = 3; i < sqrt(intmax); i += 2) {
    for (ll int j = i + 2; j < intmax; j += 2) {
      if (j % i == 0)
        isprime[j] = false;
    }
  }
}
int main() {
  int cnt = 0;
  int n;
  cin >> n;
  REP(i, n) {
    ll int tmp;
    cin >> tmp;
    if (tmp == 1)
      continue;
    if (tmp == 2 || tmp == 3) {
      cnt++;
      continue;
    }
    if (tmp % 2 == 0)
      continue;
    bool flag = true;
    for (ll int j = 3; j < sqrt(tmp) + 1; j += 2) {
      if (tmp % j == 0) {
        flag = false;
      }
    }
    if (flag)
      cnt++;
  }
  cout << cnt << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

#define intmax 100000003
bool isprime[intmax];
void makeprime(int datasize) { /*??¨???????????????????????????*/
  REP(i, datasize) if (i % 2 != 0) isprime[i] = true;
  isprime[1] = false;
  isprime[2] = true;
  for (ll int i = 3; i < sqrt(datasize); i += 2) {
    for (ll int j = i + 2; j < datasize; j += 2) {
      if (j % i == 0)
        isprime[j] = false;
    }
  }
}

int main() {
  int n;
  cin >> n;
  int cnt = 0;
  ll int mmax = -1;
  ll int data[10001];
  REP(i, n) {
    ll int tmp;
    cin >> tmp;
    data[i] = tmp;
    mmax = max(mmax, tmp);
  }
  makeprime(mmax + 1);
  REP(i, n) {
    if (isprime[data[i]])
      cnt++;
  }
  cout << cnt << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int co = 0;
  for (int i = 0; i < n; i++) {
    int nu;
    cin >> nu;
    int f = 0;
    for (int i = 2; i * i <= nu; i++) {
      if (nu % i == 0) {
        f = 1;
        break;
      }
    }
    if (!f)
      co++;
  }
  cout << co << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int pri[100000001] = {};
  pri[2] = 1;
  int f = 0;
  for (int i = 3; i < 100000001; i++) {
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        f = 1;
        break;
      }
    }
    if (f == 0)
      pri[i] = 1;
  }
  int n;
  cin >> n;
  int co = 0;
  for (int i = 0; i < n; i++) {
    int nu;
    cin >> nu;
    if (pri[nu])
      co++;
  }
  cout << co << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

const int MAX_N = 10000;

int main() {
  int n = 0;
  int numbers[MAX_N] = {0};

  std::cin >> n;

  int count = n;

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

    std::cin >> x;

    if (x == 2) {
      continue;
    }

    if ((x > 2) && ((x % 2) == 0)) {
      count--;
    } else {
      numbers[i] = x;
    }
  }

  for (int i = 0; i < n; i++) {
    if (numbers[i] == 0) {
      continue;
    }

    int v = numbers[i];
    int max = std::sqrt(v);

    for (int j = 3; j <= max; j += 2) {
      if ((v % j) == 0) {
        count--;
        numbers[i] = 0;

        break;
      }
    }
  }

  // for (int i = 0; i < n; i++) {
  //   std::cout << numbers[i] << std::endl;
  // }

  std::cout << count << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

const int MAX_N = 10000;

int main() {
  int n = 0;
  int numbers[MAX_N] = {0};

  std::cin >> n;

  int count = n;
  int max = 0;

  for (int i = 0; i < n; i++) {
    std::cin >> numbers[i];

    if ((numbers[i] >= 4) && ((numbers[i] % 2) == 0)) {
      numbers[i] = -1;
      count--;
    }

    if (numbers[i] > max) {
      max = numbers[i];
    }
  }

  for (int i = 2; i < (max / 2); i++) {
    for (int j = 0; j < n; j++) {
      if ((numbers[j] < 0) || (numbers[j] < i)) {
        continue;
      }

      int step = i;

      for (int k = i + i; k <= max; k += step) {
        if (numbers[j] == k) {
          numbers[j] = -1;
          count--;
        }
      }
    }
  }

  // for (int i = 0; i < n; i++) {
  //   std::cout << numbers[i] << std::endl;
  // }

  std::cout << count << std::endl;

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

#include <iostream>
using namespace std;
int main() {
  long n;
  long a[10000];
  int ans = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    int check = 0;
    for (int j = 1; j * j <= a[i]; ++j) {
      if (a[i] % j == 0)
        check++;
    }
    if (check == 1)
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  long n;
  long a[10000];
  int ans = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    int check = 0;
    for (int j = 1; j <= a[i]; ++j) {
      if (a[i] % j == 0)
        check++;
    }
    if (check == 2)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <stdbool.h>
#include <stdio.h>

bool prime(int x) {
  if (x <= 1) {
    return false;
  }
  if (x == 2) {
    return true;
  }

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

int main(void) {
  int n;
  scanf("%d", &n);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    if (prime(x)) {
      ans++;
    }
  }
  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <stdbool.h>
#include <stdio.h>

int main(void) {
  int n;
  scanf("%d", &n);
  int x[n];
  int ans = 0;
  int max = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &x[i]);
    if (x[i] > max) {
      max = x[i];
    }
  }
  bool prm[max + 1];
  prm[0] = false;
  prm[1] = false;
  prm[2] = true;
  for (int i = 2; i < max + 1; i++) {
    prm[i] = true;
    int j = 2;
    while (j * j <= i) {
      if (prm[j]) {
        if (i % j == 0) {
          prm[i] = false;
          break;
        }
      }
      j++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (prm[x[i]]) {
      ans++;
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX_INPUT_SIZE 10240

using namespace std;

int main(int argc, char *argv[]) {
  char input[MAX_INPUT_SIZE];

  int lines = atoi(fgets(input, sizeof(input), stdin));
  int *array = new int[lines];
  for (int i = 0; i < lines; i++) {
    array[i] = atoi(fgets(input, sizeof(input), stdin));
  }

  sort(array, array + lines);
  int max = array[lines - 1];
#if 0
    for (int i = 0; i < lines; i++) {
        printf("%d\\n", array[i]);
    }
#endif
  double sq = sqrt(max);

  for (int i = 0; i < lines; i++) {
    for (int j = 2; j < sq; j++) {
      if (array[i] != j && array[i] % j == 0) {
        array[i] = -1;
      }
    }
  }

  int num = 0;
  for (int i = 0; i < lines; i++) {
    if (array[i] > 0) {
      num++;
    }
  }

  printf("%d\\n", num);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <stdlib.h>

#define MAX_INPUT_SIZE 10240

using namespace std;

int main(int argc, char *argv[]) {
  char input[MAX_INPUT_SIZE];

  int lines = atoi(fgets(input, sizeof(input), stdin));
  int *array = new int[lines];
  for (int i = 0; i < lines; i++) {
    array[i] = atoi(fgets(input, sizeof(input), stdin));
  }

  sort(array, array + lines);
  int max = array[lines - 1];

  for (int i = 0; i < lines; i++) {
    if (array[i] != -1) {
      for (int j = 2; j < max; j++) {
        if (array[i] % j == 0 && array[i] != j) {
          array[i] = -1;
        }
      }
    }
  }

  int num = 0;
  for (int i = 0; i < lines; i++) {
    if (array[i] > 0) {
      num++;
    }
  }

  printf("%d\\n", num);

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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int check(int x) {
  if (x == 2)
    return 1;
  if (x < 2 || x % 2 == 0)
    return 0;
  for (int i = 3; i <= sqrt(x); i += 2) {
    if (x % i == 0)
      return 0;
  }
  return 1;
}
int main() {
  int n;
  int ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    ans += check(a);
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
bool memo[100000000] = {};
int main() {
  memo[1] = 1;
  for (int i = 2; i < 100000000 / 2; i++) {
    if (memo[i] == 0) {
      for (int k = 2 * i; k < 100000000; k += i) {
        memo[k] = 1;
      }
    }
  }
  int ans = 0;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (memo[a]) {
    } else
      ans++;
  }
  cout << ans << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cassert>
#include <iostream>
#include <math.h>
// assert( ) ??????assert.h
// ????????則??????????????即????????贈?即??????????????????????assert( )
// ???????????????????????其????????????????????????????????????expression
// ????????????????????属????即??????即??????????????? stderr
// ??????????????????????????即????????????abort( )
// ??????????????????????????属???????????属?????????????????????expression
// ??????????????\\???????????属???assert( ) ???????????????????????????

using namespace std;

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

int main(void) {
  int n, x;
  int cnt = 0;
  cin >> n;
  assert(1 <= n && n <= 10000);
  for (int i = 0; i < n; i++) {
    cin >> x;
    assert(2 <= x && x <= 1000000000);
    if (isPrime(x))
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#define MAX 100000000

using namespace std;

bool prime[MAX];

void isPrime(void) {

  fill(prime, prime + MAX, true);
  prime[0] = prime[1] = false;

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

int main(void) {

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

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

int countPrimeNumbers(int nums[], int n);

int main(void) {
  int n, nums[10000];
  cin >> n;

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

  int count = countPrimeNumbers(nums, n);

  cout << count << endl;

  return 0;
}

int countPrimeNumbers(int nums[], int n) {
  int count = 0;

  for (int i = 0; i < n; i++) {
    int j = 2;
    for (j = 2; j * j <= nums[i]; j++) {
      if (nums[i] % j == 0)
        break;
    }
    if (j * j > nums[i])
      count++;
  }

  return count;
}

Error: TLE

Fixed submission:

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

int countPrimeNumbers(int nums[], int n);

int main(void) {
  int n, nums[10000];
  cin >> n;

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

  int count = countPrimeNumbers(nums, n);

  cout << count << endl;

  return 0;
}

int countPrimeNumbers(int nums[], int n) {
  int count = 0;

  for (int i = 0; i < n; i++) {
    if (nums[i] == 2) {
      count++;
      continue;
    }

    int j = 0;
    for (j = 3; j <= nums[i]; j += 2) {
      if (nums[i] % j == 0)
        break;
    }
    if (j == nums[i])
      count++;
  }

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

#include <iostream>
using namespace std;
unsigned int a, n;
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 <= n / i; i += 2)
    if (n % i == 0)
      return 0;
  return 1;
}
int main() {
  int temp = 0, flag = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (IsPrime(a) == 1)
      temp++;
  }
  cout << temp << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
unsigned int a, n;
int main() {
  int temp = 0, flag = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    for (int i = 2; i < a; i++) {
      if (a % i == 0) {
        flag = 1;
        break;
      }
    }
    if (flag == 0)
      temp++;
    flag = 0;
  }
  cout << temp << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  const int v = std::sqrt(n);
  for (int i = 3; i <= v; i += 2) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int main(int argc, char *argv[]) {
  int n = 0;
  std::cin >> n;
  std::vector<int> list(n);
  for (int i = 0; i < n; ++i)
    std::cin >> list[i];

  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    bool prime = true;
    prime = isPrime(list[i]);
    if (prime)
      ++cnt;
  }

  std::cout << cnt << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

int main(int argc, char *argv[]) {
  int n = 0;
  std::cin >> n;
  std::vector<int> list(n);
  for (int i = 0; i < n; ++i)
    std::cin >> list[i];

  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    bool prime = true;
    for (int j = list[i] - 1; j > 1; --j) {
      if (list[i] % j == 0)
        prime = false;
    }
    if (prime)
      ++cnt;
  }

  std::cout << cnt << std::endl;

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

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

int main(void) {
  int datanum;
  int notprimenum;
  int data;
  int divnum;

  notprimenum = 0;

  cin >> datanum;

  for (int i = 0; i < datanum; i++) {
    cin >> data;
    if (data == 1) {
      notprimenum++;
      continue;
    } else if (data == 2) {
      continue;
    } else if (data == 3) {
      continue;
    } else {

      if (data % 2 == 0) {
        notprimenum++;
        continue;
      }

      for (divnum = 3; divnum <= (int)sqrt(data); divnum += 2) {
        if (data % divnum == 0) {
          notprimenum++;
          break;
        }
      }
    }
  }

  cout << datanum - notprimenum << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;

int main(void) {
  int datanum;
  int *databuff;
  int *primecheck;
  int maxnum;
  int primecnt;

  primecnt = 0;
  maxnum = 0;

  cin >> datanum;

  databuff = new int[datanum];

  for (int i = 0; i < datanum; i++) {
    cin >> databuff[i];
    if (databuff[i] > maxnum) {
      maxnum = databuff[i];
    }
  }

  primecheck = new int[maxnum + 1];

  for (int i = 2; i <= maxnum; i++) {
    primecheck[i] = 1;
  }

  for (int i = 2; i <= maxnum; i++) {
    if (primecheck[i] == 1) {
      for (int j = 2 * i; j <= maxnum; j++) {
        if (j % i == 0) {
          primecheck[j] = 0;
        }
      }
    }
  }

  for (int i = 0; i < datanum; i++) {
    if (primecheck[databuff[i]] == 1) {
      primecnt++;
    }
  }

  cout << primecnt << endl;

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

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define ELEM(array) (sizeof(array) / sizeof *(array))
#define MAX_N 100005
#define MS(m, v) memset(m, v, sizeof(m))
#define SAFE_FREE(ptr)                                                         \\
  if (ptr != NULL) {                                                           \\
    free(ptr);                                                                 \\
    ptr = NULL;                                                                \\
  }
typedef unsigned int UINT;
typedef long long ll;
typedef pair<int, int> P;

bool solve(int x) {
  int i = 3;
  if (x % 2 == 0)
    return false;
  while (i * i <= x) {
    if (x % i == 0) {
      return false;
    }
    i += 2;
  }
  return true;
}

int main() {
  int n;
  int ans = 0;
  int x;
  cin >> n;
  REP(i, n) {
    cin >> x;
    if (x == 2)
      ans++;
    else if (solve(x))
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define ELEM(array) (sizeof(array) / sizeof *(array))
#define MAX_N 100000005
#define SAFE_FREE(ptr)                                                         \\
  if (ptr != NULL) {                                                           \\
    free(ptr);                                                                 \\
    ptr = NULL;                                                                \\
  }
typedef unsigned int UINT;
#define ll long long
typedef pair<int, int> P;

bool prime[MAX_N];

void solve() {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  FOR(i, 2, (int)sqrt(MAX_N)) {
    if (prime[i] == true) {
      for (int j = 2; j * i <= 1000000; j++) {
        if (prime[i * j] == true)
          prime[i * j] = false;
      }
    }
  }
}

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

    int ans = 0;

    solve();
    REP(i, n + 1) {
      if (prime[i] == true) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    int sum = 0;
    int input[10000] = {0};
    // int max = 0;
    int now = 0;
    for (int i = 0; i < n; i++) {
      int temp;
      cin >> temp;
      int flag = 0;
      for (int j = 0; j < now; j++) {
        if (input[i] == temp) {
          flag++;
        }
      }
      if (flag == 0) {
        int flag2 = 0;
        for (int j = 2; j <= sqrt(temp); j++) {
          if (temp % j == 0) {
            flag2++;
            break;
          }
        }
        if (flag2 == 0)
          sum++;
      }
    }
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

#include <fstream>
#include <iostream>

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    int input[10000] = {0};
    int max = 0;
    for (int i = 0; i < n; i++) {
      cin >> input[i];
      if (max < input[i])
        max = input[i];
    }

    bool *sosuu = new bool[60000000];
    for (int i = 0; i <= max; i++)
      sosuu[i] = 1;

    sosuu[0] = 0;
    for (int i = 2; i <= max; i++) {
      if (i % 2 != 0) {
        int now = i;
        if (sosuu[i / 2] == 1) {
          while (now <= max) {
            now += i * 2;
            sosuu[now / 2] = 0;
          }
        }
      }
    }

    int sum = 0;
    int temp = 1;
    for (int i = 0; i < n; i++) {
      if (input[i] == 2) {
        sum += temp;
        temp = 0;
      } else if (input[i] % 2 != 0) {
        sum += sosuu[input[i] / 2];
        sosuu[input[i] / 2] = 0;
      }
    }
    cout << sum << endl;
  }
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

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

int main(void) {
  int n, c = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    if (IsPrime(p) == true) {
      c++;
    }
  }
  cout << c << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool primes[100000000];

void Sieve() {
  for (int i = 2; i < 100000000; i++) {
    primes[i] = true;
  }
  primes[1] = false;

  for (int i = 2; i < 10000; i++) {
    if (primes[i] == true)
      for (int j = i; j * i <= 100000000; j++) {
        primes[j * i] = false;
      }
  }
}

bool IsPrime(int x) { return primes[x]; }

int main(void) {
  Sieve();
  int n, c = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    if (IsPrime(p) == true) {
      c++;
    }
  }
  cout << c << endl;

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

#include <cstdio>
using namespace std;
bool Prime(int x) {
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}
int main() {
  int n;
  scanf("%d", &n);
  int N[n];
  for (int i = 0; i < n; i++) {
    scanf(" %d", &N[i]);
  }
  int cnt;
  for (int i = 0; i < n; i++) {
    if (Prime(N[i]) == true) {
      cnt++;
    }
  }
  printf("%d\\n", cnt);
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
using namespace std;
bool Prime(int x) {
  for (int i = 2; i < (x / 2 + 1); i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}
int main() {
  int n;
  scanf("%d", &n);
  int N[n];
  for (int i = 0; i < n; i++) {
    scanf(" %d", &N[i]);
  }
  int cnt;
  for (int i = 0; i < n; i++) {
    if (Prime(N[i]) == true) {
      cnt++;
    }
  }
  printf("%d\\n", cnt);
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long int n, count = 0, i;
  long long int num;
  cin >> n;
  while (n--) {
    cin >> num;
    bool flag = 0;
    for (i = 2; i <= sqrt(num); i++) {
      if (num % i == 0) {
        flag = 1;
        break;
      }
    }
    if (flag == 0)
      count++;
  }
  cout << count << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long int n, count = 0, i;
  long long int num;
  cin >> n;
  while (n--) {
    cin >> num;
    bool flag = 0;
    for (i = 2; i <= num / 2; i++) {
      if (num % i == 0) {
        flag = 1;
        break;
      }
    }
    if (flag == 0)
      count++;
  }
  cout << count << "\\n";
  return 0;
}