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


#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define mp make_pair
#define ERASE(v, i) (v).erase(remove(all(v), i), (v).end())
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(it, c)                                                            \\
  for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define debug(x) cerr << #x << " = " << (x) << endl;
#define LINE cerr << "LINE: " << __LINE__ << endl;
inline int toInt(string s) {
  int v;
  istringstream i(s);
  i >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream o;
  o << x;
  return o.str();
}
template <class T> void pv(T a, T b) {
  for (T it = a; it != b; ++it)
    cerr << *it << " ";
  cerr << endl;
}
const int INF = 1000000000;
const double PI = acos(-1.0), EPS = 1e-10;

int main() {
  int cnt = 0;
  int N;
  cin >> N;
  rep(i, N) {
    int p;
    cin >> p;
    bool f = true;
    for (int j = 2; j * j <= p; j++)
      if (p % j == 0) {
        f = false;
        break;
      }
    if (f)
      cnt++;
  }
  cout << cnt << endl;
}

Error: MLE

Fixed submission:


#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define mp make_pair
#define ERASE(v, i) (v).erase(remove(all(v), i), (v).end())
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(it, c)                                                            \\
  for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define debug(x) cerr << #x << " = " << (x) << endl;
#define LINE cerr << "LINE: " << __LINE__ << endl;
inline int toInt(string s) {
  int v;
  istringstream i(s);
  i >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream o;
  o << x;
  return o.str();
}
template <class T> void pv(T a, T b) {
  for (T it = a; it != b; ++it)
    cerr << *it << " ";
  cerr << endl;
}
const int INF = 1000000000;
const double PI = acos(-1.0), EPS = 1e-10;

const int M = 100000000;

bool isp[M + 1];

