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

#include <cmath>
#include <iostream>
using namespace std;
#define rep(i, k, n) for (int i = k; i < n; i++)
#define MAX 10000001

bool is[MAX];

// n log log n
// 10^8
void era() {
  rep(i, 0, MAX) is[i] = true;
  is[0] = is[1] = 0;
  rep(i, 2, MAX) {
    if (is[i]) {
      for (int j = 2; i * j < MAX; j++) {
        is[i * j] = false;
      }
    }
  }
}

int main() {
  // N root(n)
  // 10^4 10^4
  int n, p, ans = 0;
  cin >> n;

  rep(i, 0, n) {
    cin >> p;
    bool ex = true;
    if ((p % 2 == 0 && p != 2) || p == 1)
      continue;
    for (int j = 3; j <= sqrt(p); j += 2) {

      if (p % j == 0)
        ex = false;
    }
    if (ex)
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define rep(i, k, n) for (int i = k; i < n; i++)
#define MAX 10000001

bool is[MAX];
int main() {

  rep(i, 0, MAX) is[i] = true;
  is[0] = is[1] = 0;
  rep(i, 2, MAX) {
    if (is[i]) {
      for (int j = 2; i * j < MAX; j++) {
        is[i * j] = false;
      }
    }
  }
  int n, p, ans = 0;
  cin >> n;

  rep(i, 0, n) {
    cin >> p;
    if (is[p])
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int main(void) {
  int n, i, j, ans = 0, x[10000];

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &x[i]);
  }

  for (i = 0; i < n; i++) {
    if (x[i] % 2 != 0 && x[i] > 3073) {
      for (j = 3; j < x[i] / 64; j += 2) {
        if (x[i] % j == 0) {
          break;
        }
      }
      if (j >= x[i] / 64) {
        ans++;
      }
    } else if ((x[i] % 2 != 0 && x[i] <= 3073) || x[i] == 2) {
      for (j = 3; j < x[i]; j += 2) {
        if (x[i] % j == 0)
          break;
      }
      if (x[i] <= j || x[i] == 2) {
        ans++;
      }
    }
  }
  printf("%d\\n", ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>

int main(void) {
  int n, i, j, ans = 0, x[10000];

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &x[i]);
  }

  for (i = 0; i < n; i++) {
    if (x[i] % 2 != 0 && x[i] > 50) {
      for (j = 3; j < x[i] / 8; j += 2) {
        if (x[i] % j == 0) {
          break;
        }
      }
      if (j >= x[i] / 8) {
        ans++;
      }
    } else if ((x[i] % 2 != 0 && x[i] <= 50) || x[i] == 2) {
      for (j = 3; j < x[i]; j += 2) {
        if (x[i] % j == 0)
          break;
      }
      if (x[i] <= j || x[i] == 2) {
        ans++;
      }
    }
  }
  printf("%d\\n", ans);

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

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

int prs[10000];
int pri = 1;

int isPrime(int a) {
  int flg;
  if (sqrt(a) > prs[pri]) {
    for (int i = prs[pri] + 2; i <= sqrt(a); i += 2) {
      flg = 1;
      for (int j = 0; j <= pri; j++) {
        if (sqrt(i) < prs[i])
          break;
        if (i % prs[j] == 0) {
          flg = 0;
          break;
        }
      }
      if (flg)
        prs[++pri] = i;
    }
    // for (int i=0;i<=pri;i++)cout << prs[i] << " ";
  }
  for (int i = 0; i <= pri; i++) {
    if (sqrt(a) < prs[i])
      break;
    if (a % prs[i] == 0)
      return 0;
  }
  return 1;
}

int main() {
  prs[0] = 2;
  prs[1] = 3;

  int n, a, cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    cnt += isPrime(a);
  }
  cout << cnt << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int prs[5000];
int pri = 1;

int isPrime(int a) {
  int flg;
  if (sqrt(a) > prs[pri]) {
    for (int i = prs[pri] + 2; i <= sqrt(a); i += 2) {
      flg = 1;
      for (int j = 0; j <= pri; j++) {
        if (sqrt(i) < prs[i])
          break;
        if (i % prs[j] == 0) {
          flg = 0;
          break;
        }
      }
      if (flg)
        prs[++pri] = i;
    }
    // for (int i=0;i<=pri;i++)cout << prs[i] << " ";
  }
  for (int i = 0; i <= pri; i++) {
    if (sqrt(a) < prs[i])
      break;
    if (a % prs[i] == 0)
      return 0;
  }
  return 1;
}

int main() {
  prs[0] = 2;
  prs[1] = 3;

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

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

int isPrime(int num) {
  int i;
  if (num <= 1)
    return 0;

  else if (num == 2)
    return 1;
  if (num % 2 == 0)
    return 0;
  for (i = 3; i <= sqrt(static_cast<double>(num)); i += 2) {
    if (num % i == 0)
      return 0;
  }
  return 1;
}

int main() {
  const int max = 10000;
  int i, size, A[max], count = 0;
  cin >> size;
  for (i = 0; i < size; ++i) {
    cin >> A[i];
    if (isPrime(A[i]))
      ++count;
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int isPrime(int num) {
  int i;
  if (num <= 1)
    return 0;

  else if (num == 2)
    return 1;
  if (num % 2 == 0)
    return 0;

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

int main() {
  const int max = 10000;
  int i, size, A[max], count = 0;
  cin >> size;
  for (i = 0; i < size; ++i) {
    cin >> A[i];
    if (isPrime(A[i]))
      ++count;
  }
  cout << count << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

bool isprime[10000];

int main(void) {

  for (int i = 0; i < 10000; ++i) {
    isprime[i] = true;
  }
  isprime[0] = isprime[1] = false;

  for (int i = 2; i < 100; ++i) {
    if (isprime[i]) {
      for (int j = i + i; j < 10000; j += i) {
        isprime[j] = false;
      }
    }
  }

  int N;
  int A;
  int ans = 0;

  std::cin >> N;
  ans = N;
  for (int i = 0; i < N; ++i) {
    std::cin >> A;
    int n = std::min(10000, A);
    for (int j = 2; j < n; ++j) {
      if (isprime[j]) {
        if (A % j == 0) {
          ans -= 1;
          break;
        }
      }
    }
  }

  std::cout << ans << std::endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

bool isprime[100000000];

int main(void) {

  for (int i = 0; i < 100000000; ++i) {
    isprime[i] = true;
  }
  isprime[0] = isprime[1] = false;

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

  int N;
  int A;
  int ans = 0;

  std::cin >> N;
  for (int i = 0; i < N; ++i) {
    std::cin >> A;
    if (isprime[A]) {
      ans += 1;
    }
  }

  std::cout << ans << std::endl;

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

#include <bits/stdc++.h>

int main() {

  const int max = 100000;

  bool *p;
  p = new bool[max];

  for (int i = 0; i < max; ++i) {
    p[i] = true;
  }

  p[0] = p[1] = false;
  for (int i = 2; i < max; ++i) {
    if (p[i]) {
      for (int j = i * 2; j < max; j += i) {
        p[j] = false;
      }
    }
  }

  std::vector<int> pv;
  for (int i = 0; i < max; ++i) {
    if (p[i]) {
      pv.push_back(i);
    }
  }

  int N;
  int temp;
  int ans = 0;
  std::cin >> N;

  for (int i = 0; i < N; ++i) {
    std::cin >> temp;
    for (int j = 0; j < pv.size(); ++j) {
      if (temp % pv[j] == 0 && pv[j] != temp) {
        goto label_1;
      }
    }
    ans += 1;
  label_1:;
  }

  std::cout << ans << std::endl;

  delete[] p;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

int main() {

  const int max = 100000100;

  bool *p;
  p = new bool[max];

  for (int i = 0; i < max; ++i) {
    p[i] = true;
  }

  p[0] = p[1] = false;
  for (int i = 2; i < max; ++i) {
    if (p[i]) {
      for (int j = i * 2; j < max; j += i) {
        p[j] = false;
      }
    }
  }

  int N;
  int temp;
  int ans = 0;
  std::cin >> N;

  for (int i = 0; i < N; ++i) {
    std::cin >> temp;
    if (p[temp]) {
      ans += 1;
    }
  }

  std::cout << ans << std::endl;

  delete[] p;

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

#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <stdlib.h>
#include <string>
#include <typeinfo>
#include <vector>

using namespace std;

// std::vector<bool> isNotPrime(int max){
//   std::vector<bool> is_not_prime(max+1);
//   for(int i=2; i<=max/2; i++){
//     int j = i*2;
//     while(j <= max){
//       is_not_prime[j] = true;
//       j+=i;
//     }
//   }
//   return is_not_prime;
// }

bool isPrime(int target) {
  // cout << "c" << endl;

  for (int i = 2; i * i <= target; i++) {
    // cout << i << endl;
    if (target % i == 0) {
      // cout << "divide" << endl;
      return false;
    }
  }
  // cout << "d" << endl;

  return true;
}

int main(int argc, char *argv[]) {
  int n, cnt = 0, num;
  cin >> n;
  // std::vector<int> a(n);
  // std::vector<bool> is_not_prime;
  for (int i = 0; i < n; i++) {
    cin >> num;
    if (isPrime(num) == true) {
      cnt++;
    }
    // cout << "b" << endl;
  }

  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <stdlib.h>
#include <string>
#include <typeinfo>
#include <vector>

using namespace std;

std::vector<bool> isNotPrime(int max) {
  std::vector<bool> is_not_prime(max + 1);
  for (int i = 2; i <= max / 2; i++) {
    int j = i * 2;
    while (j <= max) {
      is_not_prime[j] = true;
      j += i;
    }
  }
  return is_not_prime;
}

int main(int argc, char *argv[]) {
  int n, max = 0, cnt = 0, num;
  cin >> n;
  std::vector<int> a(n);
  std::vector<bool> is_not_prime;
  for (int i = 0; i < n; i++) {
    cin >> num;
    a[i] = num;
    if (num > max) {
      max = num;
    }
  }
  // cout << "max number is = " << max << endl;
  // maxまでの範囲でエラトステネスの篩
  is_not_prime = isNotPrime(max);

  for (int i = 0; i < n; i++) {
    if (is_not_prime[a[i]] == false) { // 見つかったら
      cnt++;
      // cout << a[i];
      // cout << "は素数" << endl;
    }
  }

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

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

bool isprime(int x) {
  if (x == 2)
    return true;

  if (x < 2 || 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, x;
  int cnt = 0;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> x;
    if (isprime(x))
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  int p, prime = 0;
  int cnt = 0;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> p;
    if (p == 0 || p == 1)
      break;
    cnt = 0;

    for (int j = 2; j <= p; j++) {
      if (p % j == 0)
        cnt++;
    }
    if (cnt == 1)
      prime++;
  }
  cout << prime << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, ans = 0, a, i;
int sub(int x) {
  if (x == 2)
    return true;
  if (x < 2 || x % 2 == 0)
    return false;
  i = 3;
  while (i <= sqrt(x)) {
    if (x % i == 0)
      return false;
    i = i + 2;
  }
  return true;
}
int main() {
  cin >> n;
  for (int j = 0; j < n; j++) {
    cin >> a;
    ans += sub(a);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a, kazu = 0, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (a == 2 || a % 2 == 1) {
      for (int j = 2; j < a; j++) {
        if (a % j == 0) {
          kazu++;
          break;
        }
      }
      if (kazu == 0)
        ans++;
      else
        kazu = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define REP(i, j, n) for (int i = j; i < n; i++)
#define SORT(x) sort(x.begin(), x.end())

using namespace std;

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

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

int main() {
  int n, m, count = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m;
    if (isPrime(m))
      count++;
  }

  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
#define REP(i, j, n) for (int i = j; i < n; i++)
#define SORT(x) sort(x.begin(), x.end())

using namespace std;

int main() {
  int n, m, count = 0, allcount = 0;
  cin >> n;
  REP(i, 0, n) {
    cin >> m;
    REP(j, 1, m / 2 + 1) {
      if (m % j == 0)
        count++;
      if (count > 1)
        break;
    }
    if (count < 2)
      allcount++;
    count = 0;
  }
  cout << allcount << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  int a;
  int c = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    bool f = true;
    int t = 2;
    int s;
    s = a / t;
    for (t = 2; t <= s; t++) {
      if (a / t * t == a) {
        f = false;
        break;
      }
      s = a / t;
    }
    if (f) {
      c++;
    }
  }
  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int a;
  int c = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    bool f = true;
    for (int t = 2; t < a; t++) {
      if (a / t * t == a) {
        f = false;
        break;
      }
    }
    if (f) {
      c++;
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bitset>
#include <cmath>
#include <iostream>

using namespace std;

const int N = 100000000 + 1;
bitset<N> arr;

void eratosthenes() {
  for (int i = 2; i < N; ++i) {
    arr[i] = 1;
  }

  for (int i = 2; i < sqrt(N); ++i) {
    if (arr[i]) {
      for (int j = 2; i * j < N; ++j) {
        arr[i * j] = 0;
      }
    }
  }
}

bool is_prime(int n) { return arr[n]; }

int main() {
  eratosthenes();

  int n;
  cin >> n;

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

    if (is_prime(x)) {
      // cerr << "debug: " <<  x << endl;
      count++;
    }
  }

  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

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

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

    if (is_prime(x)) {
      count++;
    }
  }

  cout << count << endl;

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

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

typedef long long ll;

#define N 100000001

int table[N];

void prim() {
  table[0] = table[1] = 1;
  for (int i = 2; i * i < N; i++) {
    for (int j = 2 * i; j < N; j += i) {
      table[j] = 1;
    }
  }
}

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

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int cou = 0, a;
  //    prim();
  //    for(int i=0;i<n;i++){
  //        cin >> a;
  //        if(!table[a])cou++;
  //    }
  //    cout << cou << endl;

  for (int i = 0; i < n; i++) {
    cin >> a;
    if (chepri(a))
      cou++;
  }
  cout << cou << endl;
}


Error: MLE

Fixed submission:

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

typedef long long ll;

#define N 100000001

int table[N];

void prim() {
  table[0] = table[1] = 1;
  for (int i = 2; i * i < N; i++) {
    for (int j = 2 * i; j < N; j += i) {
      table[j] = 1;
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  prim();
  int n;
  cin >> n;
  int cou = 0, a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (!table[a])
      cou++;
  }
  cout << cou << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

using namespace std;

typedef unsigned int uint32;

namespace {
bool IsPrime(uint32 x) {
  if (x == 2) {
    return true;
  }
  if (x % 2 == 0) {
    return false;
  }

  const uint32 m = static_cast<uint32>(sqrt(x));
  for (size_t i = 3; i <= m; ++i) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}
} // namespace

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

  uint32 c = 0;
  uint32 x;
  for (uint32 i = 0; i < n; ++i) {
    cin >> x;
    if (IsPrime(x)) {
      ++c;
    }
  }
  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

typedef unsigned int uint32;

namespace {
bool IsPrime(uint32 x) {
  for (size_t i = 2; i < x; ++i) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}
} // namespace

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

  uint32 x;
  uint32 c = 0;
  for (uint32 i = 0; i < n; ++i) {
    cin >> x;
    if (IsPrime(x)) {
      ++c;
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

#define CALC_PRIME_MAX 10007   // _/10 ^ 8 + next prime
#define PRIME_ELEMENT_MAX 1230 // prime( _/10 ^ 8 ) + 1(10007)

int prime_number[PRIME_ELEMENT_MAX];

bool isPrime(int element) {
  for (int i = 0; prime_number[i] * prime_number[i] <= element; i++) {
    if (element % prime_number[i] == 0)
      return false;
  }
  return true;
}

void calcPrime() {
  // add 2, 3 (prime)
  int prime_index = 2;
  prime_number[0] = 2;
  prime_number[1] = 3;
  // next prime is 5
  for (int element = 5; element <= CALC_PRIME_MAX; element += 2) {
    bool is_prime = true;
    // judge prime
    for (int i = 1; prime_number[i] * prime_number[i] <= element; i++) {
      if (element % prime_number[i] == 0) {
        is_prime = false;
        break;
      }
    }
    // add prime
    if (is_prime)
      prime_number[prime_index++] = element;
  }
  //	cout << "prime_element_num = " << prime_index << endl;
  return;
}

int main() {
  int n, element, count = 0;
  calcPrime();
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> element;
    if (isPrime(element))
      count++;
  }
  cout << count << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

#define CALC_PRIME_MAX 10000   // _/10 ^ 8
#define PRIME_ELEMENT_MAX 1229 // prime( _/10 ^ 8 )

int prime_number[PRIME_ELEMENT_MAX];

bool isPrime(int element) {
  for (int i = 0; prime_number[i] * prime_number[i] <= element; i++) {
    if (element % prime_number[i] == 0)
      return false;
  }
  return true;
}

void calcPrime() {
  // add 2, 3 (prime)
  int prime_index = 2;
  prime_number[0] = 2;
  prime_number[1] = 3;
  // next prime is 5
  for (int element = 5; element <= CALC_PRIME_MAX; element += 2) {
    bool is_prime = true;
    // judge prime
    for (int i = 1; prime_number[i] * prime_number[i] <= element; i++) {
      if (element % prime_number[i] == 0) {
        is_prime = false;
        break;
      }
    }
    // add prime
    if (is_prime)
      prime_number[prime_index++] = element;
  }
  //	cout << "prime_element_num = " << prime_index << endl;
  return;
}

int main() {
  int n, element, count = 0;
  calcPrime();
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> element;
    if (isPrime(element))
      count++;
  }
  cout << count << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  cin >> n;

  while (cin >> a) {
    if (a % 2 != 0) {
      tmp = 0;
      for (i = 2; i <= sqrt(a); i++) {
        if ((a % i) == 0)
          tmp++;
      }
      if (tmp == 0)
        cnt++;
    } else if (a == 2)
      cnt++;
  }

  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

  cin >> n;

  while (cin >> a) {
    tmp = 0;
    for (i = 2; i < a; i++) {
      if ((a % i) == 0)
        tmp++;
    }
    if (tmp == 0)
      cnt++;
  }

  cout << cnt << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

bool check(int a) {
  bool flag = true;

  if (a % 2 == 0) {
    return false;
  }

  for (int i = 3; i <= (int)sqrt(a); i += 2) {
    if (a % i == 0) {
      flag = false;
      break;
    }
  }

  if (flag == true) {
    return true;
  } else {
    return false;
  }
}

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

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m;
    if (m == 2) {
      count++;
      continue;
    }
    if (check(m) == true) {
      count++;
    }
  }

  cout << count << endl;

  return 0;
}

Error: MLE

Fixed submission:

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

int main() {
  int n, m;
  int max = 2;
  vector<int> vec;
  vector<int> vecin;
  vector<int>::iterator it;
  int count = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m;
    vecin.push_back(m);
    if (max < m) {
      max = m;
    }
  }

  vec.push_back(2);
  vec.push_back(3);
  vec.push_back(5);
  vec.push_back(7);
  vec.push_back(11);
  vec.push_back(13);
  for (int i = 15; i <= max; i += 2) {
    if (i % 3 == 0 || i % 5 == 0 || i % 7 == 0 || i % 11 == 0 || i % 13 == 0) {
      continue;
    }
    vec.push_back(i);
  }
  if (max % 2 == 1) {
    vec.push_back(max);
  }

  for (vector<int>::iterator iter = vec.begin(); iter != vec.end(); ++iter) {
    it = iter;
    ++it;
    while (it != vec.end()) {
      if ((*it) % (*iter) == 0) {
        it = vec.erase(it);
      } else {
        ++it;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (vector<int>::iterator iter = vec.begin(); iter != vec.end(); ++iter) {
      if (vecin[i] == (*iter)) {
        count++;
        break;
      }
      if (vecin[i] < (*iter)) {
        break;
      }
    }
  }

  cout << count << endl;

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

#include <cmath>
#include <iostream>

using namespace std;

void PrimeNumSum(int &sum, unsigned int &num) {
  unsigned int divisor = sqrt(num) + 1;

  if (divisor == 2) {
    sum++;
    return;
  }
  while (divisor >= 2) {
    if (num % divisor == 0) {
      return;
    }
    divisor--;
  }

  sum++;
}

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

  unsigned int *list = new unsigned int[count];

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

  int sum = 0;

  for (int i = 0; i < count; i++) {
    PrimeNumSum(sum, list[i]);
  }

  cout << sum << endl;

  delete[] list;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

void PrimeNumSum(int &sum, unsigned int &num) {
  unsigned int divisor = num / 2 + 1;

  if (divisor == 2) {
    sum++;
    return;
  }
  while (divisor >= 2) {
    if (num % divisor == 0) {
      return;
    }
    divisor--;
  }

  sum++;
}

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

  unsigned int *list = new unsigned int[count];

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

  int sum = 0;

  for (int i = 0; i < count; i++) {
    PrimeNumSum(sum, list[i]);
  }

  cout << sum << endl;

  delete[] list;
  return 0;
}