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

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

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

  int cnt = 0;
  int num = 0;
  for (int i = 0; i < N; i++) {
    cin >> num;
    if (num == 2) {
      cnt += 1;
      continue;
    }
    if (num % 2 == 0) {
      continue;
    }

    double sqrtnum = sqrt(num);
    for (int j = 3; j <= sqrtnum; j += 2) {
      if (num % j == 0) {
        num = 0;
        break;
      }
    }
    if (num != 0) {
      cnt += 1;
    }
  }

  cout << cnt << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  int cnt = 0;
  int num = 0;
  for (int i = 0; i < N; i++) {
    cin >> num;
    for (int j = 1; j < num; j++) {
      if (j == 1) {
        continue;
      }
      if (num % j == 0) {
        num = 0;
        break;
      }
    }
    if (num != 0) {
      cnt += 1;
    }
  }

  cout << cnt << endl;

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

template <typename t1, class t2> void input(t1 count, t2 array) {
  for (t1 i = 0; i < count; i++) {
    cin >> array[i];
  }
}

template <typename t1, class t2> void output(t1 count, t2 array) {
  for (t1 i = 0; i < count; i++) {
    cout << array[i];
    if (i != count - 1)
      cout << " ";
  }
  cout << endl;
}

ll gcd(ll a, ll b) {
  int x = min(a, b), y = max(a, b);
  if (x == 0)
    return y;
  else
    return gcd(x, y % x);
}

set<ll> prime;

bool isPrime(ll x) {
  for (auto e : prime) {
    if (e >= x)
      return true;
    if (x % e == 0)
      return false;
  }
  return true;
}

int main(void) {
  bool flag;
  for (int i = 2; i <= 100000; i++) {
    flag = true;
    for (auto e : prime) {
      if (i % e == 0) {
        flag = false;
        break;
      }
    }
    if (flag)
      prime.insert(i);
  }
  int n, t, c = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> t;
    if (isPrime(t))
      c++;
  }
  cout << c << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

template <typename t1, class t2> void input(t1 count, t2 array) {
  for (t1 i = 0; i < count; i++) {
    cin >> array[i];
  }
}

template <typename t1, class t2> void output(t1 count, t2 array) {
  for (t1 i = 0; i < count; i++) {
    cout << array[i];
    if (i != count - 1)
      cout << " ";
  }
  cout << endl;
}

ll gcd(ll a, ll b) {
  int x = min(a, b), y = max(a, b);
  if (x == 0)
    return y;
  else
    return gcd(x, y % x);
}

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

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

#include <cmath>
#include <iostream>

using namespace std;

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

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

  int count = 0;

  for (int i = 0; i < n; i++) {
    if (isPrime(array[i])) {
      count++;
    }
  }
  cout << count << "\\n";
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

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

  int count = 0;

  for (int i = 0; i < n; i++) {
    if (isPrime(array[i])) {
      count++;
    }
  }
  cout << count << "\\n";
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>

bool isPrime(int n) {
  bool flg = true;

  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      flg = false;
      break;
    }
  }
  return flg;
}

