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

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

int main() {
  int n;
  cin >> n;
  vector<int> data(n);
  for (auto &a : data) {
    cin >> a;
  }
  int check = 0;
  for (int i = 0; i < n; i++) {
    int count = 0;
    for (int j = 2; j <= sqrt(data[i]); j++) {
      if (data[i] % j == 0) {
        count++;
        j = data[i];
      }
    }
    if (!count)
      check++;
  }
  cout << check << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> data(n);
  for (auto &a : data) {
    cin >> a;
  }
  int check = 0;
  for (int i = 0; i < n; i++) {
    int count = 0;
    for (int j = 2; j < data[i]; j++) {
      if (data[i] % j == 0) {
        count++;
        j = data[i];
      }
    }
    if (!count)
      check++;
  }
  cout << check << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_C
// Getting Started - Prime Numbers

#define SUBMIT

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <numeric>
#include <set>
#include <utility>
#include <vector>

using namespace std;
using i64 = long long;

const int MAX_N = 20;
bool is_prime_number[MAX_N + 1];
bool checked[MAX_N + 1];

// this problem constraint shows the judged value x is 2 <= x <= 10^8.
// so eratos is not worked.
void eratos() {
  is_prime_number[0] = false, checked[0] = true;
  is_prime_number[1] = false, checked[1] = true;
  for (int i = 2; i <= MAX_N; ++i) {
    if (!checked[i]) {
      is_prime_number[i] = true;
      for (int k = 2 * i; k <= MAX_N; k += i) {
        is_prime_number[k] = false;
        checked[k] = true;
      }
    }
  }
}

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

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

  return true;
}

int n;

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


Error: 0

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_C
// Getting Started - Prime Numbers

#define SUBMIT

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <numeric>
#include <set>
#include <utility>
#include <vector>

using namespace std;
using i64 = long long;

const int MAX_N = 10000;
bool is_prime_number[MAX_N + 1];
bool checked[MAX_N + 1];

void eratos() {
  is_prime_number[0] = false, checked[0] = true;
  is_prime_number[1] = false, checked[1] = true;
  for (int i = 2; i <= MAX_N; ++i) {
    if (!checked[i]) {
      is_prime_number[i] = true;
      for (int k = 2 * i; k <= MAX_N; k += i) {
        is_prime_number[k] = false;
        checked[k] = true;
      }
    }
  }
}

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

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

  return true;
}

int n;

