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

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

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

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

  for (int i = 0; i < n; i++) {
    cin >> x;
    count += Myprime(x);
  }
  cout << count << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

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

  for (int i = 0; i < n; i++) {
    cin >> x;
    count += Myprime(x);
  }
  cout << count << "\\n";
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

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

#define MAX_N (1e8) + 1

int main(void) {
  vector<bool> eratos(MAX_N, true);
  eratos[0] = eratos[1] = false;
  for (int i = 2; i * i < MAX_N; i++) {
    if (eratos[i]) {
      for (int j = 2; i * j < MAX_N; j++) {
        eratos[i * j] = false;
      }
    }
  }

  int n, input, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> input;
    if (eratos[input]) {
      ++ans;
    }
  }

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

Error: 0

Fixed submission:

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

#define MAX_N 1e6

int main(void) {
  vector<bool> eratos(MAX_N, true);
  eratos[0] = eratos[1] = false;
  for (int i = 2; i * i < MAX_N; i++) {
    if (eratos[i]) {
      for (int j = 2; i * j < MAX_N; j++) {
        eratos[i * j] = false;
      }
    }
  }

  int n, input, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> input;
    if (eratos[input]) {
      ++ans;
    }
  }

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

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

using namespace std;

int gcd(int a, int b) {
  int r;
  while (b > 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

bool isprime(int x) {
  if (x == 2)
    return true;
  if (x < 2 || 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, cnt = 0;
  cin >> n;
  for (int i; i < n; i++) {
    bool judge;
    int num;
    cin >> num;
    judge = isprime(num);
    if (judge)
      cnt += 1;
  }
  cout << cnt << endl;
}


Error: TLE

Fixed submission:

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

int gcd(int a, int b) {
  int r;
  while (b > 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int main() {
  int n, cnt = 0;
  cin >> n;
  for (int i; i < n; i++) {
    bool judge = true;
    int num;
    cin >> num;
    for (int j = 2; j < num; j++) {
      if (num % j == 0)
        judge = false;
    }
    if (judge)
      cnt += 1;
  }
  cout << cnt << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded


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

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

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &x);
    if (x == 2)
      count++;
    else if (x % 2) {
      for (j = 3; j < sqrt((double)x); j += 2) {
        if (x % j == 0)
          break;
      }
      if (j > sqrt((double)x))
        count++;
    }
  }
  printf("%d\\n", count);

  return 0;
}

Error: TLE

Fixed submission:


#include <stdio.h>
int main() {
  int i, j, n, x = 0;
  int sosu[10000];
  int count = 0;

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%d", &sosu[i]);
    if (sosu[i] != 2 && (sosu[i] % 2) == 0) {
    } else {
      sosu[x] = sosu[i];
      x++;
    }
  }

  // printf("%d\\n",x);

  for (i = 0; i < x; i++) {
    if (sosu[i] == 1)
      continue;
    int f = 0;
    for (j = 2; j < sosu[i]; j++) {
      if ((sosu[i] % j) == 0) {
        f = 1;
        break;
      }
    }
    if (f == 0)
      count++;
  }

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

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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

const int MAX_NUM = 100000000;
vector<bool> ps(MAX_NUM + 1, true);

int main() {
  ps[0] = ps[1] = false;
  for (int i = 4; i <= MAX_NUM; i += 2) {
    ps[i] = false;
  }
  for (int i = 3; i <= MAX_NUM; i += 2) {
    if (!ps[i]) {
      continue;
    }
    for (int j = i + i; j <= MAX_NUM; j += i) {
      ps[j] = false;
    }
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

const int MAX_NUM = 100000000;
vector<bool> ps(MAX_NUM + 1, true);

int main() {
  ps[0] = ps[1] = false;
  for (int i = 2; i <= MAX_NUM; i++) {
    if (!ps[i]) {
      continue;
    }
    for (int j = i + i; j <= MAX_NUM; j += i) {
      ps[j] = false;
    }
  }

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

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF 1000000001
#define EPS 1.0e-6

using namespace std;

typedef long long ll;

int main() {
  vector<int> prime_number;

  int n;
  cin >> n;
  int a[10500];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int ope = 0;
    for (int j = 2; j * j <= a[i]; j++) {
      if (a[i] % j == 0) {
        ope = 1;
        break;
      }
    }
    if (ope == 0)
      ans++;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF 1000000001
#define EPS 1.0e-6

using namespace std;

typedef long long ll;

int main() {
  vector<int> prime_number;

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

  prime_number.push_back(2);
  for (int i = 3; i <= a[0]; i++) {
    int ope = 0;
    for (int j = 0; j < prime_number.size(); j++) {
      if (prime_number[j] * prime_number[j] > i)
        break;

      if (i % prime_number[j] == 0) {
        ope = 1;
        break;
      }
    }
    if (ope == 0)
      prime_number.push_back(i);
  }

  int ans = 0;
  int i = 0, j = 0;

  sort(a, a + n);

  while (i < n && j < prime_number.size()) {
    if (a[i] == prime_number[j]) {
      ans++;
      i++;
      j++;
    } else if (a[i] < prime_number[j])
      i++;
    else if (a[i] > prime_number[j])
      j++;
  }
  cout << ans << endl;

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

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

bool isPrime(const int);

int main(int argc, char *argv[]) {
  int n, tmp, cnt = 0;
  cin >> n;
  while (n-- > 0) {
    cin >> tmp;
    if (isPrime(tmp)) {
      cnt++;
    }
  }
  cout << cnt << endl;

  return 0;
}

bool isPrime(const int x) {
  if (x < 2) {
    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;
}


Error: TLE

Fixed submission:

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

bool isPrime(const int);

int main(int argc, char *argv[]) {
  int n, tmp, cnt = 0;
  cin >> n;
  while (n-- > 0) {
    cin >> tmp;
    if (isPrime(tmp)) {
      cnt++;
    }
  }
  cout << cnt << endl;

  return 0;
}

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

#include <iostream>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)

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

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

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define MAX 100000000

bool NUM[MAX + 1] = {};

int main() {
  REP(i, MAX + 1) NUM[i] = true;
  NUM[0] = false;
  NUM[1] = false;
  for (int i = 2; i * i <= MAX + 1; i++) {
    if (NUM[i] == false)
      continue;
    else {
      for (int j = i * 2; j < MAX + 1; j += i) {
        NUM[j] = false;
      }
    }
  }
  int n, ans = 0;
  cin >> n;
  REP(i, n) {
    int temp;
    cin >> temp;
    if (NUM[temp] == true)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <math.h>
#include <stdio.h>
int isPrime(int);
int main() {
  int i;
  int sum;
  int x;
  int count = 0;
  /*数字の合計を入力*/
  scanf("%d", &sum);

  for (i = 0; i < sum; i++) {
    scanf("%d", &x);

    count += isPrime(x);
  }
  printf("%d\\n", count);
  return 0;
}

int isPrime(int x) {
  int i;
  if (x == 2)
    return 1;

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

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

  return 1;
}


Error: TLE

Fixed submission:


#include <stdio.h>

int main() {
  int i, j;
  int sum;
  int n;
  int f = 0;
  int count = 0;
  /*数字の合計を入力*/
  scanf("%d", &sum);

  for (i = 0; i < sum; i++) {

    f = 0;
    /*数字を入力*/
    scanf("%d", &n);
    /*素数判定を行う*/
    for (j = 2; j < n; j++) {

      if (n % j == 0) {
        f = 1;
        break;
      }
    }
    /*素数ならカウント*/
    if (f == 0)
      count++;
  }

  /*個数を表示*/
  printf("%d\\n", count);
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
 * ITP1_6_B.cpp
 *
 *  Created on: Apr 20, 2018
 *      Author: 13743
 */

#include <cmath>
#include <iostream>

using namespace std;

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

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


Error: TLE

Fixed submission:

/*
 * ITP1_6_B.cpp
 *
 *  Created on: Apr 20, 2018
 *      Author: 13743
 */

#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;
  int cnt = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int n;
    cin >> n;
    if (isPrime(n)) {
      cnt++;
    }
  }
  cout << cnt << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

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

using namespace std;

typedef long long ll;

long long extgcd(long long a, long long b, long long &x, long long &y) {
  long long d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else
    x = 1, y = 0;
  return d;
}

long long mod_inv(long long a, long long m) {
  long long x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

ll modmultiply(ll a, ll b, ll c) {
  ll x = 0, y = a % c;
  while (b > 0) {
    if (b % 2 == 1)
      x = (x + y) % c;
    y = (y * 2) % c;
    b /= 2;
  }
  return x % c;
}

ll modpow(ll x, ll y, ll mod) {
  ll ret = 1; // ret = x^y%mod;
  while (y) {
    if (y & 1)
      // ret = (ret*x)%mod;
      ret = modmultiply(ret, x, mod);
    // x = (x*x)%mod;
    x = modmultiply(x, x, mod);
    y >>= 1;
  }
  return ret;
}

#define MR_MAX 40
bool MillerRabin(long long v) {
  if (v == 2)
    return true;
  if (v < 2 || !(v & 1LL))
    return false;
  srand((unsigned int)time(NULL));
  long long d = v - 1;
  while ((d & 1LL) == 0)
    d >>= 1LL;
  for (int i = 0; i < MR_MAX; ++i) {
    long long a = rand() % (v - 2) + 1LL;
    long long t = d;
    long long y = modpow(a, t, v);
    while (t != v - 1LL && y != 1LL && y != v - 1LL) {
      y = (y * y) % v;
      t <<= 1LL;
    }
    if (y != v - 1LL && (t & 1LL) == 0)
      return false;
  }
  return true;
}

int main() {
  int n, v, cnt = 0;
  cin >> n;
  rep(i, n) {
    cin >> v;
    cnt += MillerRabin(v);
  }
  cout << cnt << endl;
  /*
  string pi;
  cin >> pi;
  rep(i,(int)pi.size()-10) {
    assert(i+10-1<(int)pi.size());
    string tmp = pi.substr(i,10);
    long long prim = atoll(tmp.c_str());
    if( MillerRabin(prim) ) {
      cout << "Found!!! : " << prim << endl;
    }
  }
  puts("Not Found...");
  */
  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
#define MAX 10000
using namespace std;
bool isntprime[10001];

int main() {
  isntprime[0] = isntprime[1] = true;
  for (int i = 2; i <= MAX; i++)
    if (!isntprime[i])
      for (int j = i * i; j <= MAX; j += i)
        isntprime[j] = true;

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

#include <cmath>
#include <iostream>
using namespace std;
inline bool is_prime(int n) {
  if (n <= 1)
    return false;
  int m = (int)(floor(sqrt(n) + 0.5));
  for (int i = 2; i <= m; ++i) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int main() {
  int x, count = 0, m;
  cin >> x;
  for (int i = 1; i <= x; ++i) {
    cin >> m;
    if (m <= 1)
      ;
    else if (is_prime(m))
      ++count;
  }
  cout << count << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
bool prime[10009];
inline void prime_sifter(int);
int main() {
  memset(prime, true, sizeof(prime));
  int n, count = 0, x;
  cin >> n;
  prime_sifter(100000009);
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    if (x <= 1)
      continue;
    if (prime[x])
      ++count;
  }
  cout << count << endl;
  return 0;
}
inline void prime_sifter(int n) {
  for (int i = 2; i <= n; ++i) {
    if (prime[i]) {
      for (int j = i + i; j <= n; j += i) {
        prime[j] = false;
      }
    }
  }
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int getPrimeNumbers(int max, int prime[]) {
  prime[0] = 2;
  int counter = 0;
  bool isPrime;
  for (int i = 3; prime[counter] * prime[counter] < max; i += 2) {
    isPrime = true;
    for (int j = 0; j <= counter; j++) {
      if (i % prime[j] == 0)
        isPrime = false;
    }
    if (isPrime) {
      prime[++counter] = i;
    }
  }
  return counter;
}

int main() {

  int max, num = 0;
  int element[10000];
  int prime[10000];
  int numOfPrime = 1;

  cin >> num;

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

  numOfPrime = getPrimeNumbers(max, prime);

  int primeCounter = 0;
  for (int i = 0; i < num; i++) {
    for (int j = 0; j <= numOfPrime; j++) {
      if (prime[j] == element[i]) {
        primeCounter++;
        break;
      }
      if (element[i] % prime[j] == 0) {
        break;
      }
      if (element[i] < prime[j] * prime[j]) {
        primeCounter++;
        break;
      }
    }
  }

  cout << primeCounter << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int getPrimeNumbers(int max, int prime[]) {
  prime[0] = 2;
  int counter = 0;
  bool isPrime;
  for (int i = 3; i < max + 1 / 2; i += 2) {
    isPrime = true;
    for (int j = 0; j <= counter; j++) {
      if (i % prime[j] == 0)
        isPrime = false;
    }
    if (isPrime) {
      prime[++counter] = i;
    }
  }
  return counter;
}

int main() {

  int max, num = 0;
  int element[10000];
  int prime[1000000];
  int numOfPrime = 1;

  cin >> num;

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

  numOfPrime = getPrimeNumbers(max, prime);

  int primeCounter = 0;
  for (int i = 0; i < num; i++) {
    for (int j = 0; j < numOfPrime; j++) {
      if (prime[j] == element[i]) {
        primeCounter++;
        break;
      }
      if (element[i] % prime[j] == 0) {
        break;
      } else if (element[i] < prime[j] * 2) {
        primeCounter++;
        break;
      }
    }
  }

  cout << primeCounter << endl;

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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main() {
  // ifstream fin("ALDS1_1_C_in1.txt");

  int n;
  cin >> n;
  cin.ignore();

  vector<int> v;

  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    v.push_back(t);
  }

  int count = 0;
  for (int i = 0; i < v.size(); i++) {
    bool isPrime = true;
    for (int j = 2; j <= sqrt(v[i]); j++) {
      if (v[i] % j == 0) {
        isPrime = false;
        break;
      }
    }
    if (isPrime == true)
      count++;
  }
  cout << count << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main() {
  // ifstream fin("ALDS1_1_C_in1.txt");

  int n;
  cin >> n;
  cin.ignore();

  vector<int> v;

  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    v.push_back(t);
  }

  int m = *max_element(v.begin(), v.end()) + 1;

  bool *bp;
  bp = (bool *)malloc(sizeof(bool) * m);

  memset(bp, true, sizeof(bool) * m);
  bp[0] = false;
  bp[1] = false;

  for (int i = 2; i * i < m; i++) {
    if (*(bp + sizeof(bool) * i) == true) {
      for (int j = i * i; j < m; j += i) {
        *(bp + sizeof(bool) * j) = false;
      }
    }
  }

  int count = 0;
  for (int i = 0; i < v.size(); i++) {
    if (*(bp + v[i]) == true)
      count++;
  }
  cout << count << endl;

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

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

int main() {
  int n, num, count = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num;
    if (num == 2) {
      count++;
      continue;
    }

    for (int j = 2; j <= sqrt(num); j++)
      if (num % j == 0) {
        count--;
        break;
      }

    count++;
  }
  cout << count << endl;
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;

bool prime[100000001][2] = {false};

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

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

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

#include <iostream>
using namespace std;

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

int main(void) {
  int count = 0;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    if (IsPrime(p)) {
      count++;
    }
  }
  cout << count << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

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

#include <iostream>
int main(void) {
  std::ios::sync_with_stdio(false);
  int n, COUNT = 0;
  std::cin >> n;
  int x;
  for (int i = 0; i < n; ++i) {
    std::cin >> x;
    if (x == 2) {
      ++COUNT;
    } else if (x % 2 != 0) {
      bool FLUG = false;
      for (int i = 3; i * i <= x; i += 2) {
        if (x % i == 0) {
          FLUG = true;
          break;
        }
      }
      if (FLUG == false) {
        ++COUNT;
      }
    }
  }
  std::cout << COUNT << std::endl;
  // system("pause");
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
int main(void) {
  std::ios::sync_with_stdio(false);
  int n, COUNT = 0;
  std::cin >> n;
  int x;
  for (int i = 0; i < n; ++i) {
    std::cin >> x;
    if (x == 2) {
      ++COUNT;
    } else if (x % 2 != 0) {
      bool FLUG = false;
      int Max = static_cast<int>(x / 2) + 1;
      for (int i = 3; i < Max; i += 2) {
        if (x % i == 0) {
          FLUG = true;
          break;
        }
      }
      if (FLUG == false) {
        ++COUNT;
      }
    }
  }
  std::cout << COUNT << std::endl;
  // system("pause");
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, count = 0, m;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m;
    for (int j = 2; j * j <= m; j++)
      if (m % j == 0) {
        count++;
        break;
      }
  }
  cout << n - count << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define N 100000000
bool a[100000000];
int main() {
  a[0] = a[1] = 1;
  for (int i = 2; i * i <= N; i++)
    if (a[i] == 0)
      for (int j = 2; j * i < N; j++)
        a[i * j] = 1;

  int n, count = 0, m[10000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m[i];
    if (a[m[i]] == 0)
      count++;
  }
  cout << count << endl;
  return 0;
}