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

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

int main() {
  int n;
  cin >> n;
  int input[n];
  for (int i = 0; i < n; i++)
    cin >> input[i];
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 2; j <= sqrt(input[i]); j++) {
      if (input[i] % j == 0) {
        cnt++;
        break;
      }
    }
  }
  cout << n - cnt << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  bool *is_prime = new bool[100000001];
  for (int i = 2; i <= 100000000; i++)
    is_prime[i] = true;
  for (int i = 2; i <= sqrt(100000000); i++)
    if (is_prime[i])
      for (int j = 2; i * j <= 100000000; j++)
        is_prime[i * j] = false;
  int n;
  cin >> n;
  int input[n];
  for (int i = 0; i < n; i++)
    cin >> input[i];
  int cnt = 0;
  for (int i = 0; i < n; i++)
    if (is_prime[input[i]])
      cnt++;
  cout << cnt << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

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

int main() {
  int n, c = 0;
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    int a;
    std::cin >> a;
    if (isPrime(a))
      c++;
  }
  std::cout << c << '\\n';
}


Error: TLE

Fixed submission:

#include <iostream>

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

int main() {
  int n, c = 0;
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    int a;
    std::cin >> a;
    if (isPrime(a))
      c++;
  }
  std::cout << c << '\\n';
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int N, a, ans = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a;
    bool flag = true;
    if (a != 2 && a % 2 == 0 || a == 1) {
      flag = false;
    } else if (a > 2) {
      for (int j = 3; j * j <= a; j += 2) {
        if (a % j == 0) {
          flag = false;
          break;
        }
      }
    }
    if (flag) {
      ans += 1;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int N, a, ans = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a;
    bool flag = true;
    if (a != 2 && a % 2 == 0 || a == 1) {
      flag = false;
    } else if (a != 2) {
      for (int j = 3; j < a; j += 2) {
        if (a % j == 0) {
          flag = false;
          break;
        }
      }
    }
    if (flag) {
      ans += 1;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

// 2015/05/19 Tazoe

#include <iostream>
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;
  cin >> n;

  int cnt = 0;

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

    if (isprime(x)) {
      cnt++;
    }
  }

  cout << cnt << endl;

  return 0;
}

Error: MLE

Fixed submission:

// 2015/05/19 Tazoe

#include <iostream>
using namespace std;

bool is_prime[100000001];

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

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

  int n;
  cin >> n;

  int cnt = 0;

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

    if (is_prime[x]) {
      cnt++;
    }
  }

  cout << cnt << endl;

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

// 2017/05/19 Tazoe

#include <iostream>
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;
  cin >> n;

  int cnt = 0;

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

    if (isprime(x)) {
      cnt++;
    }
  }

  cout << cnt << endl;

  return 0;
}

Error: MLE

Fixed submission:

// 2017/05/19 Tazoe

#include <iostream>
using namespace std;

bool is_prime[100000001]; // ?´???°???????????¨

int main() {
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < 100000001; i++) { // 2??\\???n??\\????????´??°????????????
    is_prime[i] = true;
  }

  for (int i = 2; i * i < 100000001; i++) {
    if (is_prime[i]) { // m????´???°??§???m????????°???????????????
      for (int j = i * i; j < 100000001; j += i) {
        is_prime[j] = false;
      }
    }
  }

  int n;
  cin >> n;

  int cnt = 0;

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

    if (is_prime[x]) {
      cnt++;
    }
  }

  cout << cnt << endl;

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

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

void inputData(int R[], int *N) {
  cin >> *N;
  for (int i = 0; i < *N; i++) {
    cin >> R[i];
  }
}

bool isPrimeNumber(int in) {
  if (in == 2)
    return true;
  if (in % 2 == 0)
    return false;
  int s = (int)sqrt((double)in);

  for (int i = 3; i <= s; i = i + 2) {
    if ((in % i) == 0) {
      return false;
    }
  }
  return true;
}

static const int MAX = 10000;

