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

#include <iostream>
#include <math.h>
using namespace std;
int gcd(int a1, int b1) {
  if (a1 == 0 || b1 == 0) {
    return 0;
  }
  if (a1 >= b1) {
    if (a1 % b1 == 0) {
      return b1;
    } else {
      return gcd(b1, a1 % b1);
    }
  } else {
    if (b1 % a1 == 0) {
      return a1;
    } else {
      return gcd(a1, b1 % a1);
    }
  }
}

int main(void) {
  // Here your code !
  int n, k, p = 0;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    k = 0;
    cin >> a[i];
    if (a[i] == 2) {
      p++;
      continue;
    } else if (a[i] % 2 == 0) {
      continue;
    }
    for (int j = 0; j < floor(sqrt(a[i])); j++) {
      if (a[i] % (j + 1) == 0) {
        k++;
      }
    }
    if (k == 1) {
      p++;
    }
  }
  cout << p << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
using namespace std;
int gcd(int a1, int b1) {
  if (a1 == 0 || b1 == 0) {
    return 0;
  }
  if (a1 >= b1) {
    if (a1 % b1 == 0) {
      return b1;
    } else {
      return gcd(b1, a1 % b1);
    }
  } else {
    if (b1 % a1 == 0) {
      return a1;
    } else {
      return gcd(a1, b1 % a1);
    }
  }
}

int main(void) {
  // Here your code !
  int n, k, p = 0;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    k = 0;
    cin >> a[i];
    if (a[i] == 2) {
      p++;
      continue;
    } else if (a[i] % 2 == 0) {
      continue;
    }
    for (int j = 0; j < floor(sqrt(a[i])); j++) {
      if (gcd(a[i], j + 1) == j + 1) {
        k++;
      }
    }
    if (k == 1) {
      p++;
    }
  }
  cout << p << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cassert>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

