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

#include <iostream>

int main() {
  int prime[1000000] = {0};
  int number[1000000];
  int k, n;

  number[1] = 0;

  for (int i = 2; i < 1000000; i++) {
    if (prime[i] == 0) {
      for (int j = 2; i * j < 1000000; j++) {
        prime[i * j] = 1;
      }
    }
    if (prime[i] == 0) {
      number[i] = number[i - 1] + 1;
    } else {
      number[i] = number[i - 1];
    }
  }
  while (std::cin >> n) {
    std::cout << number[n] << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

int main() {
  int prime[200000];
  int k, n;
  prime[0] = 2;
  while (std::cin >> n) {
    if (n == 1) {
      std::cout << 0 << std::endl;
      continue;
    }
    k = 0;
    for (int i = 3; i <= n; i++) {
      for (int j = 0; j <= k; j++) {
        if (i % prime[j] == 0) {
          break;
        }
        if (j == k) {
          ++k;
          prime[k] = i;
        }
      }
    }
    std::cout << k + 1 << std::endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int sosuu(int n);

int main() {
  int n = 0;
  int num = 0;
  int a[30][2] = {0};
  int max = 0;
  while (scanf("%d", &n) != EOF) {
    if (max < n) {
      max = n;
    }
    a[num][0] = n;
    num++;
  }

  int b[max];
  int c[max];
  int so = 0;
  for (int i = 0; i <= max; i++) {
    b[i] = i;
  }
  b[1] = 0;

  for (int i = 2; i < sqrt(max); i++) {
    if (b[i] != 0) {
      for (int j = i + 1; j <= max; j++) {
        if (j % i == 0) {
          b[j] = 0;
        }
      }
    }
  }

  for (int i = 0; i <= max; i++) {
    if (b[i] != 0) {
      c[so] = i;
      so++;
    }
  }

  //    for(int j=0; j<num; j++){
  //        cout<<a[j][0]<<"\\n";
  //    }
  //
  //    for(int j=0; j<=so; j++){
  //        cout<<c[j]<<"\\n";
  //    }

  for (int i = 0; i < num; i++) {
    for (int j = 0; j < so; j++) {
      if (c[j] <= a[i][0]) {
        a[i][1]++;
      } else {
        j = so;
      }
    }
  }

  for (int j = 0; j < num; j++) {
    cout << a[j][1] << "\\n";
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

int sosuu(int n);

int main() {
  int n = 0;
  int num = 0;
  int a[30][2] = {0};
  int max = 0;
  while (scanf("%d", &n) != EOF) {
    if (max < n) {
      max = n;
    }
    a[num][0] = n;
    num++;
  }

  int b[max];
  int c[max];
  int so = 0;
  for (int i = 0; i <= max; i++) {
    b[i] = i;
  }
  b[1] = 0;

  for (int i = 2; i < sqrt(max); i++) {
    if (b[i] = !0) {
      for (int j = i + 1; j <= max; j++) {
        if (j % i == 0) {
          b[j] = 0;
        }
      }
    }
  }

  for (int i = 0; i <= max; i++) {
    if (b[i] != 0) {
      c[so] = i;
      so++;
    }
  }

  //    for(int j=0; j<num; j++){
  //        cout<<a[j][0]<<"\\n";
  //    }
  //
  //    for(int j=0; j<=so; j++){
  //        cout<<c[j]<<"\\n";
  //    }

  for (int i = 0; i < num; i++) {
    for (int j = 0; j < so; j++) {
      if (c[j] <= a[i][0]) {
        a[i][1]++;
      } else {
        j = so;
      }
    }
  }

  for (int j = 0; j < num; j++) {
    cout << a[j][1] << "\\n";
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
using namespace std;

int main() {
  int n, ans = 0, prime[1000000];
  prime[0] = 0;
  prime[1] = 0;
  for (int i = 2; i < 1000000; ++i) {
    prime[i] = 1;
  }
  for (int i = 2; i < 1000000; ++i) {
    if (prime[i] == 1) {
      ++ans;
      if (i < 1000)
        for (int j = i * i; j < 1000000; j += i)
          prime[j] = 0;
    }
    prime[i] = ans;
  }
  while (scanf("%d", &n) != EOF) {
    printf("%d\\n", prime[n]);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

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

int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    int cnt = 0;
    for (int i = 2; i <= n; ++i) {
      if (prime(i)) {
        ++cnt;
      }
    }
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// bool isPrime(unsigned int n);

int main() {
  int n;
  while (cin >> n) {
    vector<int> primeTable;
    if (n == 1) {
      cout << "0" << endl;
      continue;
    }
    primeTable.push_back(2);
    vector<int>::iterator j;
    for (int i = 3; i <= n; i += 2) {
      for (j = primeTable.begin(); j != primeTable.end(); j++) {
        if (!(i % (*j)))
          break;
        if ((*j) * (*j) >= i) {
          j = primeTable.end();
          break;
        }
      }
      if (j == primeTable.end())
        primeTable.push_back(i);
    }
    cout << primeTable.size() << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

// bool isPrime(unsigned int n);

int main() {
  int n;
  while (cin >> n) {
    vector<int> primeTable;
    if (n == 1) {
      cout << "0" << endl;
      continue;
    }
    primeTable.push_back(2);
    for (int i = 3; i <= n; i++) {
      vector<int>::iterator j;
      for (j = primeTable.begin(); j != primeTable.end(); j++) {
        if (!(i % (*j)))
          break; // i%j!=0
      }
      if (j == primeTable.end())
        primeTable.push_back(i);
    }
    cout << primeTable.size() << endl;
  }

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

#include <iostream>
using namespace std;

int main() {

  int n;

  while (cin >> n) {
    bool Prime[1000000] = {false};
    int i, j;

    for (i = 2; i <= n; i++) {
      if (Prime[i] == false) {

        for (j = 2; i * j <= n; j++) {
          Prime[i * j] = true;
        }
      }
    }
    int count = 0;
    for (i = 2; i <= n; i++) {
      if (Prime[i] == false) {
        count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  int n, i, j, ans;

  while (cin >> n) {
    ans = 0;

    for (; n > 1; n--) {
      if (n == 2)
        ans++;
      for (i = 2; i <= (int)sqrt((double)n); i++) {
        if (n % i == 0) {
          break;
        }
      }
      if (n % i != 0)
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

#define MAX 1000000

int main() {
  int n;
  bool p[MAX];
  p[0] = p[1] = false;
  for (int i = 2; i < MAX; i++)
    p[i] = true;
  for (int i = 2; i < MAX; i++) {
    if (p[i]) {
      for (int j = i + i; j < MAX; j = j + i) {
        p[j] = false;
      }
    }
  }

  while (cin >> n) {
    int cnt = 0;
    for (int i = 0; i <= n; i++) {
      if (p[i])
        cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;

  while (cin >> n) {
    int cnt = 0;
    for (int i = 2; i <= n; i++) {
      for (int j = 2; j < i; j++) {
        if (i % j == 0)
          break;
        if (i == j + 1)
          cnt++;
      }
    }
    cout << cnt + 1 << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
bool prime[1000100] = {0};
int main() {
  int N;
  prime[1] = 1;
  for (int i = 2; i * i < 1000000; i++) {
    if (prime[i] == 0) {
      for (int j = i; j < 1000000; j += i) {
        prime[j] = 1;
      }
      prime[i] = 0;
    }
  }
  while (~scanf("%d", &N)) {
    int cnt = 0;
    for (int i = 1; i <= N; i++) {
      if (prime[i] == 0)
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N;
  bool prime[100010] = {0};
  prime[1] = 1;
  for (int i = 2; i * i < 100000; i++) {
    if (prime[i] == 0) {
      for (int j = i; j < 100000; j += i) {
        prime[j] = 1;
      }
      prime[i] = 0;
    }
  }
  while (~scanf("%d", &N)) {
    int cnt = 0;
    for (int i = 1; i <= N; i++) {
      if (prime[i] == 0)
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef tuple<int, int> duo;
const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const int Mod = 1000000000 + 0;
//{{{ templates
#define TT_ template <typename T> inline
#define TTF_ template <typename T, typename F> inline
TT_ T sq(T x) { return x * x; }
TT_ T In() {
  T x;
  cin >> x;
  return x;
}
TT_ void Out(T &x) { cout << x; }
TT_ void sort(T &v) { sort(begin(v), end(v)); }
TT_ void revs(T &v) { reverse(begin(v), end(v)); }
TT_ void uniq(T &v) {
  sort(v);
  v.erase(unique(begin(v), end(v)), end(v));
}
TT_ int ubnd(T &v, typename T::value_type x) {
  return upper_bound(begin(v), end(v), x) - begin(v);
}
TT_ int lbnd(T &v, typename T::value_type x) {
  return lower_bound(begin(v), end(v), x) - begin(v);
}
TTF_ void inpt(T &v, int n, F f) {
  for (v.reserve(n); n--; v.emplace_back(f()))
    ;
}
TTF_ void show(T &v, F f, string d = " ", string e = "\\n") {
  int i = 0;
  for (auto &x : v)
    i++ && (cout << d), f(x);
  cout << e;
}
TT_ typename T::iterator minel(T &v) { return min_element(begin(v), end(v)); }
TT_ typename T::iterator maxel(T &v) { return max_element(begin(v), end(v)); }

inline void fast_io() {
  ios::sync_with_stdio(0);
  cin.tie(0);
}
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
inline ll pow_mod(ll a, ll k, ll m) {
  ll r = 1;
  for (; k > 0; a = a * a % m, k >>= 1)
    if (k & 1)
      r = r * a % m;
  return r;
}
inline ll mod_inv(ll a, ll p) { return pow_mod(a, p - 2, p); }
//}}} priority_queue queue deque front stringstream max_element min_element
//insert count make_tuple

template <int N> struct Sieve {
  int F[N];
  vector<int> P;
  Sieve() {}
  void build() {
    iota(F, F + N, 0);
    for (int p = 2; p < N; ++p) {
      if (F[p] == p)
        P.emplace_back(p);
      for (int i = 0; i < P.size() && P[i] <= F[p]; ++i) {
        int tmp = P[i] * p;
        if (tmp >= N)
          break;
        F[tmp] = P[i];
      }
    }
  }
  int count(int l, int r) {
    if (l)
      return count(0, r) - count(0, l);
    return lbnd(P, r);
  }
};

int main() {
  Sieve<1000000> sieve;
  sieve.build();

  for (int n; ~scanf("%d", &n);) {
    printf("%d\\n", sieve.count(0, n + 1));
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const int kMaxN = 1000000;

int main() {
  vector<int> prime;
  prime.reserve(100000);
  prime.push_back(2);
  prime.push_back(3);
  prime.push_back(5);
  prime.push_back(7);
  for (int i = 9; i <= kMaxN; i += 2) {
    bool ok = true;
    for (int j = 0, j_max = prime.size(); j < j_max; ++j) {
      if (i % prime[j] == 0) {
        ok = false;
        break;
      }
    }
    if (ok)
      prime.push_back(i);
  }

  int n;
  while (scanf("%d", &n) != EOF) {
    int count = upper_bound(prime.begin(), prime.end(), n) - prime.begin();
    printf("%d\\n", count);
  }

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

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

int main(void) {
  int n;
  bool p[1000000];
  p[0] = p[1] = 0;
  p[2] = 1;
  int ans = 0;
  bool flag;
  for (int i = 0; i < 1000000; i++)
    p[i] = false;
  p[2] = true;
  for (int i = 3; i <= 1000000; i += 2) {
    flag = true;
    for (int j = 3; j * j <= i; j += 2) {
      if (i % j == 0) {
        flag = false;
        break;
      }
    }
    if (flag)
      p[i] = true;
  }
  // cout << "end" << endl;
  while (cin >> n) {
    int ans = 0;
    for (int i = 2; i <= n; i++) {
      if (p[i])
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int n;
  while (cin >> n) {
    if (n < 3) {
      if (n < 2) {
        cout << 0 << endl;
      } else {
        cout << 1 << endl;
      }
    }
    int ans = 0;
    for (int i = 3; i <= n; i += 2) {
      bool flag = true;
      for (int j = 3; j <= sqrt(i); j += 2) {
        if (i % j == 0) {
          flag = false;
        }
      }
      if (flag)
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <set>
#include <vector>
std::vector<int> PRIMES;

void init_primes(int max) {
  if (max < 2)
    return;
  std::set<int> candidate;
  PRIMES.push_back(2);
  for (int i = 3; i < max; i += 2) {
    candidate.insert(i);
  }
  for (int i = 3; i < sqrt(max); i += 2) {
    std::set<int>::iterator it = candidate.find(i);
    if (it != candidate.end()) {
      PRIMES.push_back(i);
      int del_num = i;
      while (del_num < max) {
        it = candidate.find(del_num);
        if (it != candidate.end())
          candidate.erase(it);
        del_num += i;
      }
    }
  }
  for (std::set<int>::iterator it = candidate.begin(); it != candidate.end();
       ++it) {
    PRIMES.push_back(*it);
  }
}

int main() {
  const int max = 1000000;
  init_primes(max);
  int i;
  while (std::cin >> i) {
    std::vector<int>::iterator it = PRIMES.begin();
    int n = 0;
    while (*it <= i && it != PRIMES.end()) {
      n++;
      it++;
    }
    std::cout << n << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <set>
#include <vector>
std::vector<int> PRIMES;

void init_primes(int max) {
  if (max < 2)
    return;
  std::set<int> candidate;
  PRIMES.push_back(2);
  for (int i = 3; i < max; i += 2) {
    candidate.insert(i);
  }
  for (int i = 3; i < sqrt(max); i += 2) {
    std::set<int>::iterator it = candidate.find(i);
    if (it != candidate.end()) {
      PRIMES.push_back(i);
      int del_num = i;
      while (del_num < max) {
        it = candidate.find(del_num);
        if (it != candidate.end())
          candidate.erase(it);
        del_num += i;
      }
    }
  }
  for (std::set<int>::iterator it = candidate.begin(); it != candidate.end();
       ++it) {
    PRIMES.push_back(*it);
  }
}

int main() {
  const int max = 1000000;
  init_primes(max);
  int i;
  while (std::cin >> i) {
    std::vector<int>::iterator it = PRIMES.begin();
    int n = 0;
    while (*it <= i) {
      n++;
      it++;
    }
    std::cout << n << std::endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

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

int main() {
  int n;
  int r[30];
  bool p[1000000];
  for (int i = 1; i <= 999999; i++) {
    p[i] = IsPrime(i);
  }
  for (int i = 0; i < 30; i++) {
    r[i] = 0;
  }
  int j = 0;
  while (cin >> n) {
    for (int i = 1; i <= n; i++) {
      r[j] += p[i];
    }
    cout << r[j] << endl;
    j++;
  }
  return (0);
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

int main() {
  int n;
  int r[30];
  for (int i = 0; i < 30; i++) {
    r[i] = 0;
  }
  int j = 0;
  while (cin >> n) {
    for (int i = 1; i <= n; i++) {
      if (IsPrime(i))
        r[j]++;
    }
    cout << r[j] << endl;
    j++;
  }
  return (0);
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int N;
  bool is_prime[1000000];
  is_prime[0] = 0;
  is_prime[1] = 0;
  for (int i = 2; i < 1000000; i++) {
    is_prime[i] = 1;
  }

  for (long long s = 2; s < 1000000; s++) {
    if (is_prime[s] == 1) {
      for (long long i = s * s; i < 1000000; i += s) {
        is_prime[i] = 0;
      }
    }
  }

  while (cin >> N) {

    int ans = 0;
    for (int i = 2; i < N + 1; i++) {
      ans += is_prime[i];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int N;
  while (cin >> N) {
    int is_prime[N + 1];
    is_prime[0] = 0;
    is_prime[1] = 0;
    for (int i = 2; i < N + 1; i++) {
      is_prime[i] = 1;
    }

    for (int s = 2; s < N + 1; s++) {

      if (is_prime[s] == 1) {
        for (int i = s * s; i < N + 1; i += s) {
          is_prime[i] = 0;
        }
      }
    }
    int ans = 0;
    for (int i = 2; i < N + 1; i++) {
      ans += is_prime[i];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int a[999999];

int prime(int x) {
  if (a[x] == 1)
    return 1;
  else if (a[x] == 2)
    return 0;
  for (int i = 2; i < sqrt(x) + 1; i++) {
    if (x % i == 0) {
      a[x] = 2;
      return 0;
    }
  }
  return a[x] = 1;
}
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    int count = 0;
    for (int i = 2; i <= n; i++) {
      if (prime(i))
        count++;
    }
    count++;
    if (n == 0)
      count = 0;
    if (n == 1)
      count = 0;
    if (n == 2)
      count = 1;
    printf("%d\\n", count);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int prime(int x) {
  for (int i = 2; i < sqrt(x) + 1; i++) {
    if (x % i == 0)
      return 0;
  }
  return 1;
}
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    int count = 0;
    for (int i = 2; i <= n; i++) {
      if (prime(i))
        count++;
    }
    count++;
    if (n == 0)
      count = 0;
    if (n == 1)
      count = 0;
    if (n == 2)
      count = 1;
    printf("%d\\n", count);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int vtoad(int);

int main(void) {
  char *val;
  int c, N, ad;

  while (cin >> N) {
    c = 2;
    if (N == 1 || N == 2)
      c = N - 1;
    else {
      val = new char[N / 3 + 2];
      for (int i = 0; i < N / 3 + 2; i++)
        val[i] = 0;

      for (int i = 1; i * 6 - 1 <= N; i++)
        for (int j = -1; j <= 1; j += 2)
          if (i * 6 + j <= N && (ad = vtoad(i * 6 + j)))
            if (!val[ad]) {
              c++;
              for (int k = 2; (i * 6 + j) * k <= N; k++)
                if (ad = vtoad((i * 6 + j) * k))
                  val[ad] = 1;
            }
      delete[] val;
    }
    cout << c << endl;
  }

  return 0;
}

int vtoad(int val) {
  if (!((val + 1) % 6))
    return (val + 1) / 3 - 1;
  else if (!((val - 1) % 6))
    return (val - 1) / 3 + 2;
  else
    return 0;
}

Error: WA

Fixed submission:

#include <iostream>

using namespace std;

int main(void) {
  int val;
  while (cin >> val)
    cerr << val << endl;

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

#include <iostream>
#include <vector>
int main() {
  int n;
  while (std::cin >> n) {
    int ans = 0;
    std::vector<int> vec(n + 1);
    for (int i = 0; i <= n; i++)
      vec[i] = 1;
    vec[0] = vec[1] = 0;
    for (int i = 2; i <= n; i++) {
      if (vec[i] == 1) {
        ans++;
        for (int j = 2 * i; j <= n; j += i) {
          vec[j] = 0;
        }
      }
    }
    std::cout << ans << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
int fact(int x) {
  int sx = 0, sy = 0;
  for (int i = 2; i < x + 1; i++) {
    if (x % i == 0)
      sx++, sy += i;
  }
  if (sx == 1)
    return x;
  else
    return sy + 1;
}
int main() {
  int a = 0;
  while (std::cin >> a) {
    int n = 0;
    for (int i = 2; i < a + 1; i++) {
      if (i == fact(i))
        n++;
    }
    std::cout << n << std::endl;
  }

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

#include <iostream>

using namespace std;

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

  while (cin >> n) {
    int count = 0;
    bool flag[n + 1];

    for (i = 2; i < n + 1; i++)
      flag[i] = true;

    for (i = 2; i < n + 1; i++) {
      if (!flag[i]) {
        continue;
      }
      for (j = 2 * i; j < n + 1; j += i) {
        flag[j] = false;
      }
    }

    // 個数を走査
    for (i = 2; i < n + 1; i++) {
      if (flag[i] == true) {
        count++;
      }
    }

    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

  while (cin >> n) {
    int flug[n + 1];
    int count = 0;
    for (i = 0; i < n + 1; i++)
      flug[i] = 1;
    // 0, 1は除外
    flug[0] = flug[1] = 0;
    for (i = 2; i * i < n; i++) {
      for (k = n; k >= 2; k--) {
        if ((k % i == 0) && (k != i)) {
          flug[k] = 0;
        }
      }
    }

    // 個数を走査
    for (i = 2; i < n + 1; i++) {
      if (flug[i] == 1) {
        count++;
      }
    }

    cout << count << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
using namespace std;
bool prime[1000000];
#define max 1000000
int main() {
  prime[1] = true;
  for (int i = 2; i * i < max; i++) {
    if (!prime[i]) {
      for (int j = i * i; j < max; j += i) {
        prime[j] = true;
      }
    }
  }
  int n;
  while (scanf("%d", &n) != EOF) {
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (!prime[i])
        ans++;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
using namespace std;
bool prime[1000000];
#define max 1000000
int main() {
  prime[1] = true;
  for (int i = 2; i < max; i++) {
    if (!prime[i]) {
      for (int j = i * i; j < max; j += i) {
        prime[j] = true;
      }
    }
  }
  int n;
  while (scanf("%d", &n) != EOF) {
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (!prime[i])
        ans++;
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, count;

  while (cin >> n) {
    bool table[1000000];
    count = 0;

    for (int i = 0; i <= n; i++) {
      table[i] = true;
    }

    for (int i = 2; i <= n; i++) {
      if (table[i]) {
        count++;
        for (int j = 2; i * j <= n; j++) {
          table[i * j] = false;
        }
      }
    }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int gcd(int, int);

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

  return true;
}

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

  while (cin >> n) {
    for (int i = 2; i <= n; i++) {
      if (isPrime(i)) {
        count++;
      }
    }

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

  return 0;
}