int main() {
  int R[MAX];
  int N;
  int count = 0;
  inputData(R, &N);
  for (int i = 0; i < N; i++) {
    if (isPrimeNumber(R[i])) {
      count++;
    }
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

void inputData(int R[], int *N) {
  cin >> *N;
  for (int i = 0; i < *N; i++) {
    cin >> R[i];
  }
}

bool isPrimeNumber(int in) {
  if (in == 2)
    return true;
  if (in % 2 == 0)
    return false;
  int s;
  if ((in / 2) % 2 == 0) {
    s = (in / 2) - 1;
  } else {
    s = (in / 2);
  }
  for (int i = s; i >= 3; i = i - 2) {
    if ((in % i) == 0) {
      return false;
    }
  }
  return true;
}

static const int MAX = 10000;

int main() {
  int R[MAX];
  int N;
  int count = 0;
  inputData(R, &N);
  for (int i = 0; i < N; i++) {
    if (isPrimeNumber(R[i])) {
      count++;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool isPrime(std::size_t a) {
  if (a == 2) {
    return true;
  } else if (a % 2 == 0) {
    return false;
  }

  for (std::size_t i = 3; i <= (std::size_t)(sqrt(a)); i = i + 2) {
    if (a % i == 0) {
      return false;
    }
  }

  return true;
}

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

  std::size_t count = 0;
  for (std::size_t i = 0; i < n; ++i) {
    std::size_t a;
    std::cin >> a;
    if (isPrime(a)) {
      count++;
    }
  }

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

  return 0;
}


Error: TLE

Fixed submission:

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

bool isPrime(std::size_t a) {
  if (a == 2) {
    return true;
  } else if (a % 2 == 0) {
    return false;
  }

  for (std::size_t i = 3; i < a / 2; i = i + 2) {
    if (a % i == 0) {
      return false;
    }
  }

  return true;
}

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

  std::size_t count = 0;
  for (std::size_t i = 0; i < n; ++i) {
    std::size_t a;
    std::cin >> a;
    if (isPrime(a)) {
      count++;
    }
  }

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

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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <vector>
using namespace std;

const long long int N = 100000000;

bool solve(long long int x) {

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

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

int main() {

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

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <vector>
using namespace std;

const long long int N = 100000000;
bool prime[N + 1];
void solve() {

  fill(prime, prime + N + 1, true);
  prime[0] = false;
  prime[1] = false;
  for (int i = 2; i < N + 1; i++)
    if (prime[i])
      for (int j = 2 * i; j < N + 1; j += i)
        prime[j] = false;
}

int main() {

  solve();
  int n, cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a = 0;
    cin >> a;
    if (prime[a])
      cnt++;
  }
  cout << cnt << endl;

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

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

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

int main() {
  int n;
  cin >> n;
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    sum += isPrime(t);
  }
  cout << sum << endl;
}

Error: MLE

Fixed submission:

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

int prime[100000001];

void isPrime() {
  for (int i = 0; i < 100000000; ++i) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;

  for (int i = 2; i < 100000000 / 2; ++i) {
    if (prime[i] == 1) {
      for (int j = i + i; j < 100000000; j += i)
        prime[j] = 0;
    }
  }
}

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

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

int isPrime(int x) {

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

int main() {

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

  return 0;
}


Error: TLE

Fixed submission:

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

int isPrime(int x) {
  if (x <= 1) {
    return 0;
  }
  for (int i = 2; i < x - 1; i++) {
    if (x % i == 0)
      return 0;
  }
  return true;
}

int main() {

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

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

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

int main() {
  int n, res = 0;
  cin >> n;
  for (int i = 0, tmp; i < n; i++) {
    cin >> tmp;
    for (int j = 2; j * j <= tmp; j++) {
      if (tmp % j == 0) {
        res++;
        break;
      }
    }
  }
  cout << n - res << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define MAX 100000000
using namespace std;
int eratos[MAX]; // eratos[n - 1] ???0??????n????´???°???1???????´???°??§??????

void Eratos() {
  eratos[0] = 1;
  for (int i = 1; i * i <= MAX; i++) {
    if (eratos[i - 1] == 0) {
      for (int j = i * 2; j <= MAX; j += i) {
        eratos[j] = 1;
      }
    }
  }
}

int main() {
  int n, res;
  Eratos();
  cin >> n;
  for (int i = 0, tmp; i < n; i++) {
    cin >> tmp;
    if (eratos[tmp - 1] == 0)
      res++;
  }
  cout << res << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) for (int i = 0; i < (a); i++)

bool IsPrime(int n) {
  FOR(i, 2, int(sqrt(n)) + 1) {
    if (n % i == 0)
      return false;
  }
  return true;
}

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

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

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) for (int i = 0; i < (a); i++)

int main() {
  bool primes[10001] = {};

  FOR(i, 2, 100 + 1) {
    if (primes[i] == 0) {
      FOR(j, 2, (10000 / i) + 1) { primes[i * j] = 1; }
    }
  }

  int n;
  cin >> n;

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

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

int prime(int p) {
  int i, flag = 0;
  if (p == 2) {
    return 0;
  } else {
    for (i = 2; i <= sqrt(p); i++) {
      if (p % i == 0) {
        flag = 1;
        break;
      }
    }
    return flag;
  }
}

int main() {
  int n, i, p, P = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> p;
    P = P + prime(p);
  }
  cout << n - P << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int prime(int p) {
  int i, flag = 0;
  if (p == 2) {
    return 0;
  } else {
    for (i = 2; i < p; i++) {
      if (p % i == 0) {
        flag = 1;
        break;
      }
    }
    return flag;
  }
}

int main() {
  int n, i, p, P = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> p;
    P = P + prime(p);
  }
  cout << n - P << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

int main() {
  const int N = 10000;
  int n;
  cin >> n;

  int seisu[N];
  int counter = 0;

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

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

int main() {
  int N, max = 2, ret = 0, i, j;
  cin >> N;
  vector<int> a(N);
  for (i = 0; i < N; i++) {
    cin >> a[i];
    if (max < a[i])
      max = a[i];
  }
  vector<bool> p(max + 1, true);
  for (i = 2; i * i <= max; i++) {
    if (p[i])
      for (j = i * 2; j <= max; j += i)
        p[j] = false;
  }
  for (i = 0; i < N; i++) {
    if (p[a[i]])
      ret++;
  }
  cout << ret << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

vector<int> p;
bool isPN(int n) {
  int i, j;
  bool ret = false;
  for (i = 0; i < p.size() && p[i] <= n; i++) {
    if (n == p[i]) {
      ret = true;
      break;
    } else if (n % p[i] == 0)
      break;
  }
  if (i >= p.size() || p[i] > n) {
    if (p[p.size() - 1] < n) {
      for (i = p[p.size() - 1] + 2; i <= n; i += 2) {
        for (j = 0; j < p.size() && p[j] * p[j] < i; j++) {
          if (i % p[j] == 0)
            break;
        }
        if (j >= p.size() || p[j] * p[j] >= i) {
          p.push_back(i);
          if (i == n) {
            ret = true;
            break;
          }
        }
      }
    }
  }
  return ret;
}
int main() {
  int N, ret = 0, i, t;
  p.push_back(2);
  p.push_back(3);
  cin >> N;
  for (i = 0; i < N; i++) {
    cin >> t;
    if (isPN(t))
      ret++;
  }
  cout << ret << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool IsPrime(int a) {
  if (a == 2)
    return true;

  if (a < 2 || a % 2 == 0)
    return false;

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

  return true;
}

int main() {
  int n;
  cin >> n;
  int a[n], i, j = 0;

  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (IsPrime(a[i]))
      j++;
  }
  cout << j << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int PrimeNumber(int a) {
  int b = a, i;

  for (i = 2; i < a; i++) {
    while (b % i == 0)
      b = b / i;
    if (b == 1) {
      return 0;
    }
  }
  return 1;
}

int main() {
  int n;
  cin >> n;
  int a[n], i, j = 0;

  for (i = 0; i < n; i++) {
    cin >> a[i];
    j += PrimeNumber(a[i]);
  }
  cout << j << endl;

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


#include <bits/stdc++.h>

using namespace std;

const int maxn = 50001;

int prime[maxn], cnt, res;
bool isprime[maxn];

void maketable() {
  int base = (int)sqrt((double)maxn);
  isprime[0] = isprime[1] = 1;
  for (int i = 2; i < maxn; i++) { // 此处i<maxn与以前的不同
    if (!isprime[i]) {
      prime[cnt++] = i;
      for (int j = i + i; j < maxn; j += i) {
        isprime[j] = 1;
      }
    }
  }
}

void solve(int n) {
  int k = (int)sqrt((double)n);
  for (int i = 0; prime[i] <= k; i++) {
    if (n % prime[i] == 0) {
      return;
    }
  }
  res++;
}

int main() {
  maketable();
  int n, s;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &s);
    solve(s);
  }
  printf("%d\\n", res);
  return 0;
}


Error: 0

Fixed submission:


#include <bits/stdc++.h>

using namespace std;

const int maxn = 50001;

int prime[maxn], cnt, res;
bool isprime[maxn];

void maketable() {
  int base = (int)sqrt((double)maxn);
  isprime[0] = isprime[1] = 1;
  for (int i = 2; i < base; i++) {
    if (!isprime[i]) {
      prime[cnt++] = i;
      for (int j = i + i; j < maxn; j += i) {
        isprime[j] = 1;
      }
    }
  }
}

void solve(int n) {
  int k = (int)sqrt((double)n);
  for (int i = 0; prime[i] <= k; i++) {
    if (n % prime[i] == 0) {
      return;
    }
  }
  res++;
}

int main() {
  maketable();
  int n, s;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &s);
    solve(s);
  }
  printf("%d\\n", res);
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define MAX 100000001
/*
void eratos(bool *p){
        for(int i=1; i<MAX; ++i){
                p[i] = true;
        }
        for(int i=2; i*i<MAX; ++i){
                for(int j=2; i*j<MAX; ++j){
                        p[i*j] = false;
                }
        }
}
*/

int main() {
  int n, sum = 0;
  vector<bool> p(MAX);
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }

  for (int i = 1; i < MAX; ++i) {
    p[i] = true;
  }

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

  for (int i = 0; i < n; ++i) {
    if (p[arr[i]] == true)
      ++sum;
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define MAX 100000001

void eratos(bool *p) {
  for (int i = 1; i < MAX; ++i) {
    p[i] = true;
  }
  for (int i = 2; i * i < MAX; ++i) {
    for (int j = 2; i * j < MAX; ++j) {
      p[i * j] = false;
    }
  }
}

int main() {
  int n, sum = 0;
  vector<bool> p(MAX);
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }

  for (int i = 1; i < MAX; ++i) {
    p[i] = true;
  }
  for (int i = 2; i * i < MAX; ++i) {
    for (int j = 2; i * j < MAX; ++j) {
      p[i * j] = false;
    }
  }

  for (int i = 0; i < n; ++i) {
    if (p[arr[i]] == true)
      ++sum;
  }
  cout << sum << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

bool is_prime(int num) {
  if (num == 2 || num == 3 || num == 5 || num == 7)
    return true;
  if (num % 2 == 0)
    return false;
  for (int i = 3; i * i <= num; i += 1)
    if (num % i == 0)
      return false;
  return true;
}

int main() {
  int N, num, count;

  count = 0;

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

  return 0;
}


Error: -11

Fixed submission:

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

using namespace std;

bool is_prime[10000000];
int primes[100000000];
int prime_count = 0;

void set_prime() {
  int j, end_point;
  bool flag;
  for (int i = 5; i < 100000000; i += 2) {
    flag = false;
    end_point = (int)sqrt((double)i);

    for (j = 0; j < prime_count && primes[j] <= end_point; j++) {
      if (i % primes[j] == 0) {
        flag = true;
        break;
      }
    }
    if (!flag) {
      is_prime[i] = true;
      primes[prime_count++] = i;
    }
  }
}

int main() {
  for (int i = 2; i <= 3; i++) {
    is_prime[i] = true;
    primes[prime_count++] = i;
  }
  set_prime();
  int N, num, count;

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

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

#include <iostream>
using namespace std;

int main() {
  int T;
  long long s[200005];
  cin >> T;
  for (int i = 0; i < T; i++)
    cin >> s[i];
  long long max1 = -2000000001, min1 = s[0];
  for (int i = 1; i < T; i++) {
    max1 = max(max1, s[i] - min1);
    min1 = min(min1, s[i]);
  }
  cout << max1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int T;
  long long s[200005];
  cin >> T;
  for (int i = 0; i < T; i++)
    cin >> s[i];
  long long max = -2000000001;
  for (int i = T - 1; i >= 0; i--) {
    for (int j = i - 1; j >= 0; j--) {
      if (s[i] - s[j] > max) {
        max = s[i] - s[j];
      }
    }
  }
  cout << max << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

int main() {
  int n, max, min, mins;
  std::cin >> n;
  int R[n];
  for (int t = 0; t <= n - 1; t++) {
    std::cin >> R[t];
  }

  min = R[0];
  mins = 0;
  for (int j = 1; j <= n - 1; j++) {
    for (int i = j - 1; i < j; i++) {
      if (R[i] <= min) {
        min = R[i];
        mins = i;
      }
      if (j == 1) {
        max = R[j] - min;
      }

      if (R[j] - min > max) {
        max = R[j] - min;
      }
      /*			if(R[j]<R[j-1]){
                                      break;
                              }
                              if( R[i]<=min && R[j]-R[i]>max){
                                      max=R[j]-R[i];
                                      min=R[i];

                              }*/
    }
  }
  std::cout << max << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

int main() {
  int n, max, min;
  std::cin >> n;
  int R[n];
  for (int t = 0; t <= n - 1; t++) {
    std::cin >> R[t];
  }

  min = R[0];
  for (int j = 1; j <= n - 1; j++) {
    for (int i = 0; i < j; i++) {
      if (j == 1) {
        max = R[j] - R[i];
      }
      if (R[j] < R[j - 1]) {
        break;
      }
      if (R[i] <= min && R[j] - R[i] > max) {
        max = R[j] - R[i];
        min = R[i];
      }
    }
  }
  std::cout << max << "\\n";

  return 0;
}