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

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

bool IsPrime(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false; // 偶数はあらかじめ除く

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      // 素数ではない
      return false;
    }
  }

  // 素数である
  return true;
}

int main() {

  int n, count, tmp;
  cin >> n;

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

  cout << count << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
bool is_prime[100000000];

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

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

  int n, count, tmp;
  cin >> n;

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

  cout << count << endl;

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

#include <iostream>
using namespace std;

bool is_prime[50000000];
int main() {

  for (int i = 0; i < 50000000; i++) {
    is_prime[i] = true;
  }

  is_prime[0] = false;
  for (int i = 1; i * i + i < 25000000; i++) {
    if (is_prime[i]) {
      for (int j = i * 3 + 1; j < 50000000; j += 2 * i + 1) {
        is_prime[j] = false;
      }
    }
  }

  int n, tag;
  cin >> n;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> tag;
    if (tag % 2 == 0) {
      if (tag == 2) {
        cnt++;
      }
    } else {
      if (is_prime[tag / 2]) {
        cnt++;
      }
    }
  }

  cout << cnt << endl;
  int xxx;
  cin >> xxx;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool is_prime[50000000];
int main() {

  for (int i = 0; i < 50000000; i++) {
    is_prime[i] = true;
  }

  is_prime[0] = false;
  for (int i = 1; i < 50000000; i++) {
    if (is_prime[i]) {
      for (int j = i * 3 + 1; j < 50000000; j += 2 * i + 1) {
        is_prime[j] = false;
      }
    }
  }

  int n, tag;
  cin >> n;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> tag;
    if (tag % 2 == 0) {
      if (tag == 2) {
        cnt++;
      }
    } else {
      if (is_prime[tag / 2]) {
        cnt++;
      }
    }
  }

  cout << cnt << endl;
  int xxx;
  cin >> xxx;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
bool IsPrime(int x) {
  int i;

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

  return true;
}

int main(void) {
  int p, n, COUNT = 0;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> p;
    if (IsPrime(p))
      COUNT++;
  }

  cout << COUNT << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
bool IsPrime(int x) {
  int i, j = 2;

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

  if (j == x)
    return true;
  else
    return false;
}

int main(void) {
  int p, n, COUNT = 0;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> p;
    if (IsPrime(p))
      COUNT++;
  }

  cout << COUNT << endl;

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

#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i, a, n) for (int i = (a), i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, n) for (int i = (a); i > n; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
inline bool isprime(int x) {
  for (int i = 2; i * i <= x; ++i)
    if (x % i == 0)
      return false;
  return true;
}
int main() {
  int n;
  cin >> n;
  int tmp, cnt = 0;
  rep(i, 0, n) {
    cin >> tmp;
    if (isprime(tmp))
      ++cnt;
  }
  P(cnt);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i, a, n) for (int i = (a), i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, n) for (int i = (a); i > n; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
int main() {
  int n;
  cin >> n;
  vi v;
  v.reserve(n);
  int cnt = 0, tmp;
  rep(i, 0, n) {
    cin >> tmp;
    if (tmp == 2 || tmp == 3 || tmp == 5 || tmp == 7)
      ++cnt;
    else if (tmp % 2 != 0 && tmp % 3 != 0 && tmp % 5 != 0 && tmp % 7 != 0)
      v.eb(tmp);
  }
  if (len(v) == 0) {
    P(cnt);
    return 0;
  }
  sort(all(v));
  vi so{2};
  so.reserve(10000);
  int max_v = *max_element(all(v));
  for (int i = 3; i <= max_v; i += 2) {
    bool flg = false;
    rep(j, 0, len(so)) {
      if (i % so[j] == 0) {
        flg = true;
        break;
      }
    }
    if (flg)
      continue;
    so.eb(i);
    cnt += count(all(v), i);
  }
  P(cnt);
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repi(i, n, a) for (int i = a; i < n; ++i)
#define num 50000010

int main(void) {
  int n, i, j, ans = 0, a;
  bool *f;
  f = (bool *)malloc(sizeof(bool) * num);
  rep(i, num) f[i] = true;
  repi(i, 8002, 1) if (f[i]) for (j = 3 * i + 1; j <= num; j += 2 * i + 1)
      f[j] = false;
  scanf("%d", &n);
  rep(i, n) {
    scanf("%d", &a);
    if (a == 2)
      ++ans;
    if (a % 2 && f[(a - 1) / 2])
      ++ans;
  }
  printf("%d\\n", ans);
  free(f);
  return 0;
}


Error: TLE

Fixed submission:

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repi(i, n, a) for (int i = a; i < n; ++i)
#define num 100000010

int main(void) {
  int n, i, j, ans = 0, a;
  bool *f;
  f = (bool *)malloc(sizeof(bool) * num);
  rep(i, num) f[i] = true;
  repi(i, 10002, 2) if (f[i]) for (j = 2; i * j <= num; ++j) f[i * j] = false;
  scanf("%d", &n);
  rep(i, n) {
    scanf("%d", &a);
    if (f[a])
      ++ans;
  }
  printf("%d\\n", ans);
  free(f);
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool isprime(int a) {
  if (a == 1)
    return false;

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

  return true;
}

int main() {
  int n;
  std::vector<int> d;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int b;
    scanf("%d", &b);
    d.push_back(b);
  }

  printf("%d\\n", std::count_if(d.begin(), d.end(), isprime));

  return 0;
}

Error: TLE

Fixed submission:

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

int gcd(int x, int y) {

  if (y == 0)
    return x;
  if (x >= y)
    return gcd(y, x % y);
  else
    return gcd(x, y % x);
}

bool isprime(int a) {
  if (a == 1)
    return false;

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

  return true;
}

int main() {
  int n;
  std::vector<int> d;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int b;
    scanf("%d", &b);
    d.push_back(b);
  }

  printf("%d\\n", std::count_if(d.begin(), d.end(), isprime));

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

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

bool prime_table[100000001];

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

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

Error: 0

Fixed submission:

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

int a[10001];
bool prime_table[10000001];
int max_a = 0;

void make_prime_table() {
  for (int i = 0; i <= max_a; i++)
    prime_table[i] = true;
  prime_table[0] = prime_table[1] = false;
  for (int i = 2; i <= max_a; i++) {
    if (prime_table[i]) {
      for (int j = 2 * i; j <= max_a; j += i)
        prime_table[j] = false;
    }
  }
  return;
}

void solve(int n) {
  make_prime_table();
  int count = 0;
  for (int i = 0; i < n; i++) {
    if (prime_table[a[i]])
      count++;
  }
  cout << count << endl;
  return;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    max_a = max(max_a, a[i]);
  }
  solve(n);
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>
#include <stdio.h>
int hantei(int a);
int main() {
  int a, B[100000], z, count = 0, i;
  scanf("%d", &a);
  for (i = 0; i < a; i++) {
    scanf("%d", &B[i]);
    z = hantei(B[i]);
    if (z == 0)
      count++;
  }
  printf("%d\\n", count);
}
int hantei(int a) {
  int i = 3;
  if (a == 2)
    return 0;
  if (a < 2 || a % 2 == 0)
    return 1;
  while (i <= pow(a, 0.5)) {
    if (a % i == 0)
      return 1;
    i += 2;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
int sosuu(int);
int main() {
  int a, b, c, count1 = 0;
  scanf("%d", &a);
  for (int i = 0; i < a; i++) {
    scanf("%d", &b);
    c = sosuu(b);
    if (c == 1)
      count1++;
  }
  printf("%d\\n", count1);
  return 0;
}
int sosuu(int x) {
  int count = 0, d;
  if (x > 2 && x % 2 == 0)
    return d;
  for (int i = 1; i <= x; i++) {
    if (x % i == 0)
      count++;
  }
  if (count == 2)
    d = 1;
  else
    d = 0;
  return d;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

int main() {
  int n, x;
  int cnt = 0;
  cin >> n;
  assert(1 <= n && n <= 10000);
  for (int i = 0; i < n; i++) {
    cin >> x;
    assert(2 <= x && x <= 1000000000);
    if (isPrime(x))
      cnt++;
  }
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

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

bool isPrime(int x) {
  if (x < 2)
    return false;
  else if (x == 2)
    return true;
  if (x % 2 == 0)
    return false;
  int limit = (int)sqrt((double)x) + 1;
  for (int i = 3; i < x / 2; i += 2) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n, x;
  int cnt = 0;
  cin >> n;
  assert(1 <= n && n <= 10000);
  for (int i = 0; i < n; i++) {
    cin >> x;
    assert(2 <= x && x <= 1000000000);
    if (isPrime(x))
      cnt++;
  }
  cout << cnt << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

//
// ALDS1_1 - C Prime Numbers
// A prime number is a natural number which has exactly two distinct natural
// number divisors: 1 and itself. For example, the first four prime numbers are:
// 2, 3, 5 and 7.
//
// Write a program which reads a list of N integers and prints the number of
// prime numbers in the list.
//
#include <cmath>
#include <iostream>
using namespace std;

void read(int &size, int **ppArray);
void checkPrimeNumbers(size_t size, const int *pArray, int &count);
void write(const int count);

int main() {
  int size = 0, ret = 0;
  int *pArray = NULL;

  read(size, &pArray);
  checkPrimeNumbers(size, pArray, ret);
  write(ret);

  delete pArray;
}

void read(int &size, int **ppArray) {
  if (NULL == ppArray || NULL != *ppArray)
    return;

  cin >> size;
  if (10000 < size)
    return;

  *ppArray = new int[size];
  for (int i = 0; i < size; i++) {
    cin >> *(*ppArray + i);
  }
}

void checkPrimeNumbers(size_t size, const int *pArray, int &count) {
  if (NULL == pArray)
    return;

  for (int i = 0; i < size; i++) {
    bool flag = true;
    int value = *(pArray + i);
    if (2 > value)
      continue;
    if (2 == value) {
      count++;
      continue;
    }
    if (0 == value % 2)
      continue;
    for (int j = 3; j <= (int)sqrt(value); j += 2) {
      if (0 == value % j) {
        flag = false;
        break;
      }
    }
    if (true == flag)
      count++;
  }
}

void write(const int count) { cout << count << endl; }

Error: TLE

Fixed submission:

//
// ALDS1_1 - C Prime Numbers
// A prime number is a natural number which has exactly two distinct natural
// number divisors: 1 and itself. For example, the first four prime numbers are:
// 2, 3, 5 and 7.
//
// Write a program which reads a list of N integers and prints the number of
// prime numbers in the list.
//
#include <iostream>
using namespace std;

void read(int &size, int **ppArray);
void checkPrimeNumbers(size_t size, const int *pArray, int &count);
void write(const int count);

int main() {
  int size = 0, ret = 0;
  int *pArray = NULL;

  read(size, &pArray);
  checkPrimeNumbers(size, pArray, ret);
  write(ret);

  delete pArray;
}

void read(int &size, int **ppArray) {
  if (NULL == ppArray || NULL != *ppArray)
    return;

  cin >> size;
  if (10000 < size)
    return;

  *ppArray = new int[size];
  for (int i = 0; i < size; i++) {
    cin >> *(*ppArray + i);
  }
}

void checkPrimeNumbers(size_t size, const int *pArray, int &count) {
  if (NULL == pArray)
    return;

  for (int i = 0; i < size; i++) {
    int value = *(pArray + i);
    if (2 == value) {
      count++;
      continue;
    }
    if (0 == value % 2)
      continue;
    for (int j = 3; j <= value; j += 2) {
      if (value == j) {
        count++;
        break;
      }
      if (0 == value % j)
        break;
    }
  }
}

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

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

class PrimeNumbers {
private:
  std::vector<int> dataSet;
  bool isPrimeNumber(int x);

public:
  PrimeNumbers(std::vector<int> vec);
  int getPrimeCount();
};

PrimeNumbers::PrimeNumbers(std::vector<int> vec) { this->dataSet = vec; }

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

int PrimeNumbers::getPrimeCount() {
  int returnCount = 0;
  for (int i : dataSet) {
    if (isPrimeNumber(i)) {
      returnCount++;
    }
  }
  return returnCount;
}

int main() {

  int loop;

  std::cin >> loop;

  int input;
  std::vector<int> vec;

  for (int i = 0; i < loop; i++) {
    std::cin >> input;
    vec.push_back(input);
  }

  PrimeNumbers pn = PrimeNumbers(vec);
  std::cout << pn.getPrimeCount() << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

class PrimeNumbers {
private:
  std::vector<int> dataSet;
  bool isPrimeNumber(int x);

public:
  PrimeNumbers(std::vector<int> vec);
  int getPrimeCount();
};

PrimeNumbers::PrimeNumbers(std::vector<int> vec) { this->dataSet = vec; }

bool PrimeNumbers::isPrimeNumber(int x) {
  if (x < 2)
    return false;
  if (x == 2)
    return true;
  if (x % 2 == 0)
    return false;
  for (int i = 3; i < (x / 2); i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}

int PrimeNumbers::getPrimeCount() {
  int returnCount = 0;
  for (int i : dataSet) {
    if (isPrimeNumber(i)) {
      returnCount++;
    }
  }
  return returnCount;
}

int main() {

  int loop;

  std::cin >> loop;

  int input;
  std::vector<int> vec;

  for (int i = 0; i < loop; i++) {
    std::cin >> input;
    vec.push_back(input);
  }

  PrimeNumbers pn = PrimeNumbers(vec);
  std::cout << pn.getPrimeCount() << std::endl;

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

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

int n, x;

bool isprime(long long x) {
  if (x == 2)
    return true;
  if (x < 2 || x % 2 == 0)
    return false;
  int i = 3;
  while (i * i <= x) {
    if (x % i == 0)
      return false;
    i += 2;
  }
  return true;
}

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


Error: MLE

Fixed submission:

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

long long inf = 10e8;
vector<int> v(inf + 1);
// vector<int> prime;

void erast() {
  for (int i = 0; i <= inf; i++) {
    v[i] = 1;
  }
  for (int i = 2; i * i <= inf; i++) {
    if (v[i]) {
      for (int j = 0; i * (j + 2) <= inf; j++) {
        v[i * (j + 2)] = 0;
      }
    }
  }
  /*for(int i=2;i<10000;i++){
          if(v[i]){
                  prime.push_back(i);
          }
  }*/
}

int n, x;

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

#include <iostream>
using namespace std;

bool isPrime(int x) {
  if (x == 2) {
    return true;
  }
  if ((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, num, cnt = 0;
  cin >> N;
  while (N) {
    N--;
    cin >> num;
    cnt += isPrime(num);
  }

  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int N, num, count_d, count_p = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    count_d = 0;
    cin >> num;
    for (int j = 1; j <= (num / 2); j++) {
      if (num % j == 0) {
        ++count_d;
      }
    }
    if (count_d == 1) {
      ++count_p;
    }
  }
  cout << count_p << endl;

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

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

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

bool isPrime(int num) {
  if (num == 2)
    return true;
  if (num % 2 == 0)
    return false;

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

int main() {
  int N;
  int count = 0;
  int num;

  cin >> N;

  REP(i, N) {
    cin >> num;
    if (isPrime(num))
      ++count;
  }

  cout << count << endl;

  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  bool num[10002] = {false};
  int give[260];
  int maxs = 0, n, co = 0;
  for (int i = 0; (scanf("%d", &give[i]) != EOF); ++i) {
    if (maxs < give[i])
      maxs = give[i];
    n = i;
  }
  num[1] = true;
  for (int i = 2; i <= sqrt(maxs); i++) { // 素数＝0；素数じゃない＝１；
    if (!num[i]) {
      for (int j = 2; j * i <= maxs; j++) {
        num[j * i] = true;
      }
    }
  }
  for (int i = 0; i <= n; i++) {
    if (!num[give[i]])
      co++;
  }
  cout << co << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

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

bool isprime(int x) {
  if (x == 2) {
    return true;
  } else if (x % 2 == 0) {
    return false;
  }
  int i = 3;
  while (i <= sqrt(x)) {
    if (x % i == 0) {
      return false;
      break;
    }
    i = i + 2;
  }
  return true;
}
int main() {
  int n, ans = 0;
  cin >> n;
  vector<int> c(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < n; i++) {
    if (isprime(c[i])) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: -8

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int a, b;
  vector<int> c(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < c[i] / 2; j++) {
      a = c[i] % j;
      if (a == 0) {
        b++;
      }
    }
  }
  cout << b << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MAX = 10000;

bool prime(int n) {
  for (int i = 2; i <= sqrt(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 buf;
    cin >> buf;
    if (prime(buf))
      cnt++;
  }

  cout << cnt << endl;
  return 0;
}

Error: TLE

Fixed submission:


#include <iostream>
using namespace std;

int main() {
  int n;
  int cnt;

  cin >> n;
  cnt = n;
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    for (int j = 2; j < num; j++) {
      if (num % j == 0) {
        cnt--;
        break;
      }
    }
  }

  cout << cnt << endl;

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

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
#define INF (1 << 30)
using namespace std;

int primeNumber(int n) {
  if (n == 2)
    return 1;
  else if (n < 2)
    return 0;
  else {
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0)
        return 0;
    }
    return 1;
  }
}
int main() {
  int sum = 0;
  for (int n; cin >> n;) {
    sum += primeNumber(n);
  }
  cout << sum << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
#define INF (1 << 30)
using namespace std;

const int N = 10000;

void primeNumber(bool prime[N]) {
  rep(i, N) prime[i] = 1;
  prime[0] = prime[1] = 0;
  rep(i, N) {
    if (prime[i]) {
      for (int j = i + i; j < N; j += i) {
        prime[j] = 0;
      }
    }
  }
}
int main() {
  bool prime[N];
  int sum = 0;
  primeNumber(prime);
  for (int n; cin >> n;) {
    sum += static_cast<int>(prime[n]);
  }
  cout << sum << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int x;
int i, n, flag;
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  int ans;
  for (i = 0; i < n; i++) {
    cin >> x;
    if (x == 2) {
      ans++;
    } else if (x % 2 != 0) {
      flag = 1;
      for (int j = 3; j * j <= x; j += 2) {
        if (x % j == 0) {
          flag = 0;
          break;
        }
      }
      if (flag == 1) {
        ans++;
      }
    }
  }

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

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
static const int N = 100;
int a[N];
int i, n, flag;
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ans = 0;
  for (i = 0; i < n; i++) {
    flag = 1;
    for (int k = 2; k * k <= a[i]; k++) {
      if (a[i] % k == 0) {
        flag = 0;
        break;
      }
    }
    if (flag == 1) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}