int main() {
  cin >> n;

  eratos();
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    if (is_prime_number[x])
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>

using namespace std;
const int MAX = 10e4;
// 入力
int N, x;

bool prime[MAX];
int ans;
bool flag;
void init() {
  for (int i = 0; i < MAX; i++)
    prime[i] = true;
  prime[0] = prime[1] = false;
  for (int i = 2; i < MAX;) {
    if (prime[i]) {
      for (int j = 2 * i; j < MAX; j += i)
        prime[j] = false;
    }
    if (i == 2)
      i++;
    else
      i += 2;
  }
}

int main() {
  cin >> N;
  ans = 0;
  init();
  while (N--) {
    cin >> x;
    if (x < MAX) {
      if (prime[x])
        ans++;
    } else {
      if (x % 2 != 0) {
        flag = true;
        for (int i = 3; i < MAX; i += 2) {
          if (x % i == 0) {
            flag = false;
            break;
          }
        }
        if (flag)
          ans++;
      }
    }
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <iostream>

using namespace std;
const int MAX = 10e8;
// 入力
int N, x;

bool prime[MAX];
int ans;
void init() {
  for (int i = 0; i < MAX; i++)
    prime[i] = true;
  prime[0] = prime[1] = false;
  for (int i = 2; i < MAX;) {
    if (prime[i]) {
      for (int j = 2 * i; j < MAX; j += i)
        prime[j] = false;
    }
    if (i == 2)
      i++;
    else
      i += 2;
  }
}

int main() {
  cin >> N;
  ans = 0;
  init();
  while (N--) {
    cin >> x;
    if (prime[x])
      ans++;
  }
  cout << ans << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

using namespace std;

static bool IsPrime(int x) {
  if (1 < x && x < 4)
    return true;
  const int z = static_cast<int>(ceil(sqrt(x)));
  for (int i = 2; i <= z; ++i) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n;

  cin >> n;

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

Error: TLE

Fixed submission:

#include <iostream>
#include <unordered_set>

using namespace std;

inline bool IsPrimeSlow(int x) {
  for (int i = x - 1; i > 1; --i)
    if (x % i == 0)
      return false;

  return true;
}

static bool IsPrime(int x) {
  static unordered_set<int> primes = {2, 3, 5, 7, 11, 13, 17, 19};

  if (primes.find(x) != primes.end())
    return true;

  for (auto p : primes) {
    if (x % p == 0)
      return false;
  }

  if (IsPrimeSlow(x)) {
    primes.insert(x);
    return true;
  }

  return false;
}

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

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

#include <bits/stdc++.h>

int isprime(int x) {

  if (x == 2)
    return 1;
  else if ((x < 2) || (x % 2 == 0))
    return 0;

  int i = 3;

  while (i <= std::sqrt((double)x)) {
    if ((x % i) == 0)
      return 0;
    i = i + 2;
  }

  return 1;
}

int main() {

  int count = 0;
  int n, x;

  std::cin >> n;

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

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

int main() {

  int count = 0;
  int n, x;

  std::cin >> n;

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

    if (x == 1)
      continue;

    for (int j = 2; j <= x; j++) {
      if (j == x)
        count++;
      else if ((x % j) == 0)
        break;
    }
  }

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

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

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  int n, tmp, count = 0;
  int lim;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> tmp;
    lim = sqrt(tmp);
    for (int j = 2; j <= lim; j++) {
      if (tmp % j == 0) {
        count++;
        break;
      }
    }
  }
  cout << n - count << "\\n";
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;
  int x;
  int check = 0;
  int cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    for (int j = 1; j <= x; j++) {
      if (x % j == 0) {
        check++;
      }
    }
    if (check == 2) {
      cnt++;
    }
    check = 0;
  }
  cout << cnt << "\\n";
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int isPrime(int n);

int main() {
  string str;
  getline(cin, str);
  int num;
  stringstream num_ss(str);
  num_ss >> num;

  int counter = 0;

  for (int i = 0; i < num; i++) {
    getline(cin, str);
    stringstream tmp_ss(str);
    int n;
    tmp_ss >> n;
    n = isPrime(n);
    if (n != -1) {
      counter++;
    }
  }
  cout << counter << endl;
  return 0;
}

// if the param is a prime number, then return param. Else return -1.
int isPrime(int n) {
  int rtn = n;
  int end_num = (int)sqrt(n) + 1;

  if (n % 2 == 0) {
    if (n == 2) {
      return n;
    } else {
      return -1;
    }
  }

  for (int i = 3; i < end_num; i += 2) {
    if (n % i == 0) {
      rtn = -1;
      break;
    }
  }
  return rtn;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <sstream>

using namespace std;

int isPrime(int n);

int main() {
  string str;
  getline(cin, str);
  int num;
  stringstream num_ss(str);
  num_ss >> num;

  int counter = 0;

  for (int i = 0; i < num; i++) {
    getline(cin, str);
    stringstream tmp_ss(str);
    int n;
    tmp_ss >> n;
    n = isPrime(n);
    if (n != -1) {
      counter++;
    }
  }
  cout << counter << endl;
  return 0;
}

int isPrime(int n) {
  int rtn = n;
  int end_num = (n / 2) + 1;

  if (n % 2 == 0) {
    if (n == 2) {
      return n;
    } else {
      return -1;
    }
  }

  for (int i = 3; i < end_num; i += 2) {
    if (n % i == 0) {
      rtn = -1;
      break;
    }
  }
  return rtn;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main() {
  int i, j, s, p, tmp;

  int primes[1230];
  primes[i = 0] = 2;
  for (p = 3; p <= 10007; p += 2) {
    s = sqrt((double)p);
    for (j = 0; j <= i; j++) {
      tmp = primes[j];
      if (tmp > s) {
        i++;
        primes[i] = p;
        break;
      } else if (p % tmp == 0) {
        break;
      }
    }
  }

  int N;
  cin >> N;

  int n, count = 0;

  for (i = 0; i < N; i++) {
    cin >> n;
    s = sqrt((double)n);
    for (int p : primes) {
      if (p > s) {
        count++;
        break;
      } else if (n % p == 0) {
        break;
      }
    }
  }

  cout << count << endl;

  return 0;
}


Error: 0

Fixed submission:

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

using namespace std;

int main() {
  int i, j, s, p, tmp;

  int primes[1229];
  primes[i = 0] = 2;
  for (p = 3; p < 10000; p += 2) {
    s = sqrt((double)p);
    for (j = 0; j <= i; j++) {
      tmp = primes[j];
      if (tmp > s) {
        i++;
        primes[i] = p;
        break;
      } else if (p % tmp == 0) {
        break;
      }
    }
  }

  int N;
  cin >> N;

  int n, count = 0;

  for (i = 0; i < N; i++) {
    cin >> n;
    s = sqrt((double)n);
    for (int p : primes) {
      if (p > s) {
        count++;
        break;
      } else if (n % p == 0) {
        break;
      }
    }
  }

  cout << count << endl;

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

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

int main() {
  int n;
  cin >> n;
  int A[n], count = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < n; i++) {
    int L = A[i];
    int M = sqrt(L);
    for (int x = 2; x <= M; x++) {
      if (L % x == 0) {
        count--;
        break;
      }
    }
    count++;
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int A[n], count = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < n; i++) {
    int L = A[i];
    int M = sqrt(L);
    for (int x = 2; x < L; x++) {
      if (L % x == 0) {
        count--;
        break;
      }
    }
    count++;
  }
  cout << count << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> IVEC;

int seive(IVEC &A, int n) {
  IVEC num(n + 1, 1);
  num[0] = 0;
  num[1] = 0;
  int count = 1, limit = int(sqrt(n));
  for (int i = 3; i <= n; i += 2)
    if (num[i]) {
      count++;
      if (i <= limit)
        for (int j = 3 * i; j <= n; j += 2 * i)
          num[j] = 0;
    }

  A.resize(count);
  A[0] = 2;
  int j = 1;
  for (int i = 3; i <= n; i += 2)
    if (num[i] == 1)
      A[j++] = i;
  return 0;
}

int main(void) {
  IVEC primes;
  int max_num = 10000;
  seive(primes, max_num);

  int n, n0, lim, count;
  cin >> n;
  count = n;
  for (int i = 0; i < n; i++) {
    cin >> n0;
    lim = int(sqrt(n0));
    for (int j = 0; j < (int)primes.size(); j++) {
      int tmp = primes[j];
      if (tmp * tmp > n0)
        break;
      if (n0 % tmp == 0) {
        count--;
        break;
      }
    }
  }
  cout << count << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> IVEC;

int seive(IVEC &A, int n);

int main(void) {
  IVEC primes, nums;
  int n;

  cin >> n;
  nums.resize(n);
  for (int i = 0; i < n; i++)
    cin >> nums[i];
  sort(nums.begin(), nums.end());

  int max_num = nums[nums.size() - 1];
  seive(primes, max_num);

  for (int i = 0; i < (int)primes.size(); i++)
    for (int j = 0; j < (int)nums.size(); j++)
      if ((nums[j] >= pow(primes[i], 2)) && (nums[j] % primes[i] == 0))
        nums[j] = 0;

  int count = 0;
  for (int i = 0; i < (int)nums.size(); i++)
    if (nums[i])
      count++;

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

int seive(IVEC &A, int n) {
  IVEC num(n + 1, 1);
  num[0] = 0, num[1] = 0;
  int count = 1, limit = int(sqrt(n));
  for (int i = 3; i <= n; i += 2)
    if (num[i]) {
      count++;
      if (i <= limit)
        for (int j = 3 * i; j <= n; j += 2 * i)
          num[j] = 0;
    }

  A.resize(count);
  A[0] = 2;
  int j = 1;
  for (int i = 3; i <= n; i += 2)
    if (num[i] == 1)
      A[j++] = i;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
bool checkNotOdd(int n) {
  if (n % 2 == 0) {
    return false;
  } else {
    return true;
  }
}
bool check(int n) {
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  int t = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    bool flg = true;
    flg = checkNotOdd(a);
    if (flg) {
      flg = check(a);
    }
    if (flg || a == 2) {
      t++;
    }
  }
  cout << t << endl;
}

Error: TLE

Fixed submission:

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

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int main() {
  int x, y, n, b = 0, o;
  cin >> n;
  for (o = 0; o < n; o++) {
    cin >> x;
    for (y = 2; y * y <= x; y++) {
      if (x % y == 0) {
        goto loop;
      }
    }
    b++;
  loop:;
  }
  cout << b << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int main() {
  int x, y, n, b = 0, o;
  cin >> n;
  for (o = 0; o < n; o++) {
    cin >> x;
    for (y = 2; y < x; y++) {
      if (x % y == 0) {
        goto loop;
      }
    }
    b++;
  loop:;
  }
  cout << b << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, a[10010];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == 2)
      sum++;
    else {
      int k = ceil(sqrt(a[i]));
      for (int j = 2; j <= k; j++) {
        if (a[i] % j == 0)
          break;
        if (j == k)
          sum++;
      }
    }
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, a[10010];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == 2)
      sum++;
    else {
      for (int j = 2; j < a[i]; j++) {
        if (a[i] % j == 0)
          break;
        if (j == a[i] - 1)
          sum++;
      }
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <array>
#include <cmath>
#include <iostream>

using namespace std;

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

int main() {
  int len;
  cin >> len;
  array<int, 10000> src;

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

  int primeNumCount = 0;
  for (int i = 0; i < len; i++) {
    if (IsPrime(src[i]))
      primeNumCount++;
  }

  cout << primeNumCount << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

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

int main() {
  int len;
  cin >> len;
  vector<int> src(len);

  for (int &a : src)
    cin >> a;

  int primeNumCount = 0;
  for (int a : src) {
    if (IsPrime(a))
      primeNumCount++;
  }

  cout << primeNumCount << endl;

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

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

bool is_prime(long x) {
  long d = ceil(sqrt(x));
  if (x == d)
    d--;
  while (d > 1 && (x % d) != 0) {
    d--;
  }
  return (d == 1);
}

int main(int argc, char **argv) {
  int N;
  cin >> N;

  long x;
  int count = 0;
  for (int i = 0; i < N; i++) {
    cin >> x;
    if (is_prime(x))
      count++;
  }
  cout << count << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool is_prime(long x) {
  long d = x - 1;
  while (d > 1 && (x % d) != 0) {
    d--;
  }
  return (d == 1);
}

int main(int argc, char **argv) {
  int N;
  cin >> N;

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

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

using namespace std;

bool Sieve(int x) {
  for (int i = 2; i <= sqrt(x); i++)
    if (x % i == 0)
      return false;
  return true;
}
int main() {
  int n;
  vector<int> items;
  // input
  cin >> n;
  items.resize(n);
  for (int i = 0; i < n; i++)
    cin >> items[i];

  // process
  int count = 0;
  for (int i = 0; i < n; i++)
    if (Sieve(items[i]))
      count++;
  cout << count << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

bool Sieve(int x) {
  for (int i = 2; i <= sqrt(x); i++)
    if (x % i == 0)
      return false;
  return true;
}
int main() {
  int n;
  vector<int> items;
  // input
  cin >> n;
  items.resize(n);
  for (int i = 0; i < n; i++)
    cin >> items[i];

  // process
  int count = 0;
  for (int y : items)
    if (Sieve(y))
      count++;
  cout << count << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool sosu(int n) {
  if (n < 2)
    return false; // 2以下除く
  else if (n == 2)
    return true; // 2は素数
  else if (n % 2 == 0)
    return false; // 偶数除く
  int root = sqrt(n);
  for (int i = 3; i <= root; i += 2) { // 奇数で割る
    if (n % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (sosu(x) == true)
      sum++;
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool sosu(int n) {
  if (n == 2)
    return true;
  else if (n % 2 == 0)
    return false;

  int kisu = 3;
  while (1) {
    if (kisu >= n)
      break;
    if (n % kisu == 0)
      return false;
    kisu += 2;
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (sosu(x) == true)
      sum++;
  }
  cout << sum << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

bool is_prime(const unsigned n) {
  switch (n) {
  case 0:
    return false;
  case 1:
    return false;
  case 2:
    return true;
  }

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

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

  return true;
}

int main() {
  int cnt = 0;
  int n;
  cin >> n;

  int s[n];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    if (is_prime(s[i]))
      cnt++;
  }

  cout << cnt << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define lim 100000001

int main() {
  int cnt = 0;
  int n;
  cin >> n;

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

  bool *is_prime = new bool[lim];

  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i <= max; i++) {
    is_prime[i] = true;
  }

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

  for (int i = 0; i < n; i++) {
    if (is_prime[s[i]]) {
      cnt++;
    }
  }

  cout << cnt << "\\n";

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

#include <math.h>
#include <stdio.h>
#define MAX 10000000

int main() {
  int i, j, n, s, ary[10000], y;
  double x;

  scanf("%d", &n);
  s = n;
  for (i = 0; i < n; i++) {
    scanf("%d", &ary[i]);
  }
  for (i = 0; i < n; i++) {
    for (j = 2; j <= sqrt(ary[i]); j++) {
      if (ary[i] % j == 0) {
        s = s - 1;
        break;
      }
    }
  }
  printf("%d\\n", s);
}

Error: -11

Fixed submission:

#include <math.h>
#include <stdio.h>
#define MAX 10000000
int main() {
  int i, j, n, s, ary[10000], y;
  bool a[MAX];
  double x;

  for (i = 0; i < MAX; i++) {
    a[i] = i > 1 ? 1 : 0;
  }
  x = sqrt(MAX);
  y = (int)x;
  for (i = 2; i < y; i++) {
    if (a[i]) {
      for (j = 0; i * (j + 2) < MAX; j++) {
        a[i * (j + 2)] = 0;
      }
    }
  }

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

  for (i = 0; i < n; i++) {
    if (a[ary[i]])
      s++;
  }
  printf("%d\\n", s);
}