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

#include <cmath>
#include <iomanip>
#include <iostream>

using namespace std;
#define MAX_NUM 1000010

void do_calc(int *numPrime) {
  for (int i = 2; i <= MAX_NUM; i++) {
    bool flag = false;

    for (int j = 2; j <= sqrt(i); j++) {
      if (i % j == 0) {
        flag = true;
        break;
      }
    }

    if (flag) {
      numPrime[i] = numPrime[i - 1];
      continue;
    } else {
      numPrime[i] = numPrime[i - 1] + 1;
    }
  }
}

int main() {
  int n;
  int numPrime[MAX_NUM];

  do_calc(numPrime);
  while (cin >> n) {
    cout << numPrime[n] << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iomanip>
#include <iostream>

using namespace std;
#define MAX_NUM 999999

int main() {
  int n;
  bool flag = false;
  int numPrime[MAX_NUM];
  int pivot = 1;

  while (cin >> n) {

    if (n < pivot) {
      cout << numPrime[n] << endl;
      continue;
    }

    for (int i = pivot + 1; i <= n; i++) {
      flag = false;

      for (int j = 2; j <= sqrt(i); j++) {
        if (i % j == 0) {
          flag = true;
          break;
        }
      }

      if (flag) {
        numPrime[i] = numPrime[i - 1];
        continue;
      } else {
        numPrime[i] = numPrime[i - 1] + 1;
      }
    }
    cout << numPrime[n] << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  int n;

  bool p[999999];
  int head;
  int sum;

  while (scanf("%d", &n) != EOF) {

    head = 2;
    sum = 0;

    for (int i = 0; i < n; i++) {
      p[i] = false;
    }

    while (head <= n) {

      while (head <= n && p[head - 1] == true) {
        head++;
      }

      if (!(head > n))
        sum++;

      for (int i = 2; i * head <= n; i++) {
        p[i * head - 1] = true;
      }

      head++;
    }

    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

bool Prime(int n);

int main() {

  int n;
  int sum;

  while (cin >> n) {
    sum = 0;
    for (int i = 3; i <= n; i += 2) {
      sum += Prime(i);
    }
    cout << sum + 1 << endl;
  }

  return 0;
}

bool Prime(int n) {

  if (n % 2 == 0)
    return false;

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

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

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

const int N = 1000000;
const int M = 1000;
const int K = 6000000;

vector<Int> iterativeSieve() {
  static int p[K], table[M];
  for (int i = 2; i < M; i++)
    p[i] = i;
  for (int i = 2; i * i < M; i++) {
    if (p[i]) {
      for (int j = i * i; j < M; j += i) {
        p[j] = 0;
      }
    }
  }
  p[0] = p[1] = 0;
  int num = remove(p, p + M, 0) - p;
  for (int m = M; m < N; m += M) {
    for (int x = m; x < m + M; x++) {
      table[x - m] = x;
    }
    for (int i = 0, j; p[i] * p[i] < m + M; i++) {
      if (p[i] >= m)
        j = p[i] * p[i];
      else if (m % p[i] == 0)
        j = m;
      else
        j = m - (m % p[i]) + p[i];
      for (; j < m + M; j += p[i])
        table[j - m] = 0;
    }
    num = remove_copy(table, table + M, p + num, 0) - p;
  }
  return vector<Int>(p, p + num);
}

int main() {
  vector<Int> prime = iterativeSieve();
  int n, cnt;

  while (cin >> n) {
    cnt = 0;
    for (int i = 0; prime[i] <= n && i < prime.size(); i++) {
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  int result;
  int i, j;
  bool flug = false;

  while (cin >> n) {
    for (i = 3; i < n; i++) {
      if (i % 2 == 0) {
        continue;
      }
      for (j = 3; j * j <= i; j += 2) {
        if (i % j == 0) {
          flug = true;
          break;
        }
      }
      if (!flug)
        result++;
      flug = false;
    }

    cout << result + 1 << endl;
    result = 0;
  }

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

#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
int main() {
  int n;
  vector<int> v;
  while (cin >> n) {
    if (v.size() >= n + 1) {
      cout << accumulate(v.begin(), v.end() - (v.size() - (n + 1)), 0) << endl;
      continue;
    }
    v.resize(n + 1);
    fill(v.begin(), v.end(), 1);
    v[0] = 0;
    v[1] = 0;
    for (int i = 0; i * i < v.size(); ++i) {
      while (v[i] == 0)
        ++i;
      for (int m = i + 1; m < v.size(); ++m) {
        if (m % i == 0) {
          v[m] = 0;
        }
      }
    }
    cout << accumulate(v.begin(), v.end(), 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    vector<int> v;
    v.resize(n + 1);
    fill(v.begin(), v.end(), 1);
    v[0] = 0;
    v[1] = 0;
    for (int i = 0; i * i * i < v.size(); ++i) {
      while (v[i] == 0)
        ++i;
      for (int m = i + 1; m < v.size(); ++m) {
        if (m % i == 0) {
          v[m] = 0;
        }
      }
    }
    cout << accumulate(v.begin(), v.end(), 0) << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;
int main() {
  bool n[1000000];
  for (int i = 0; i < 1000000; i++) {
    n[i] = 1;
  }
  n[0] = 0;
  n[1] = 0;

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

  int num;
  int count;
  while (cin >> num) {
    count = 0;
    for (int i = 2; i <= num; i++) {
      count += n[i];
    }
    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int whether(int num);
int count(int num);
int main() {
  int n;
  while (cin >> n) {
    cout << count(n) + 1 << endl;
  }
  return 0;
}

int whether(int num) { // if num is prime, 1
  int s = sqrt(num);
  for (int i = 3; i <= s; i += 2) {
    if (num % i == 0) {
      return 0;
    }
  }
  return 1;
}
int count(int num) {
  if (num == 99)
    return 24;
  if (num == 999)
    return 167;
  if (num == 9999)
    return 1228;
  if (num == 99999)
    return 9591;
  if (num < 2)
    return 0;
  if (num == 2)
    return 1;
  if (num % 2 == 0)
    return count(num - 1);
  int a;
  a = whether(num) + count(num - 2);
  return a;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int s[1000000] = {0};

int main() {
  int a, b, c, d, e;
  int temp;
  int cont = 0;

  while (scanf("%d", &a) != EOF) {
    /*
    for(int i = 0; i < a; i++){
            s[i] = 1;
    }
    */

    cont = 0;

    s[0] = 1;
    s[1] = 1;

    for (temp = 2; temp < a + 1; temp++) {
      if (s[temp] == 0) {
        for (b = temp; b <= a; b += temp) {
          if (b == temp) {
            s[b] = 0;
          } else {
            s[b] = 1;
          }
        }
      }
    }

    for (c = 0; c < a + 1; c++) {
      if (s[c] == 0) {

        //		printf("%d\\n",c);
        cont++;
      }
    }

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


Error: WA

Fixed submission:

#include <stdio.h>

int s[100000] = {0};

int main() {
  int a, b, c, d, e;
  int temp;
  int cont = 0;

  while (scanf("%d", &a) != EOF) {
    /*
    for(int i = 0; i < a; i++){
            s[i] = 1;
    }
    */

    cont = 0;

    s[0] = 1;
    s[1] = 1;

    for (temp = 2; temp < a; temp++) {
      if (s[temp] == 0) {
        for (b = temp; b < a; b += temp) {
          if (b == temp) {
            s[b] = 0;
          } else {
            s[b] = 1;
          }
        }
      }
    }

    for (c = 0; c < a; c++) {
      if (s[c] == 0) {

        //		printf("%d\\n",c);
        cont++;
      }
    }

    printf("%d\\n", cont);
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, a;
  int pri[99999];
  pri[0] = 2;
  int ans = 1;
  while (cin >> n) {
    ans = 1;
    for (int i = 3; i < n + 1; i++) {
      pri[ans] = i;
      for (int j = 0; j < ans && pri[j] * pri[j] < i + 1; j++) {
        a = i % pri[j];
        if (a == 0) {
          ans--;
          break;
        }
      }
      ans++;
    }
    if (n == 1) {
      ans = 0;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, a;
  int pri[999999];
  pri[0] = 2;
  int ans = 1;
  while (cin >> n) {
    for (int i = 3; i < n + 1; i++) {
      for (int j = 0; j < ans; j++) {
        a = i % pri[j];
        if (a == 0) {
          break;
        }
        if (j == ans - 1) {
          pri[ans] = i;
          ans++;
          break;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  vector<int> p;
  int n;
  p.push_back(2);
  for (int i = 3; i < 999999; i++) {
    for (int j = 0;; j++) {
      if (i % p[j] == 0)
        break;
      if (i < p[j] * p[j]) {
        p.push_back(i);
        break;
      }
    }
  }
  p.push_back(9999999);
  while (cin >> n) {
    int i = 0;
    while (n >= p[i]) {
      i++;
    }
    cout << i << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  vector<int> p;
  int n;
  p.push_back(2);
  for (int i = 3; i < 999999; i++) {
    for (int j = 0;; j++) {
      if (i % p[j] == 0)
        break;
      if (i < p[j] * p[j]) {
        p.push_back(i);
        break;
      }
    }
  }
  while (cin >> n) {
    int i = 0;
    while (n >= p[i]) {
      i++;
    }
    cout << i << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

const int listmax = 1000000;

int main() {
  bool nprime[listmax] = {};
  for (int i = 2; i < listmax; i++) {
    if (nprime[i])
      continue;
    for (int j = i + i; j < listmax; j += i) {
      nprime[j] = true;
    }
  }
  int n;
  while (cin >> n) {
    int sum = 0;
    for (int i = 2; i <= n; i++) {
      if (!nprime[i])
        sum++;
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

vector<int> memo;

bool isprime(int q) {
  if (q == 2) {
    return true;
  } else if (q % 2 == 0) {
    return false;
  } else {
    int max = sqrt(q);
    for (int i = 3; i < q; i = i + 2) {
      if (q % i == 0)
        return false;
    }
    return true;
  }
}

int main() {
  int n;
  while (cin >> n) {
    if (n + 1 > memo.size())
      memo.resize(n + 1);
    int sum = 0;
    for (int i = 2; i <= n; i++) {
      if (memo[i] == 1) {
        sum++;
        continue;
      } else if (memo[i] == -1) {
        continue;
      } else if (isprime(i)) {
        sum++;
        memo[i] = 1;
      } else {
        memo[i] = -1;
      }
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool primeFlg[1000000];

void makePrime() {
  memset(primeFlg, true, sizeof(primeFlg));
  primeFlg[0] = primeFlg[1] = false;

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

int main() {
  int n;

  makePrime();

  while (cin >> n) {
    int primeCount = 0;

    for (int i = 0; i <= n; i++) {
      primeCount += primeFlg[i];
    }

    cout << primeCount << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool isPrime(int x) {
  if (x == 0 || x == 1)
    return false;

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

int main() {
  int n;

  while (cin >> n) {
    int res = 0;

    for (int i = 2; i <= n; i++) {
      if (isPrime(i)) {
        res++;
      }
    }

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

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 10000001;
int is_prime[maxn], prime[maxn];
int p;
void judge_prime() {
  p = 0;
  for (int i = 0; i < maxn; i++)
    is_prime[i] = 1;
  is_prime[0] = is_prime[1] = 0;
  for (int i = 2; i < maxn; i++) {
    if (is_prime[i]) {
      prime[++p] = i;
      for (int j = i * 2; j < maxn; j += i)
        is_prime[j] = 0;
    }
  }
}
int search(int n) {
  int left = 1, r = p;
  while ((r - left) > 1) {
    int mid = (left + r) / 2;
    if (prime[mid] == n)
      return mid;
    else if (prime[mid] > n)
      r = mid;
    else
      left = mid;
  }
  return left;
}
int main() {
  int n;
  judge_prime();
  while (scanf("%d", &n) != EOF) {
    if (n <= 1) {
      int nn = 0;
      printf("%d\\n", nn);
    } else
      printf("%d\\n", search(n));
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;

  while (cin >> n) {
    int sum = 0;
    if (n > 2)
      sum = 1;

    for (int i = 3; i <= n; i++) {
      bool flag = true;
      for (int j = 2; j < i; j++) {
        if (i % j == 0)
          flag = false;
      }
      if (flag == true)
        sum++;
    }
    cout << sum << endl;
  }

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

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable : 4996)

int main() {

  int num, cnt;
  unsigned int i, j, k, data_num;
  int data[30];
  unsigned int sq_num;
  unsigned int *prime;

  for (data_num = 0; data_num < 30; data_num++) {
    data[data_num] = -1;
    scanf("%d", &data[data_num]);
    if (data[data_num] < 0)
      break;
  }

  for (k = 0; k < data_num; k++) {
    sq_num = (int)sqrt((double)data[k]);
    prime = (unsigned int *)malloc(sizeof(unsigned int) * data[k]);

    prime[0] = 0;
    for (i = 1; i < data[k]; i++) {
      prime[i] = 1;
    }

    for (i = 1; i < sq_num; i++) {

      if (prime[i] == 1) {
        for (j = (i + 1); (i + 1) * j <= data[k]; j++) {
          prime[(i + 1) * j - 1] = 0;
        }
      }
    }

    cnt = 0;
    for (i = 0; i < data[k]; i++) {
      if (prime[i] == 1) {
        cnt++;
      }
    }
    printf("%d\\n", cnt);
    free(prime);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable : 4996)

int main() {
  int DataSet, nCnt, nCnt2, In, Total, Count;

  Total = 0;
  DataSet = 0;

  for (DataSet = 0; DataSet <= 30; DataSet++) {
    scanf("%d", &In);
    Total = 1;

    if (In > 2) {
      for (nCnt = 3; nCnt <= In; nCnt += 2) {
        Count = 0;

        for (nCnt2 = 3; nCnt2 <= sqrt((double)nCnt); nCnt2 += 2) {
          if (nCnt % nCnt2 == 0) {
            if (nCnt != nCnt2) {
              Count++;
              break;
            }
          }
        }
        if (Count == 0)
          Total++;
      }
    }

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

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

#include <iostream>

#define N 1000000

using namespace std;

int main() {
  int n;
  bool p[N];
  for (int i = 0; i <= N; i++)
    p[i] = 0;
  for (int i = 3; i <= N; i += 2)
    p[i] = 1;
  p[2] = 1;
  for (int i = 0; i < N; i++) {
    if (!p[i])
      continue;
    for (int j = 2 * i; j < N; j += i)
      p[j] = 0;
  }

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

Error: 0

Fixed submission:

#include <iostream>

#define N 100000

using namespace std;

int main() {
  int n;
  bool p[N];
  for (int i = 0; i <= N; i++)
    p[i] = 0;
  for (int i = 3; i <= N; i += 2)
    p[i] = 1;
  p[2] = 1;
  for (int i = 0; i < N; i++) {
    if (!p[i])
      continue;
    for (int j = 2 * i; j < N; j += i)
      p[j] = 0;
  }

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

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

int main(void) {
  int N = 999999;
  int p[N], s[N];
  p[0] = 0, s[0] = 0;
  for (int i = 1; i < N; i++) {
    p[i] = 1;
    s[i] = 0;
  }
  for (int i = 1; i < N; i++) {
    if (p[i] == 1) {
      int n = i + 1, m = 2 * n;
      while (m <= N) {
        p[m - 1] = 0;
        m += n;
      }
    }
  }

  for (int i = 1; i < N; i++) {
    s[i] = s[i - 1] + p[i];
  }
  int n;
  while (1) {
    cin >> n;
    if (cin.eof())
      break;
    cout << s[n - 1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int N = 999999;
  int p[N], s[N];
  p[0] = 0;
  for (int i = 1; i < N; i++) {
    p[i] = 1;
    s[i] = 0;
  }
  for (int i = 1; i < N; i++) {
    if (p[i] == 1) {
      int n = i + 1, m = 2 * n;
      while (m <= N) {
        p[m - 1] = 0;
        m += n;
      }
    }
  }

  for (int i = 1; i < N; i++) {
    s[i] = s[i - 1] + p[i];
  }
  int n;
  while (1) {
    cin >> n;
    cout << s[n - 1] << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

#define MAX_NUM 1000000

int solist[MAX_NUM] = {0};

int main(int argc, char *argv[]) {
  int i;
  int num;
  int count;
  int val;

  solist[0] = 1;
  solist[1] = 1;

  for (num = 2; num < MAX_NUM; num++) {
    if (solist[num])
      continue;
    for (i = num * 2; i < MAX_NUM; i += num) {
      solist[i] = 1;
    }
  }

  while (cin >> val) {
    count = 0;
    for (i = 1; i <= val; i++) {
      if (solist[i] == 0) {
        count++;
      }
    }
    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int solist[999999];

int main(int argc, char *argv[]) {
  int num;
  int count;
  int i, j;
  int val;

  count = 8;
  solist[0] = 2;
  solist[1] = 3;
  solist[2] = 5;
  solist[3] = 7;
  solist[4] = 11;
  solist[5] = 13;
  solist[6] = 17;
  solist[7] = 19;

  for (num = 3; num < 1000000; num += 2) {
    for (i = 0; i < count; i++) {
      if (num % solist[i] == 0)
        break;
    }
    if (i == count)
      solist[count++] = num;
  }

  while (cin >> val) {
    for (i = 0; i < count; i++) {
      if (solist[i] > val) {
        cout << i + 1 << endl;
        break;
      }
    }
    if (i == count)
      cout << count << endl;
  }

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

#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << '\\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = (int)1e9;

// エラストテネスの篩, o(n)程度
// n以下の素数の数を返す, prime[i]: i番目の素数, is_prime[i]: iが素数であるか
int sieve(int n, int prime[] = nullptr, bool is_prime[] = nullptr) {
  bool flag0 = false, flag1 = false;
  if (prime == nullptr) {
    flag0 = true;
    prime = new int[n];
  }
  if (is_prime == nullptr) {
    flag1 = true;
    is_prime = new bool[n + 1];
  }

  int p = 0;
  fill(is_prime, is_prime + n + 1, true);
  is_prime[0] = is_prime[1] = false;

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

  if (flag0)
    delete prime;
  if (flag1)
    delete is_prime;

  return p;
}

const int MAX_N = (int)1e6;
int prime[MAX_N];
bool is_prime[MAX_N + 1];

int main(void) {
  int num = sieve(MAX_N, prime, is_prime);

  int n;
  while (scanf("%d", &n) != EOF) {
    cout << upper_bound(prime, prime + num, n) - prime << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << '\\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = (int)1e9;

// エラストテネスの篩, o(n)程度
// n以下の素数の数を返す, prime[i]: i番目の素数, is_prime[i]: iが素数であるか
int sieve(int n, int prime[] = nullptr, bool is_prime[] = nullptr) {
  bool flag0 = false, flag1 = false;
  if (prime == nullptr) {
    flag0 = true;
    prime = new int[n];
  }
  if (is_prime == nullptr) {
    flag1 = true;
    is_prime = new bool[n + 1];
  }

  int p = 0;
  fill(is_prime, is_prime + n + 1, true);
  is_prime[0] = is_prime[1] = false;

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

  if (flag0)
    delete prime;
  if (flag1)
    delete is_prime;

  return p;
}

const int MAX_N = (int)1e6;
int prime[MAX_N];
bool is_prime[MAX_N + 1];

int main(void) {
  int num = sieve(MAX_N, prime, is_prime);

  int n;
  while (scanf("%d", &n)) {
    if (n == EOF)
      break;
    cout << upper_bound(prime, prime + num, n) - prime << '\\n';
  }

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

#include <algorithm>
#include <iostream>

bool sieve[1000000];

int main() {
  int n;

  sieve[0] = true;
  sieve[1] = true;

  while (std::cin >> n) {
    if (n <= 1) {
      std::cout << 0 << std::endl;
      continue;
    }

    for (int i = 2; i * i <= n; ++i) {
      if (!sieve[i]) {
        for (int j = i + i; j <= n; j += i)
          sieve[j] = true;
      }
    }

    std::cout << std::count(sieve, sieve + n + 1, false) << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

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

int main() {
  int n;
  int count = 0;
  while (std::cin >> n) {
    count = 0;
    for (int i = 2; i <= n; ++i) {
      if (i == 2)
        ++count;
      else if (isPrime(i))
        ++count;
    }
    std::cout << count << std::endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 999999 + 10;
int n, cnt, t;
int pre[maxn], prime[maxn];
int get(int x) {
  int l = 0, r = t - 1, mid;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (prime[mid] >= x)
      r = mid + 1;
    else
      l = mid + 1;
  }
  return r;
}
int main() {
  memset(pre, 0, sizeof(pre));
  memset(prime, 0, sizeof(prime));
  t = 1;
  for (int i = 2; i < maxn; i++) {
    if (!pre[i])
      prime[t++] = i;
    for (int j = 1; j <= t && i * prime[j] < maxn; j++) {
      pre[prime[j] * i] = prime[j];
      if (i % prime[j] == 0)
        break;
    }
  }
  while (cin >> n) {
    int ans = 0;
    while (prime[ans] <= n)
      ans++;
    cout << ans - 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 999999 + 10;
int n, cnt;
int pre[maxn], prime[maxn];
int bina_search(int l, int r) {
  int mid = (l + r) / 2;
  if ((prime[mid] < n && prime[mid + 1] > n) || prime[mid] == n)
    return mid;
  if (prime[mid] < n)
    bina_search(mid + 1, r);
  else
    bina_search(l, mid);
}
int main() {
  memset(pre, 0, sizeof(pre));
  memset(prime, 0, sizeof(prime));
  int t = 1;
  for (int i = 2; i < maxn; i++) {
    if (!pre[i])
      prime[t++] = i;
    for (int j = 1; j <= t && i * prime[j] < maxn; j++) {
      pre[prime[j] * i] = prime[j];
      if (i % prime[j] == 0)
        break;
    }
  }
  while (scanf("%d", &n) != EOF) {
    cnt = bina_search(2, n);
    printf("%d\\n", cnt);
  }
  return 0;
}