int main() {
  memset(isp, -1, sizeof isp);
  isp[0] = isp[1] = false;
  for (int i = 2; i * i <= M; i++)
    if (isp[i])
      for (int j = 2 * i; j <= M; j += i)
        isp[j] = false;
  int N;
  cin >> N;
  int cnt = 0;
  rep(i, N) {
    int p;
    cin >> p;
    if (isp[p])
      cnt++;
  }
  cout << cnt << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool judge_prime(int num) {
  if (num == 2 || num == 3) {
    return true;
  }

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

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

int main() {

  int num;
  std::cin >> num;

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

  int result = 0;
  for (int i = 0; i < num; i++) {
    if (judge_prime(list[i])) {
      result++;
    }
  }
  std::cout << result << std::endl;
}


Error: TLE

Fixed submission:

#include <iostream>

bool judge_prime(int num) {
  if (num == 2 || num == 3) {
    return true;
  }

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

int main() {

  int num;
  std::cin >> num;

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

  int result = 0;
  for (int i = 0; i < num; i++) {
    if (judge_prime(list[i])) {
      result++;
    }
  }
  std::cout << result << std::endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int isPrimeNumbers(int x) {
  if (x == 1) {
    return 0;
  } else if (x == 2) {
    return 1;
  } else if ((x % 2) == 0) {
    return 0;
  } else {
    int y = pow(x, (1.0 / 2.0));
    for (int p = 3; p <= y; p++) {
      if (x % p == 0)
        return 0;
    }
  }
  return 1;
}

int main() {
  int n;
  cin >> n;
  int x[n];
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  int count = 0;
  for (int i = 0; i < n; i++) {
    if (isPrimeNumbers(x[i]) == 1) {
      count++;
      // cout<<isPrimeNumbers(x[i])<<endl<<x[i]<<endl;
    } else {
      continue;
    }
  }
  cout << count << endl;
  return 0;
  // std::cout << pow(9,(1.0/2.0))<< '\\n';
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool isPrime(int a) {
  int b = a / 2;
  if (a == 1)
    return false;
  for (int i = b; i >= 1; i--) {
    if (a % i == 0 && i != 1)
      return false;
  }
  return true;
}

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

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

#define P 10000
#define UNDEFINED -1

int CheckPrime(int v); // v????´???°??????1????????????
int GetPrime(int n);   // n??????????´???°????????????
int PRIME[P];          //?´???°?????????

int main() {
  int v = 0;
  int N = 0;
  scanf("%d", &N);
  int num = 0;

  for (int i = 0; i < P; i++) {
    PRIME[i] = UNDEFINED;
  }

  for (int i = 0; i < N; i++) {
    scanf("%d", &v);
    num += CheckPrime(v);
  }

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

  return 0;
}

int CheckPrime(int v) {
  for (int i = 0; i < P; i++) {
    if (PRIME[i] == UNDEFINED) {
      PRIME[i] = GetPrime(i);
    }
    if (v == PRIME[i]) {
      return 1;
    }
    if (v % PRIME[i] == 0) {
      return 0;
    }
    if (sqrt(v) + 1 < PRIME[i]) {
      return 1;
    }
  }
  return 1;
}

int GetPrime(int n) {
  if (n > P) {
    printf("E\\n");
  }
  if (n == 0) {
    PRIME[0] = 2;
    return 2;
  }
  if (n == 1) {
    PRIME[1] = 3;
    return 3;
  }
  if (PRIME[n - 1] == UNDEFINED) {
    PRIME[n - 1] = GetPrime(n - 1);
  }

  int v = PRIME[n - 1] + 2;

  for (int i = 0; i < n; i++) {
    if (v % PRIME[i] == 0) {
      i = 0;
      v += 2;
    }
    if (sqrt(v) + 1 < PRIME[i]) {
      break;
    }
  }
  PRIME[n] = v;
  return v;
}

Error: TLE

Fixed submission:

#include <stdio.h>

#define P 10000000
#define UNDEFINED -1

int CheckPrime(int v); // v????´???°??????1????????????
int GetPrime(int n);   // n??????????´???°????????????
int PRIME[P];          //?´???°?????????

int main() {
  int v = 0;
  int N = 0;
  scanf("%d", &N);
  int num = 0;

  for (int i = 0; i < P; i++) {
    PRIME[i] = UNDEFINED;
  }

  for (int i = 0; i < N; i++) {
    scanf("%d", &v);
    num += CheckPrime(v);
  }

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

  return 0;
}

int CheckPrime(int v) {
  for (int i = 0; i < P; i++) {
    if (PRIME[i] == UNDEFINED) {
      PRIME[i] = GetPrime(i);
    }
    if (v == PRIME[i]) {
      return 1;
    }
    if (v % PRIME[i] == 0) {
      return 0;
    }
    if (v / 2 + 1 < PRIME[i]) {
      return 1;
    }
  }
  return 1;
}

int GetPrime(int n) {
  if (n > P) {
    printf("E\\n");
  }
  if (n == 0) {
    PRIME[0] = 2;
    return 2;
  }
  if (n == 1) {
    PRIME[1] = 3;
    return 3;
  }
  if (PRIME[n - 1] == UNDEFINED) {
    PRIME[n - 1] = GetPrime(n - 1);
  }

  int v = PRIME[n - 1] + 2;

  for (int i = 0; i < n; i++) {
    if (v % PRIME[i] == 0) {
      i = 0;
      v += 2;
    }
    if (v / 2 + 1 < PRIME[i]) {
      break;
    }
  }
  PRIME[n] = v;
  return v;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
int isprime(int a) {
  int tmp = a / 10;

  if (a == 1 || a == 2)
    return 1;
  else
    for (int i = 2; i <= int(sqrt(a)) + 1; i++) {
      if (a % i == 0)
        return 0;
      else if (i == int(sqrt(a)) + 1)
        return 1;
    }
}
int main() {
  int num;
  cin >> num;
  int a, sum = 0;
  for (int i = 0; i < num; i++) {
    cin >> a;
    sum += isprime(a);
  }
  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int isprime(int a) {
  if (a == 1 || a == 2)
    return 1;
  else
    for (int i = 2; i <= a / 2; i++) {
      if (a % i == 0)
        return 0;
      if (i == a / 2)
        return 1;
    }
}
int main() {
  int num;
  cin >> num;
  int a, sum = 0;
  for (int i = 0; i < num; i++) {
    cin >> a;
    sum += isprime(a);
  }
  cout << sum << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <stdio.h>

int main() {
  int n, tmp, count = 0, limit;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d", &tmp);
    limit = sqrt(tmp);
    for (int j = 2; j <= limit; j++) {
      if (tmp % j == 0) {
        count++;
        break;
      }
    }
  }
  printf("%d\\n", n - count);
}

Error: -11

Fixed submission:

#include <stdio.h>

char checkTable[10000000001];

int findLimit(int num) {
  int i;
  for (i = 1; i * i <= num; i++)
    ;
  return i - 1;
}

int main() {
  int n, tmp, count = 0, limit;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d", &tmp);
    if (checkTable[tmp] == 'Y')
      count++;
    else {
      limit = findLimit(tmp);
      for (int j = 2; j <= limit; j++) {
        if (tmp % j == 0) {
          checkTable[tmp] = 'Y';
          count++;
          break;
        }
      }
    }
  }
  printf("%d\\n", n - count);
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

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

// typedef long long ll;

using namespace std;

// vector<bool> p(100000100,true);

// void makep(){
// 	for(int i=0; i<100000100; i++){
// 		if(i<2)p[i]=false;
// 		for(int j=2; j<=sqrt(i); j++){
// 			if(i%j==0){
// 				p[i]=false;
// 				break;
// 			}
// 		}
// 	}
// }

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

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

Error: -8

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

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

// typedef long long ll;

using namespace std;

// vector<bool> p(100000100,true);

// void makep(){
// 	for(int i=0; i<100000100; i++){
// 		if(i<2)p[i]=false;
// 		for(int j=2; j<=sqrt(i); j++){
// 			if(i%j==0){
// 				p[i]=false;
// 				break;
// 			}
// 		}
// 	}
// }

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

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

int const SIZ = 100000001;
bitset<SIZ> chk;

void sieve() {
  chk.set();
  chk.set(0, 0);
  chk.set(1, 0);
  for (int i = 4; i < SIZ; i += 2) {
    chk.set(i, 0);
  }
  for (int i = 3; i * i < SIZ; i += 2) {
    if (!chk[i])
      continue;
    for (int j = 2; i * j < SIZ; ++j) {
      chk.set(i * j, 0);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);

  sieve();

  int n;
  cin >> n;
  int m, ans = 0;
  for (int i = 0; i < n; ++i) {
    cin >> m;
    if (chk[m]) {
      ++ans;
      chk[m] = 0;
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

int const SIZ = 100000001;
bitset<SIZ> chk;

void sieve() {
  chk.set();
  chk.set(0, 0);
  chk.set(1, 0);
  for (int i = 4; i < SIZ; i += 2) {
    chk.set(i, 0);
  }
  for (int i = 3; i < SIZ; i += 2) {
    if (!chk[i])
      continue;
    for (int j = 2; i * j < SIZ; ++j) {
      chk.set(i * j, 0);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);

  sieve();

  int n;
  cin >> n;
  int m, ans = 0;
  for (int i = 0; i < n; ++i) {
    cin >> m;
    if (chk[m]) {
      ++ans;
      chk[m] = 0;
    }
  }
  cout << ans << endl;

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

int const SIZ = 10001;

bitset<SIZ> sieve;
vector<int> primes;

void prepare() {
  sieve.set();
  sieve.set(0, 0);
  sieve.set(1, 0);
  for (int i = 4; i < SIZ; i += 2) {
    sieve.set(i, 0);
  }
  for (int i = 3; i * i < SIZ; i += 2) {
    if (!sieve[i]) {
      continue;
    }
    for (int j = 2; i * j < SIZ; ++j) {
      sieve.set(i * j, 0);
    }
  }

  for (int i = 0; i < SIZ; ++i) {
    if (sieve[i]) {
      primes.push_back(i);
    }
  }
}

int main() {
  // cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  prepare();

  unordered_map<int, int> checked;
  int m, ans = 0;
  for (int i = 0; i < n; ++i) {
    cin >> m;
    if (checked[m]) {
      continue;
    }
    if (m < SIZ && sieve[m]) {
      ++ans;
      checked[m] = 1;
      continue;
    }
    bool isprime = true;
    for (int i = 0; i < (int)primes.size(); ++i) {
      if (m % primes[i] == 0) {
        isprime = false;
        break;
      }
    }
    if (isprime) {
      ++ans;
      checked[m] = 1;
    }
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

int const SIZ = 10001;
bitset<SIZ> isprime;

void sieve() {
  isprime.set();
  isprime.set(0, 0);
  isprime.set(1, 0);
  for (int i = 4; i < SIZ; i += 2) {
    isprime.set(i, 0);
  }
  for (int i = 3; i * i < SIZ; i += 2) {
    if (!isprime[i])
      continue;
    for (int j = 2; i * j < SIZ; ++j) {
      isprime.set(i * j, 0);
    }
  }
}

int main() {
  // cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  sieve();

  int m, ans = 0;
  for (int i = 0; i < n; ++i) {
    cin >> m;
    if (isprime[m]) {
      ++ans;
      isprime.set(m, 0);
    }
  }
  cout << ans << endl;

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

#include <iostream>
using namespace std;
int main(void) {
  bool flag;
  int n, k = 0, a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    flag = false;
    for (int j = 2; j * j <= a; j++) {
      if (a % j == 0) {
        flag = true;
        break;
      }
    }
    if (flag == false)
      k += 1;
  }
  cout << k << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

bool primeNum(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;

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

int main(void) {

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

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool primeNum(int num) {
  if (num % 2 == 0) {
    for (int i = 2; i <= num / 2; i++) {
      if (num % i == 0)
        return false;
    }
  } else {
    for (int i = 3; i <= num / 2; i += 2) {
      if (num % i == 0)
        return false;
    }
  }
  return true;
}

int main(void) {

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

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

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

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

  return true;
}

int main() {

  int n;
  cin >> n;

  int sum = 0;
  int x;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (isPrime(x))
      sum += 1;
  }

  cout << sum << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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 sum = 0;
  int x;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (isPrime(x))
      sum += 1;
  }

  cout << sum << endl;

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

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

bool isprime(int num) {
  if (num == 2)
    return true;
  if (num % 2 == 0)
    return false;
  for (int j = 3; j < (int)sqrt(num) + 2; j += 2) {
    if (num % j == 0)
      return false;
  }
  return true;
}
int main() {
  int count = 0, tmp, n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    if (isprime(tmp))
      count++;
  }
  cout << count << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int count = 0, tmp, n;
  bool f, flag[10003] = {};
  flag[2] = true;
  for (int i = 3; i < 10002; i += 2) {
    f = true;
    for (int j = 3; j < (int)sqrt(i) + 1; j += 2) {
      if (i % j == 0) {
        f = false;
        break;
      }
    }
    flag[i] = f;
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    if (flag[tmp])
      count++;
  }
  cout << count << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
int main() {
  // def
  int n, j, sum = 0;
  cin >> n;
  int num[n];
  for (int i = 0; i < n; i++) {
    cin >> num[i];
  }

  // equation
  for (int i = 0; i < n; i++) {
    for (j = 2; j < sqrt(num[i]); j++) {
      if (num[i] % j == 0) {
        break;
      }
    }
    if (j > sqrt(num[i])) {
      sum++;
    }
  }

  // output
  cout << sum << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  // def
  int n, j, sum = 0;
  cin >> n;
  int num[n];
  for (int i = 0; i < n; i++) {
    cin >> num[i];
  }

  // equation
  for (int i = 0; i < n; i++) {
    for (j = 2; j < num[i]; j++) {
      if (num[i] % j == 0) {
        break;
      }
    }
    if (j == num[i]) {
      sum++;
    }
  }

  // output
  cout << sum << endl;

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

#define FOR(i, min, max) for (int(i) = (min); (i) < (max); (i)++)

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

int main() {
  int n, c = 0;
  cin >> n;
  FOR(i, 0, n) {
    int r;
    cin >> r;
    if (r == 2 || r == 3) {
      c++;
      continue;
    }
    if (r > 1 && (r % 6 == 5 || r % 6 == 1)) {
      bool ch = true;
      FOR(j, 2, (int)sqrt(r) + 1) {
        if (r % j == 0) {
          ch = false;
          break;
        }
      }
      if (ch)
        c++;
    }
  }
  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:

#define FOR(i, min, max) for (int(i) = (min); (i) < (max); (i)++)

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n, c = 0;
  cin >> n;
  FOR(i, 0, n) {
    int r;
    cin >> r;
    if (r == 2 || r == 3) {
      c++;
      continue;
    }
    if (r > 1 && (r % 6 == 5 || r % 6 == 1)) {
      bool ch = true;
      FOR(j, 2, r / 2) {
        if (r % j == 0) {
          ch = false;
          break;
        }
      }
      if (ch)
        c++;
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

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

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

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

    //        cout << a[i] << " : ";

    int numOfDivisor = 0;

    if (a[i] == 1 ||
        (a[i] != 2 &&
         a[i] % 2 == 0)) { // 1????????????2??\\????????¶??°?????????
      //            cout << "even\\n";
      continue;
    }

    for (int j = 3; j <= sqrt(a[i]); j += 2) {
      if (a[i] % j == 0) {
        numOfDivisor++;
        //                cout << j << " ";
      }
    }

    if (numOfDivisor == 0) {
      cnt++;

      //            cout << "***";
    }

    //        cout << endl;
  }

  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

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

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

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

    //        cout << a[i] << " : ";

    int numOfDivisor = 0;

    if (a[i] == 1 ||
        (a[i] != 2 &&
         a[i] % 2 == 0)) { // 1????????????2??\\????????¶??°?????????
      //            cout << "even\\n";
      continue;
    }

    for (int j = 3; j < a[i] / 2; j += 2) {
      if (a[i] % j == 0) {
        numOfDivisor++;
        //                cout << j << " ";
      }
    }

    if (numOfDivisor == 0) {
      cnt++;

      //            cout << "***";
    }

    //        cout << endl;
  }

  cout << cnt << endl;

  return 0;
}