bool IsPrime(int num) {
  if (num < 2)
    return false; // 2未満は素数
  if (num == 2)
    return true; // 2は素数
  if (num % 2 == 0)
    return false; // 2で割り切れれば素数ではない

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

// 時間切れ
int main(void) {

  int N;
  cin >> N;

  int total = 0;
  for (int i = 0; i < N; ++i) {
    int e;
    cin >> e;
    if (IsPrime(e))
      ++total;
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <cassert>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int primes[10000];

int to_i(const string &num) {
  int res = 0;
  for (int i = 0; i < num.size(); ++i) {
    res = res * 10 + (num[i] - '0');
  }
  return res;
}

int main(void) {

  int N;
  cin >> N;

  int total = 0;
  for (int i = 0; i < N; ++i) {
    string num;
    cin >> num;
    bool ok = true;
    for (int j = 2, n = to_i(num); j < n; ++j) {
      if (to_i(num) % j == 0) {
        ok = false;
        break;
      }
    }
    if (ok)
      ++total;
  }

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

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

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

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_C

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

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

Error: TLE

Fixed submission:

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

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_C

int main() {
  int n;
  cin >> n;
  int arr[n];
  int max = 0;
  vector<int> primes;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] > max)
      max = arr[i];
  }

  for (int i = 2; i <= max; i++) {
    bool prime = true;
    for (int j = 0; j < primes.size(); j++) {
      if (i % primes[j] == 0) {
        prime = false;
        break;
      }
    }

    if (!prime)
      continue;
    primes.push_back(i);
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < primes.size(); j++) {
      if (arr[i] == primes[j]) {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

bool isPrime(int x) {
  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;
  }
  return true;
}

int main() {

  int n;
  scanf("%d", &n);
  int x[n];
  for (int i = 0; i < n; ++i) {
    scanf("%d", &x[i]);
  }

  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (isPrime(x[i])) {
      ++cnt;
    }
  }
  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

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

int main() {

  int n;
  scanf("%d", &n);
  int x[n];
  for (int i = 0; i < n; ++i) {
    scanf("%d", &x[i]);
  }

  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (isPrime(x[i])) {
      ++cnt;
    }
  }
  cout << cnt << endl;

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

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

using namespace std;
bool IsPrime(int a) {
  if (a == 2)
    return true;
  if (a % 2 == 0) {
    return false;
  }
  for (int i = 3; i <= sqrt(a); i += 2) {
    if (a % i == 0) {
      return false;
    }
  }
  return true;
}
int main() {
  int n;
  cin >> n;
  int a = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (IsPrime(a))
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;
bool IsPrime(int a) {
  if (a == 2)
    return true;
  if (a % 2 == 0) {
    return false;
  }
  for (int i = 3; i < a; i += 2) {
    if (a % i == 0) {
      return false;
    }
  }
  return true;
}
int main() {
  int n;
  cin >> n;
  int a = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (IsPrime(a))
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

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

int main() {

  int n;
  cin >> n;

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int j;
    cin >> j;
    if (prime(j))
      ++ans;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

int main() {

  int n;
  cin >> n;

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int j;
    cin >> j;
    if (prime(j))
      ++ans;
  }
  cout << ans << endl;

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

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

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

  return 1;
}

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

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int prime[10000], k = 1, j = 1, n, N[10000], count = 0;
  prime[1] = 2;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> N[i];
  for (int N = 2; N < 1000000; N++) {
    while (j <= k) {
      if (N % prime[j] == 0)
        goto No;
      else
        j++;
    }
    prime[j] = N;
    k = j;
  No:
    j = 1;
  }
  for (int i = 1; i < k + 1; i++)
    cout << prime[i] << " ";
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 1000000; j++) {
      if (N[i] == prime[j])
        count++;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, num, count = 0;
  bool flag;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> num;
    flag = true;
    if (num == 2)
      count++;
    if (num % 2 == 0)
      flag = false;
    for (int j = 3; j <= sqrt(num); j += 2) {
      if (num % j == 0) {
        flag = false;
        break;
      }
    }
    if (flag == true)
      count++;
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> num;
    for (int j = 2; j <= num; j++) {
      if (j == num)
        count++;
      if (num % j == 0)
        break;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int n;
int a;
int p = 0;

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

  p++;
}

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

  cout << p << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int n;
int a;
int p = 0;

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

  p++;
}

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

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

#include <math.h>
#include <stdio.h>
#define N 10000

int isPrime(int);

int main() {

  int i, j, n, x, key, count = 0;
  int a[N];
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  for (i = 0; i < n; i++) {
    if (isPrime(a[i])) {

      count++;
    }
  }

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

  return 0;
}

int isPrime(int x) {

  int i;
  if (x <= 1)
    return 0;

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

  return 1;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#define N 10000

int isPrime(int);

int main() {

  int i, j, n, x, a[N], key, count = 0;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  for (i = 0; i < n; i++) {
    if (isPrime(a[i])) {
      count++;
    }
  }

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

  return 0;
}

int isPrime(int x) {

  int i;
  if (x <= 1)
    return 0;

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

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

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

int main() {
  long n;
  cin >> n;
  long cnt = 0;
  for (long i = 0; i < n; i++) {
    long m;
    cin >> m;
    bool p = 1;
    long o;
    o = sqrt(m);
    for (long j = 0; j < o; j++) {
      if (j > 0 && m % (j + 1) == 0) {
        p = 0;
      }
    }
    if (p == 1)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}

Error: -8

Fixed submission:

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

int main() {
  long n;
  cin >> n;
  long cnt = 0;
  for (long i = 0; i < n; i++) {
    long m;
    cin >> m;
    bool p = 0;
    long o;
    o = sqrt(m);
    for (long j = 0; j < o; j++) {
      if (m % j == 0) {
        p = 1;
        break;
      }
    }
    if (p == 1)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

using namespace std;

bool isPrime(int num) {
  if (num != 2 && num % 2 == 0) {

    return false;
  }

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

  return true;
}

int main() {
  int n;
  int num;
  int ans = 0;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> num;
    if (isPrime(num)) {
      ans++;
    }
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

bool isPrime(int num) {
  if (num != 2 && num % 2 == 0) {

    return false;
  }

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

  return true;
}

int main() {
  int n;
  int num;
  int ans = 0;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> num;
    if (isPrime(num)) {
      ans++;
    }
  }

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

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

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

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

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

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

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef long long int ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, int> pli;
typedef std::pair<ll, ll> pll;

#define FOR(i, n, m) for (ll i = (ll)(m); i < (ll)(n); ++i)
#define REP(i, n) FOR(i, n, 0)
#define IREP(i, n) for (ll i = (ll)(n); i >= 0; --i)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

struct PrimeList {
  PrimeList() { prime.clear(); };
  ~PrimeList(){};

  void init(int n) {
    std::vector<bool> isComposite(n + 1);
    for (int i = 2; i <= n; ++i) {
      if (isComposite[i])
        continue;
      prime.push_back(i);
      for (int j = i; j <= n; j += i) {
        isComposite[j] = true;
      }
    }
  }

  std::vector<int> prime;
};

int A[10005];

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

  int max = 0;
  REP(i, N) {
    cin >> A[i];
    max = std::max(max, A[i]);
  }
  p.init(10001);
  sort(A, A + N);
  int sum = 0;
  int pi = 0;
  REP(i, N) {
    bool isP = true;
    REP(j, p.prime.size()) {
      if (A[i] <= p.prime[j])
        break;
      if (A[i] % p.prime[j] == 0) {
        isP = false;
        break;
      }
    }
    if (isP) {
      sum++;
    }
  }
  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef long long int ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, int> pli;
typedef std::pair<ll, ll> pll;

#define FOR(i, n, m) for (ll i = (ll)(m); i < (ll)(n); ++i)
#define REP(i, n) FOR(i, n, 0)
#define IREP(i, n) for (ll i = (ll)(n); i >= 0; --i)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

struct PrimeList {
  PrimeList() { prime.clear(); };
  ~PrimeList(){};

  void init(int n) {
    std::vector<bool> isComposite(n + 1);
    for (int i = 2; i <= n; ++i) {
      if (isComposite[i])
        continue;
      prime.push_back(i);
      for (int j = i; j <= n; j += i) {
        isComposite[j] = true;
      }
    }
  }

  std::vector<int> prime;
};

int A[10005];

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

  int max = 0;
  REP(i, N) {
    cin >> A[i];
    max = std::max(max, A[i]);
  }
  p.init(max);
  sort(A, A + N);
  int sum = 0;
  int pi = 0;
  REP(i, N) {
    while (p.prime[pi] <= A[i] && p.prime.size() > pi) {
      if (p.prime[pi] == A[i]) {
        sum++;
        break;
      }
      pi++;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

bool isPrime(int num) {
  // if(num < 2) return false;
  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: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

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

  for (int i = 3; i < num; 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;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>

int a[10000];

int judge(int n) {
  int i, a = 1;
  for (i = 2; i < sqrt(n) + 1; i++)
    if (n % i == 0 && n != 2)
      a = 0;
  return a;
}

int main() {
  int i, n, count;

  while (~scanf("%d", &n)) {
    count = 0;
    while (n--)
      scanf("%d", &a[count++]);
    int ans = 0;
    for (i = 0; i < count; i++)
      if (judge(a[i]))
        ans++;
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>

int judge(int n) {
  int i, a = 1;
  for (i = 2; i < sqrt(n) + 1; i++)
    if (n % i == 0 && n != 2)
      a = 0;
  return a;
}

int main() {
  int i, n, count;
  int a[1000];
  while (~scanf("%d", &n)) {
    count = 0;
    while (n--)
      scanf("%d", &a[count++]);
    int ans = 0;
    for (i = 0; i < count; i++)
      if (judge(a[i]))
        ans++;
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded


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

bool isPrime(int num) {
  if (num <= 1)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;
  else {
    bool prime = true;
    int divisor = 3;
    double num_d = static_cast<double>(num);
    int upperLimit = static_cast<int>(sqrt(num_d) + 1);

    while (divisor <= upperLimit) {
      if (num % divisor == 0)
        prime = false;
      divisor += 2;
    }
    return prime;
  }
}
int main() {
  unsigned int n, j;
  cin >> n;
  unsigned int num[n], count = 0;
  for (int i = 0; i < n; i++) {
    cin >> num[i];
  }
  for (int i = 0; i < n; i++) {
    if (isPrime(num[i]))
      count++;
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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


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

int GCD(int x, int y) {
  if (y == 0) {
    return x;
  }
  return GCD(y, x % y);
}

bool isPrime(int N) {
  if (N == 2)
    true;
  if (N % 2 == 0)
    false;
  for (int i = 2; i * i <= N; i++) {
    if (GCD(N, i) != 1)
      return false;
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  int primeNumberCount = 0;
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    if (isPrime(num) == true)
      primeNumberCount++;
  }
  cout << primeNumberCount << endl;
  return 0;
}


Error: TLE

Fixed submission:


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

int GCD(int x, int y) {
  if (y == 0) {
    return x;
  }
  return GCD(y, x % y);
}

bool isPrime(int N) {
  for (int i = 2; i < N; i++) {
    if (GCD(N, i) != 1)
      return false;
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  int primeNumberCount = 0;
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    if (isPrime(num) == true)
      primeNumberCount++;
  }
  cout << primeNumberCount << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int N;
  cin >> N;
  int ans = 0;
  for (int i = 0; i < N; i++) {
    int p;
    cin >> p;
    ans++;
    for (int j = 2; j * j <= p; j++) {
      if (p % j == 0) {
        ans--;
        break;
      }
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  const int MAX = 10001;
  int primes[MAX];
  for (int i = 0; i < MAX; i++)
    primes[i] = -1;

  for (int i = 2; i < MAX; i++) {
    if (primes[i] == 0)
      continue;
    primes[i] = 1;
    for (int j = i * 2; j < MAX; j += i) {
      primes[j] = 0;
    }
  }

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

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

bool prime(int);

int main() {
  int n, k, c = 0;
  cin >> n;
  while (n--) {
    cin >> k;
    if (prime(k))
      c++;
  }
  cout << c << endl;
  return 0;
}

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

Error: TLE

Fixed submission:

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

bool prime(int);

int main() {
  int n, k, c = 0;
  cin >> n;
  while (n--) {
    cin >> k;
    if (prime(k))
      c++;
  }
  cout << c << endl;
  return 0;
}

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