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

#include <bits/stdc++.h>
#define _USE_MATH_DEFINES
#include <math.h>
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 <= static_cast<int>(sqrt(x))) {
    if (x % i == 0) {
      return false;
    }
    i = i + 2;
  }
  return true;
}

int main() {
  int ans = 0;
  int biggest_number = 0;
  vector<bool> v(100000000);

  int n;
  while (cin >> n) {
    if (n > biggest_number) {

      biggest_number = n;
    }
    v[n] = true;
  }

  for (int i = 0; i < biggest_number + 1; i++) {
    if (v[i] == true) {
      if (isPrime(i)) {
        ans++;
      }
    }
  }

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

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define _USE_MATH_DEFINES
#include <math.h>
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 <= static_cast<int>(sqrt(x))) {
    if (x % i == 0) {
      return false;
    }
    i = i + 2;
  }
  return true;
}

int main() {
  int ans = 0;
  int biggest_number = 0;
  vector<int> v(100000000);

  int n;
  while (cin >> n) {
    if (n > biggest_number) {

      biggest_number = n;
    }
    v[n] = 1;
  }

  for (int i = 0; i < biggest_number + 1; i++) {
    if (v[i] == 1) {
      if (isPrime(i)) {
        ans++;
      }
    }
  }

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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int inf = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int in;
    cin >> in;
    int limit = sqrt(in);
    for (int j = 2; j <= limit; j++) {
      if (in % j == 0) {
        ans++;
        break;
      }
    }
  }
  cout << n - ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int inf = 1e9 + 7;

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