int main() {

  int n, cnt = 0;
  std::cin >> n;

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

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>

#define MAX 100000001

bool *isPrime = new bool[MAX];

void init() {
  for (int i = 2; i < MAX; i++)
    isPrime[i] = true;

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

int main() {

  init();

  int n, cnt = 0;
  std::cin >> n;

  for (int i = 0, a; i < n; i++) {
    std::cin >> a;
    if (isPrime[a])
      cnt++;
  }

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

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

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

bool isPrime(int prime);

int main() {
  int N, i, j, count = 0, flag, prime;
  // int prime[MAX];
  bool primeFlag;

  cin >> N;
  for (i = 0; i < N; i++) {
    cin >> prime;
    primeFlag = isPrime(prime);
    if (primeFlag == true)
      count++;
  }

  cout << count << endl;

  return 0;
}

//?卒???属???????????蔵??属
bool isPrime(int prime) {
  int i;
  double j;

  j = sqrt((double)prime);

  if (prime == ((int)j * (int)j))
    return false;

  if (prime == 2)
    return true;
  if (prime == 3)
    return true;

  if ((prime % 2) == 0)
    return false;
  if ((prime % 3) == 0)
    return false;

  for (i = 3; (double)i < j; i += 2) {
    if ((prime % i) == 0) {
      return false;
    }
  }
  return true;
}

Error: TLE

Fixed submission:

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

bool isPrime(int prime);

int main() {
  int N, i, j, count = 0, flag, prime;
  // int prime[MAX];
  bool primeFlag;

  cin >> N;
  for (i = 0; i < N; i++) {
    cin >> prime;
    primeFlag = isPrime(prime);
    if (primeFlag == true)
      count++;
  }

  cout << count << endl;

  return 0;
}

//?卒???属???????????蔵??属
bool isPrime(int prime) {
  int i, j;

  // j = sqrt((double)prime);

  if (prime == 2)
    return true;
  if ((prime % 2) == 0)
    return false;

  for (i = 3; i < (prime / 2); i += 2) {
    if ((prime % i) == 0) {
      return false;
    }
  }
  return true;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>

using namespace std;

int isPrime(int a) {
  int boundaly = 2;
  if (a < boundaly)
    return false;
  if (a == boundaly)
    return true;
  if (a % 2 == 0)
    return false;

  double sqNum = sqrt(a);
  for (int i = 3; i <= sqNum; i += boundaly) {
    if (a % i == 0)
      return false;
  }

  return true;
}

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

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


Error: -8

Fixed submission:

#include <cmath>
#include <cstdio>

using namespace std;

int isPrime(int a) {
  int boundaly = 2;
  if (a == boundaly)
    return true;
  if (a < boundaly)
    return false;
  if (a % 2 == 0)
    return false;

  double sqNum = sqrt(a);
  for (int i = 0; i < sqNum - 1; i += boundaly) {
    if (a % i == 0)
      return false;
  }

  return true;
}

int main() {
  int input;
  int ans = 0;
  while ((scanf("%d", &input)) != EOF) {
    if (isPrime(input))
      ans++;
  }

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

#include <cmath>
#include <iostream>

int gcd(long unsigned int a, long unsigned int b) {
  // assuming a > b
  if (a % b == 0)
    return b;

  return gcd(b, a % b);
}

bool is_prime(long unsigned int n) {
  for (long unsigned int i = 2; i < n; i++)
    if (gcd(n, i) != 1)
      return false;
  return true;
}

bool is_prime_A(long unsigned int n) {
  for (long unsigned int i = 2; i <= std::sqrt(n); i++)
    if (gcd(n, i) != 1)
      return false;
  return true;
}

bool is_prime_E(long unsigned int n) { return true; }

int main() {

  {
    using namespace std;

    long unsigned int n;
    cin >> n;

    int c = 0;
    for (long unsigned int i = 0; i < n; i++) {
      long unsigned int a;
      cin >> a;
      if (is_prime_A(a))
        c++;
    }
    cout << c << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

int gcd(long unsigned int a, long unsigned int b) {
  // assuming a > b
  if (a % b == 0)
    return b;

  return gcd(b, a % b);
}

bool is_prime(long unsigned int n) {
  for (long unsigned int i = 2; i < n; i++)
    if (gcd(n, i) != 1)
      return false;
  return true;
}

bool is_prime_E(long unsigned int n) { return true; }

int main() {

  {
    using namespace std;

    long unsigned int n;
    cin >> n;

    int c = 0;
    for (long unsigned int i = 0; i < n; i++) {
      long unsigned int a;
      cin >> a;
      if (is_prime(a))
        c++;
    }
    cout << c << endl;
  }

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

//
//
//  prime.cpp - created by us162022
//
#include <iostream>
#include <math.h>
using namespace std;

int main(int argc, char *argv[]) {
  int n, x, count = 0;
  cin >> n;

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

    if (x % 2) {
      bool flag = true;
      for (int j = 3; j <= sqrt(x); j += 2) {
        if (!(x % j)) {
          flag = false;
          break;
        }
      }

      if (flag)
        count++;

    }

    else {
      if (x == 2)
        count++;
    }
  }

  cout << count << endl;
}

Error: TLE

Fixed submission:

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

int main(int argc, char *argv[]) {
  int n, x, count = 0;
  cin >> n;

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

    if (!(x % 2)) {
      bool flag = true;
      for (int j = 3; i < sqrt(x); j += 2) {
        if (!(x % j)) {
          flag = false;
          break;
        }
      }
      if (flag)
        count++;
    }
  }

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

#include <complex>
#include <iostream>

using namespace std;

bool isPrimeNumber(int n) {
  if (n == 2)
    return true;

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

  int i = 3;
  while (i <= sqrt(n))
    if (n % i == 0)
      return false;
    else
      i += 2;

  return true;
}

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

  for (int i = 0; i < n; i++) {
    cin >> tmp;
    if (isPrimeNumber(tmp))
      count++;
  }

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

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

bool isPrimeNumber(int n) {
  if (n < 2)
    return false;

  bool notExists = true;
  for (int i = 2; i < n / 2 + 1 && notExists; i++)
    notExists = n % i != 0;

  return notExists;
}

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

  for (int i = 0; i < n; i++) {
    cin >> tmp;
    if (isPrimeNumber(tmp))
      count++;
  }

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

#include <cmath>
#include <iostream>

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

int main() {
  int n, count = 0;
  int x;
  std::cin >> n;

  for (int i = 0; i < n; i++) {
    std::cin >> x;
    if (isPrime(x) == true) {
      count++;
    }
  }
  std::cout << count << std::endl;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
#define MAX 100000000
bool prime[MAX + 1];
int main() {
  int n, count = 0;
  int i, j;
  std::cin >> n;

  for (i = 0; i <= MAX; i++) {
    prime[i] = true;
  }
  prime[0] = false;
  prime[1] = false;

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

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

#include <cmath>
#include <iostream>

using namespace std;

int main() {
  int i, c, j, n, count = 0, number[10000];

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> number[i];
    c = 0;
    for (j = 1; j <= sqrt(number[i]); j++) {
      if (number[i] % j == 0) {
        c++;
      }
    }
    if (c == 1) {
      count++;
    }
  }
  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int i, j, n, c, count = 0, number[10000];

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> number[i];
    c = 0;
    for (j = 2; j <= number[i] / 2; j++) {
      if (number[i] % j == 0) {
        c++;
      }
      if (c != 0) {
        break;
      }
    }
    if (c == 0) {
      count++;
    }
  }
  cout << count << endl;

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

/***
 *	?´???° Prime Numbers
 ****/

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

int isprime(long x) {
  if (x == 2) { // x ???1??\\????????´????????¨????????????
    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 1;
}

int main(void) {
  int n;
  long x;
  int count = 0; //?´???°????????°???????????????

  cin >> n;

  for (int i = 0; i < n; i++) { // n???????????????????????°??????????¢????
    cin >> x;

    if (isprime(x) == 1) {
      count++;
    } else {
      //?´???°??§????????´???????????????????????????
    }
  }
  cout << count << endl; //?´???°??§???????????°?????¨?????????

  return 0;
}

Error: TLE

Fixed submission:

/***
 *	?´???° Prime Numbers
 ****/

#include <iostream>
using namespace std;

int isPrime(long x) {
  if (x <= 1) { // x ???1??\\????????´????????¨????????????
    return 0;

  } else {
    for (int i = 2; i < (x - 1); i++) { //????????°??¨1??\\?????§????????????
      if (x % i == 0) {
        return 0;
      } else {
        // return 1;
      }
    }
  }
  return 1;
}

int main(void) {
  int n;
  long x;
  int count = 0; //?´???°????????°???????????????

  cin >> n;

  for (int i = 0; i < n; i++) { // n???????????????????????°??????????¢????
    cin >> x;

    if (x > 2 && x % 2 == 0) {
    } else if (x > 3 && x % 3 == 0) {
    } else if (x > 5 && x % 5 == 0) {
    } else if (x > 7 && x % 7 == 0) {
    } else if (x > 9 && x % 9 == 0) {
    } else if (x > 11 && x % 11 == 0) {
    } else if (x > 13 && x % 13 == 0) {
    } else if (x > 17 && x % 17 == 0) {
    } else if (x > 19 && x % 19 == 0) {
    } else {
      if (isPrime(x) == 1) {
        count++;
      } else {
        //?´???°??§????????´???????????????????????????
      }
    }
  }
  cout << count << endl; //?´???°??§???????????°?????¨?????????

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

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

#define MAX 256

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

int main(void) {

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

  for (int i = 0; i < a; i++) {
    scanf("%d", &b);
    count += f(b);
  }

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

  return 0;
}

Error: TLE

Fixed submission:

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

#define MAX 256

int f(int x) {
  if (x == 2)
    return 1;
  if (x % 2 == 0)
    return 0;
  for (int i = 3; i <= x / 2; i += 2) {
    if (x % i == 0)
      return 0;
  }
  return 1;
}

int main(void) {

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

  for (int i = 0; i < a; i++) {
    scanf("%d", &b);
    count += f(b);
  }

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

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


//  sosu.cpp(2014/10/12-)

#include <cstdio>
#define MAX_N 50000000

bool s[MAX_N] = {false};

int main() {
  int k = 0, d, n;

  for (int i = 3; i < 10000; i += 2) {
    if (!s[(i - 1) / 2]) {
      for (int j = i; i * j <= MAX_N * 2; j += 2)
        s[(i * j - 1) / 2] = true;
    }
  }

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &d);
    if (d % 2 == 1 || d == 2)
      if (!s[(d - 1) / 2])
        k++;
  }

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

  return 0;
}

Error: -11

Fixed submission:


//  sosu.cpp(2014/10/12-)

#include <cstdio>
#define MAX_N 50000000

bool s[MAX_N] = {false};

int main() {
  int k = 0, d, n;
  s[0] = true; // 2

  for (int i = 3; i < MAX_N; i += 2) {
    if (!s[(i - 1) / 2]) {
      k++;
      for (int j = i; i * j <= MAX_N * 2; j += 2)
        s[(i * j - 1) / 2] = true;
    }
  }

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &d);
    if (d % 2 == 1 || d != 2)
      if (!s[(d - 1) / 2])
        k++;
  }

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

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, f, n) for (int i = (f); i <= (n); ++i)
#define outl(x) cout << (x) << '\\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
template <class A, class B> inline bool chmax(A &a, B b) {
  return b > a ? a = b, 1 : 0;
}
template <class A, class B> inline bool chmin(A &a, B b) {
  return b < a ? a = b, 1 : 0;
}
inline int updiv(int a, int b) { return ((a + b - 1) / b); }

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

int main() {
  FAST();
  int n, a, cnt = 0;
  cin >> n;
  while (n--) {
    cin >> a;
    cnt += prime(a);
  }
  outl(cnt);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, f, n) for (int i = (f); i <= (n); ++i)
#define outl(x) cout << (x) << '\\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
template <class A, class B> inline bool chmax(A &a, B b) {
  return b > a ? a = b, 1 : 0;
}
template <class A, class B> inline bool chmin(A &a, B b) {
  return b < a ? a = b, 1 : 0;
}
inline int updiv(int a, int b) { return ((a + b - 1) / b); }

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

int main() {
  FAST();
  int n, a, cnt = 0;
  cin >> n;
  while (n--) {
    cin >> a;
    cnt += prime(a);
  }
  outl(cnt);
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int isprime(int x) {
  if (x == 2)
    return true;
  if (x < 2 || x % 2 == 0)
    return false;
  else {
    int i = 3;
    while (i <= sqrt(x)) {
      if (x % i == 0)
        return false;
      i = i + 2;
    }
    return true;
  }
}

int main() {
  int n;
  cin >> n;
  int a[n], b;
  b = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (isprime(a[i]) != 0) {
      b++;
    }
  }
  cout << b << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int isprime(int x) {
  if (x == 2)
    return true;
  if (x < 2 || x % 2 == 0)
    return false;
  else {
    int i = 3;
    while (i < sqrt(x)) {
      if (x % i == 0)
        return false;
    }
    return true;
  }
}

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

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int n, x, c;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    for (int j = 2; j <= (int)sqrt(x); j++) {
      if (x % j == 0) {
        c++;
        break;
      }
    }
  }
  cout << n - c << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)

int main() {
  int ans = 0;
  int n;
  vector<bool> isprime(100000004);
  for (int i = 0; i < isprime.size(); ++i) {
    isprime[i] = true;
  }
  isprime[0] = isprime[1] = false;

  for (int i = 2; i < isprime.size(); i++) {
    if (!isprime[i]) {
      continue;
    }
    for (int j = i * 2; j < isprime.size(); j += i) {
      isprime[j] = false;
    }
  }
  /*for (int i = 0; i<10004; i++) {
          cout << i;
          cout << " ";
          cout << (isprime[i] ? "YES" : "NO") << endl;
  }*/

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

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

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

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

    int t = sqrt(a);
    for (int j = 2; j <= t; j++) {
      if (a % j == 0) {
        cnt++;
        break;
      }
    }
  }

  cout << n - cnt << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

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

    for (int j = 2; j < a; j++) {
      if (a % j == 0) {
        cnt++;
        break;
      }
    }
  }

  cout << n - cnt << endl;

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

#include <iostream>
using namespace std;

int isPrime(int n) {
  int i;
  if (n < 2)
    return 0;
  else if (n == 2)
    return 1;
  if (n % 2 == 0)
    return 0;
  for (i = 3; i <= n / i; i += 2) {
    if (n % i == 0)
      return 0;
  }
  return 1;
}

int main() {
  int 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 <iostream>
using namespace std;

bool isDivisible(int a, int b) {
  if (a % b == 0)
    return true;
  else
    return false;
}

int main(int argc, char const *argv[]) {
  int n, m, count = 0;
  bool flag;
  cin >> n;
  for (int i = 0; i < n; i++) {
    flag = true;
    cin >> m;
    for (int j = m - 1; j > 1; j--) {
      if (isDivisible(m, j)) {
        flag = false;
        break;
      }
    }
    if (flag)
      count++;
  }
  cout << count << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int counter = 0;

void isPrime(int num) {
  int i = 2, limit = sqrt(num);
  while (i <= limit) {
    if (num % i == 0)
      return;
    i++;
  }

  counter++;
  return;
}

int main() {
  int N;
  cin >> N;
  int num;
  for (int i = 0; i < N; i++) {
    scanf("%d", &num);
    isPrime(num);
  }
  cout << counter << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int counter = 0;

void isPrime(int num) {
  int i = 2;
  while (1) {
    if (num % i == 0)
      break;
    i++;
  }
  if (i == num)
    counter++;
  return;
}

int main() {
  int N;
  cin >> N;
  int num;
  for (int i = 0; i < N; i++) {
    cin >> num;
    isPrime(num);
  }
  cout << counter << endl;
}