int main() {
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int in;
    cin >> in;
    if (isPrime(in)) {
      ans++;
    }
  }
  cout << ans << endl;

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

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

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

  for (int j = 0; j < n; ++j) {
    cin >> m;
    if (m == 2)
      ++count;
    else if (m < 2 || m % 2 == 0) {
      continue;
    } else {
      i = 3;
      while (i <= sqrt(m)) {
        if (m % i == 0)
          break;
        i = i + 2;
      }
      if (i > sqrt(m)) {
        ++count;
      }
    }
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

  for (int j = 0; j < n; ++j) {
    cin >> m;
    if (m == 2)
      ++count;
    else if (m < 2 || m % 2 == 0) {
      continue;
    } else {
      i = 3;
      while (i <= sqrt(m)) {
        if (m % i == 0)
          continue;
        i = i + 2;
      }
      if (i > sqrt(m)) {
        ++count;
      }
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
bool check(int);
int main() {
  int n, a, count;
  count = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a;
    if (check(a))
      count++;
  }
  cout << count << endl;

  return 0;
}
bool check(int a) {
  if (a == 2)
    return true;
  if (a % 2 == 0 || a < 2)
    return false;
  int k;
  k = 3;
  while (k <= sqrt((float)a)) {
    if (a % k == 0)
      return false;
    k += 2;
  }

  return true;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
int main() {
  int n;
  vector<int> dp(100000001, false);
  dp[0] = true;
  dp[1] = true;
  for (int i = 3; i < 100000001; i += 2) {
    if (dp[i] = false) {
      for (int j = i + i; j <= 100000001; j += i) {
        dp[j] = true;
      }
    }
  }
  cin >> n;

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

#include <cstdlib>
#include <iostream>
// n個の数値の入力に対して,素数の数を出力する問題
using namespace std;
bool isPrime(int x);

int main() {
  int n, num, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num;
    if (isPrime(num))
      ans++;
  }
  cout << ans << endl;
  return EXIT_SUCCESS;
}

// 素数判定
// 合成数xはx <= √pを満たす素因子pを持つ
bool isPrime(int x) {
  if (x == 2)
    return true;
  if (x < 2 || x % 2 == 0)
    return false;
  int i = 3;
  while (i * i <= x) {
    if (x % i == 0)
      return false;
    i += 2;
  }
  return true;
}


Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>
// n個の数値の入力に対して,素数の数を出力する問題
using namespace std;
bool isPrime(int x);

int main() {
  int n, num, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num;
    if (isPrime(num))
      ans++;
  }
  cout << ans << endl;
  return EXIT_SUCCESS;
}

bool isPrime(int x) {
  if (x <= 1)
    return false;
  else {
    for (int i = 2; i < x; i++)
      if (x % i == 0)
        return false;
    return true;
  }
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int Judge(int i) {
  int flag = 1;
  for (int j = 2; j <= sqrt(i); j++) {
    if (i % j == 0) {
      flag = 0;
    }
  }
  return flag;
}

int main() {

  int n, a;
  int count = 0;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a;
    if (Judge(a)) {
      count++;
    }
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
const int SIZE = 1000005;
using namespace std;

int book[SIZE];

void initBook() {
  for (int i = 0; i < SIZE; i++) {
    book[i] = 0;
  }

  for (int i = 2; i < SIZE; i++) {
    int flag = 1;
    for (int j = 2; j <= sqrt(i); j++) {
      if (i % j == 0) {
        flag = 0;
      }
    }
    if (flag) {
      book[i] = 1;
    }
  }
}

int main() {

  initBook();

  int n, a;
  int count = 0;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a;
    if (book[a]) {
      count++;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

template <class T> bool isPrime(T n) {
  if (n == 2)
    return true;
  if (n < 2 || n % 2 == 0)
    return false;
  for (T i = 3; i * i <= n; i += 2)
    if (n % i == 0)
      return false;
  return true;
}

#include <iostream>
using namespace std;
int main() {
  int N;
  cin >> N;
  int a = 0;
  for (int i = 0; i < N; ++i) {
    int x;
    cin >> x;
    a += isPrime(x);
  }
  cout << a << endl;
}

Error: -11

Fixed submission:

const int MAX_NUM = 100000001;
class Sieve {
public:
  Sieve() {
    for (auto &a : prime)
      a = true;
    prime[0] = prime[1] = false;
  }
  void erato() {
    for (int i = 2; i * i <= MAX_NUM; ++i)
      if (prime[i])
        for (int j = 2; i * j <= MAX_NUM; ++j)
          prime[i * j] = false;
  }
  bool isPrime(int x) { return prime[x]; }

private:
  bool prime[MAX_NUM];
};

#include <iostream>
using namespace std;
int main() {
  Sieve SV;
  SV.erato();
  int N;
  cin >> N;
  int a = 0;
  for (int i = 0; i < N; ++i) {
    int x;
    cin >> x;
    a += SV.isPrime(x);
  }
  cout << a << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int solve(int m) {

  if (m == 2) {
    // printf("?´???°??§???%d\\n",m);
    return 1;
  }
  if (m < 2 || m % 2 == 0)
    return 0;
  int e = 0;
  for (int j = 3; j <= sqrt(m); j++) {
    if (m % j == 0) {
      return 0;
    }
  }

  if (e == 0) {
    // printf("?´???°??§???%d\\n", m);
    return 1;
  }
}

int main() {

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

    int j = solve(m);
    if (j == 1)
      p++;
  }

  cout << p << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>

using namespace std;

int main() {

  int n;
  int p = 0;
  cin >> n;

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

    int d = 0;
    for (int j = 1; j <= q; j++) {
      if (q % j == 0)
        d++;
      if (d > 2)
        break;
    }
    if (d == 2)
      p++;
  }

  cout << p << endl;

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

#include <stdio.h>

int main() {
  int n, primes = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int num;
    scanf("%d", &num);
    int w = 2;
    for (; w * w <= num; w++) {
      if (!(num % w)) {
        primes--;
        break;
      }
    }
    primes++;
  }
  printf("%d\\n", primes);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main() {
  int n, primes = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
  NEXT_N:
    int num;
    scanf("%d", &num);
    for (int w = 2; w * w <= num; w++) {
      if (!(num % w)) {
        i++;
        goto NEXT_N;
      }
    }
    primes++;
  }
  printf("%d\\n", primes);
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

using namespace std;

bool PrimeN(int x) {

  if (x == 1) {
    return false;
  }

  for (int i = 2; i <= sqrt(x); i++) {

    if (x == i) {
      // cout << x << endl;
      return true;
    }

    if (x % i == 0) {
      return false;
    }
  }

  // cout << x << endl;
  return true;
}

int main(void) {

  int n;
  int cnt = 0;

  cin >> n;

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

    int X;
    cin >> X;

    if (PrimeN(X)) {
      cnt++;
    }
  }

  cout << cnt << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

bool PrimeN(int x) {

  if (x == 1) {
    return false;
  }

  for (int i = 2; i < x - 1; i++) {

    if (x == i) {
      cout << x << endl;
      return true;
    }

    if (x % i == 0) {
      return false;
    }
  }

  return true;
}

int main(void) {

  int n;
  int cnt = 0;

  cin >> n;

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

    int X;
    cin >> X;

    if (PrimeN(X)) {
      cnt++;
    }
  }

  cout << cnt << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>

#define REP(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

bool Solution(int data) {
  int i;

  if (data < 2) {
    return false;
  } else if (data == 2) {
    return true;
  }

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

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

  return true;
}

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

  cin >> n;

  rep(i, n) {
    cin >> m;
    if (Solution(m)) {
      count++;
    }
  }
  cout << count << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>

using namespace std;

void Solution() {
  const int n = 100000000;
  vector<bool> a(n, true);
  int N;
  long int b;
  int count = 0;

  cin >> N;

  for (long int i = 0; i < n; i++) {
    if (i % 2 == 0) {
      a[i] = false;
    } else {
      a[i] = true;
    }
  }

  a[1] = false;
  a[2] = true;

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

  for (int i = 0; i < N; i++) {
    cin >> b;

    if (a[b]) {
      count++;
    }
  }

  cout << count << endl;
}

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

#include <iostream>
typedef long long ll;
bool isPrime(ll a);

int main() {
  using namespace std;
  ll A[10001];
  int n, prime = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (isPrime(A[i])) {
      prime++;
    }
  }
  cout << prime << endl;
  return 0;
}

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


Error: TLE

Fixed submission:

#include <iostream>
typedef long long ll;
bool isPrime(ll a);

int main() {
  using namespace std;
  ll A[10001];
  int n, prime = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (isPrime(A[i])) {
      prime++;
    }
  }
  cout << prime << endl;
  return 0;
}

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

/*
URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_C&lang=jp
?????????: Getting Started - Prime Numbers
?????°
7/5
14:08 Online Programming Lesson???????????¢????????????????????£????????????
14:12 ????????????
14:13 ?????????????????????????????????
14:14 TLE???????????????????????§????????????
*/
#include <cstdio>

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

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

Error: TLE

Fixed submission:

/*
URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_C&lang=jp
?????????: Getting Started - Prime Numbers
?????°
7/5
14:08 Online Programming Lesson???????????¢????????????????????£????????????
14:12 ????????????
14:13 ?????????????????????????????????
*/
#include <cstdio>

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

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

#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int n, i, j = 0, x[100001];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x[i];
    int total = 0;
    if (x[i] % 2 == 0 && x[i] != 2)
      continue;
    int N = sqrt(x[i]);
    for (int k = 1; k <= N; k++) {
      if (x[i] % k == 0)
        total++;
    }
    if (total == 1) {
      j++;
    }
  }
  cout << j << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, i, j = 0, x[100001];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x[i];
    int total = 0;
    if (x[i] % 2 == 0 && x[i] != 2)
      continue;
    for (int k = 1; k < x[i]; k++) {
      if (x[i] % k == 0)
        total++;
    }
    if (total == 1) {
      j++;
    }
  }
  cout << j << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

bool is_prime(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 true;
}

int main(int argc, char const *argv[]) {
  int n, count = 0;
  scanf("%d", &n);

  while (n-- > 0) {
    int i;
    scanf("%d", &i);
    if (is_prime(i))
      count++;
  }

  printf("%d\\n", count);
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>

const int N = 10000;
int primes[N + 1];

int main(int argc, char const *argv[]) {

  for (int i = 2; i < N + 1; ++i)
    primes[i] = i;

  for (int i = 2; i < N + 1; ++i)
    if (primes[i] == i)
      for (int j = i * 2; j < N + 1; j += i)
        primes[j] = -1;

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

  while (n-- > 0) {
    int i;
    scanf("%d", &i);
    if (primes[i] == i)
      count++;
  }

  printf("%d\\n", count);
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <string.h>
#include <string>

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

int main() {
  int N;
  int cnt = 0;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    int e;
    scanf("%d", &e);
    if (isprime(e)) {
      cnt++;
    }
  }
  printf("%d\\n", cnt);

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <string.h>
#include <string>

using namespace std;

const int MAXN = 100000000 + 1;
char isprime[MAXN];

void init() {
  memset(isprime, -1, sizeof(isprime));
  for (int i = 2; i < MAXN; i++) {
    if (isprime[i]) {
      for (long long j = ((long long)i) * i; j < MAXN; j += i) {
        isprime[j] = 0;
      }
    }
  }
}

int main() {
  int N;
  int cnt = 0;
  init();
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    int e;
    scanf("%d", &e);
    if (isprime[e]) {
      cnt++;
    }
  }
  printf("%d\\n", cnt);

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

#include <cmath>
#include <iostream>

using namespace std;

int isprime(int x) {
  if (x == 2)
    return 1;
  if (x < 2 || x % 2 == 0)
    return 0;

  int i = 3;
  while (i <= sqrt(x)) {
    if (x % i == 0)
      return 0;
    i += 2;
  }
  return 1;
}

int main() {
  int i, *k, n, judge, c = 0;

  cin >> n;
  k = new int[n];
  for (i = 0; i < n; i++) {
    cin >> k[i];
    if ((judge = isprime(k[i])) == 1)
      c++;
  }
  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int i, j, *k, n, c = 0, a;

  cin >> n;
  k = new int[n];
  for (i = 0; i < n; i++) {
    cin >> k[i];
  }
  for (i = 0; i < n; i++) {
    a = 0;
    for (j = 2; j <= k[i] / 2; j++) {
      if (k[i] % j == 0)
        a = 1;
      if (a == 1)
        break;
    }
    if (a == 0)
      c++;
  }
  cout << c << endl;

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

#define _USE_MATH_DEFINES
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, start, n) for (int i = start; i < n; i++)
#define And &&
#define Or ||
#define Lb cout << endl
using namespace std;

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

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

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

int main(void) {
  int n;
  cin >> n;
  int x;
  int sum = 0;
  rep(i, 0, n) {
    cin >> x;
    if (isprime(x))
      sum++;
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, start, n) for (int i = start; i < n; i++)
#define And &&
#define Or ||
#define Lb cout << endl
using namespace std;

int gcd(int x, int y) {
  int r;
  while (y > 0) {
    if (y > x)
      swap(x, y);
    r = x % y;
    x = y;
    y = r;
  }
  return x;
}

int main(void) {
  int n;
  cin >> n;
  int sum = 0;
  int x, hantei = true;
  rep(i, 0, n) {
    cin >> x;
    hantei = true;
    rep(j, 1, x) {
      if (gcd(x, j) != 1) {
        hantei = false;
      }
    }
    if (hantei == true)
      sum++;
  }
  cout << sum << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

bool Judge(int A) {
  for (int j = 2; j <= floor(sqrt((double)A)); j++) {
    if (A % j == 0)
      return false;
  }
  return true;
}

int PrimeNumbers(vector<int> A) {
  int count = 0;
  for (int i = 0; i < A.size(); i++) {
    if (Judge(A[i]))
      count++;
  }
  return count;
}

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

  vector<int> A;
  A.resize(n);

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

  cout << PrimeNumbers(A) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int GCB(int x, int y) {
  int z = y;
  y = x % y;
  x = z;

  if (y == 0)
    return x;

  return GCB(x, y);
}

int PrimeNumbers(vector<int> A) {
  int count = 0;
  for (int i = 0; i < A.size(); i++) {
    for (int j = 2; j < A[i] / 2 + 1; j++) {
      if (GCB(A[i], j) != 1) {
        count++;
        break;
      }
    }
  }
  return A.size() - count;
}

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

  vector<int> A;
  A.resize(n);

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

  cout << PrimeNumbers(A) << endl;

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

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

int main() {
  int i, j;
  int n;
  int A[10000];
  int res = 0;
  bool prime;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", A + i);
  }

  for (i = 0; i < n; i++) {
    prime = true;
    for (j = 2; j <= (int)sqrt(A[i]); j++) {
      if (A[i] % j == 0) {
        prime = false;
        break;
      }
    }
    if (prime)
      res++;
  }

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

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int i, j;
  int n;
  int A[10000];
  int res = 0;
  vector<int> P;
  vector<int>::iterator it;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", A + i);
  }

  sort(A, A + n);
  P.push_back(2);
  j = 3;
  for (i = 0; i < n; i++) {
    for (; j <= A[i]; j++) {
      it = P.begin();
      while (true) {
        if (j % (*it) == 0) {
          break;
        }
        if (++it == P.end()) {
          P.push_back(j);
          break;
        }
      }
    }
    it = P.begin();
    while (true) {
      if (A[i] % (*it) == 0 && (*it) != A[i]) {
        break;
      }
      if (++it == P.end()) {
        res++;
        P.push_back(A[i]);
        j = A[i] + 1;
        break;
      }
    }
  }

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

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

#include <math.h>
#include <stdio.h>
int isprime(int x) {
  int i;
  if (x == 2) {
    return 1;
  } else if (x % 2 == 0) {
    return 0;
  }
  i = 3;
  while (i <= sqrt(x)) {
    if (x % i == 0) {
      return 0;
    }
    i += 2;
  }
  return 1;
}
int main() {
  int n, x, i;
  int count = 0;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &x);
    if (isprime(x)) {
      count++;
    }
  }
  printf("%d\\n", count);
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
int isprime(int x) {
  int i = 3;
  if (x == 2) {
    return 1;
  } else if (x % 2 == 0) {
    return 0;
  }
  while (i <= sqrt(x)) {
    if (x % i == 0) {
      return 0;
      i += 2;
    }
  }
  return 1;
}
int main() {
  int n, x, i;
  int count = 0;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &x);
    if (isprime(x)) {
      count++;
    }
  }
  printf("%d\\n", count);
  return 0;
}