alexjercan/bugnet huggingface dataset:
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAX_N = 1e6 + 5;
int prime[MAX_N];
bool is_prime[MAX_N];
int sieve() {
  memset(is_prime, true, sizeof(is_prime));
  int p = 0;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < MAX_N; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j < MAX_N; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return p;
}

int main() {
  int n;

  while (~scanf("%d", &n)) {
    int p = sieve();
    //        printf("test :\\n");
    //        for(int i = 0;i < 10;i++){
    //            printf("%d ",prime[i]);
    //        }
    //        cout <<endl;
    int num = upper_bound(prime, prime + p, n) - prime;
    printf("%d\\n", num);
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int prime[10005] = {0};
int nprime = 0;
void get_prime() {
  for (int i = 2; i < 10005; i++) {
    if (!prime[i]) {
      for (int j = i * i; j < 10005; j += i)

      {
        prime[j] = 1;
      }
    }
  }
}
void print() {
  for (int i = 1; i < 10005; i++) {
    if (prime[i] == 0) {
      cout << i << " ";
    }
  }
}
int main() {
  int n;
  get_prime();
  while (cin >> n) {
    int sum = 0;
    for (int i = 2; i <= n; i++) {
      if (prime[i] == 0) {
        sum++;
      }
    }
    cout << sum << endl;
  }

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

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

using namespace std;
int N = 999999;
int prime[999999];
int status[999999 / 32];

bool Check(int N, int pos) { return (bool)(N & (1 << pos)); }
int Set(int N, int pos) { return N = N | (1 << pos); }
void sieve() {
  int i, j, sqrtN;
  sqrtN = sqrt(N);

  for (i = 3; i <= sqrtN; i += 2) {
    if (Check(status[i / 32], i % 32) == 0) {
      for (j = i * i; j <= N; j += (2 * i))
        status[j / 32] = Set(status[j / 32], j % 32);
    }
  }
  int k = 1;
  prime[0] = 2;
  for (int i = 3; i <= N; i += 2) {
    if (Check(status[i / 32], i % 32) == 0)
      prime[k++] = i;
  }
}
int main() {
  sieve();
  int k, num, count = 0;
  k = 0;
  while (scanf("%d", &num) != EOF) {
    k = 0;
    count = 0;

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

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
int solve(int n, int start, int end);
int nextprime(int n);
int prime[500000];
int getprime(void);
int main(void) {
  int n;
  int all = getprime();
  while (scanf("%d", &n) == 1) {

    printf("%d\\n", solve(n, 0, all) + 1);
  }
}
int solve(int n, int start, int end) {

  if (end == start) {
    if (n < prime[end])
      return end - 1;
    else
      return end;
  }
  int k = (end + start) / 2;
  if (n < prime[k])
    return solve(n, start, k - 1);
  if (n > prime[k])
    return solve(n, k + 1, end);
  return k;
}
int nextprime(int n) {
  for (int i = n + 1;; i++) {
    int pan = 1;
    for (int k = 2; k <= sqrt(i); k++) {
      if (i % k == 0) {
        pan = 0;
        break;
      }
    }
    if (pan)
      return i;
  }
}
int getprime(void) {
  int i = 1, n = 0;
  while ((i = nextprime(i)) < 999999) {
    prime[n] = i;
    n++;
  }
  return n - 1;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

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

bool d[1000000];

int main() {
  int n;
  int ans;
  while (cin >> n) {
    n++;
    for (int i = 0; i < 1000000; i++)
      d[i] = false;
    d[0] = d[1] = true;
    ans = 0;
    for (int i = 2; i < n; i++) {
      if (d[i])
        continue;
      for (int j = i + i; j < n; j += i)
        d[j] = true;
    }
    for (int i = 0; i < n; i++) {
      // cout << d[i] << endl;
      ans += !d[i] ? 1 : 0;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

bool d[100000];

int main() {
  int n;
  int ans;
  while (cin >> n) {
    n++;
    for (int i = 0; i < 100000; i++)
      d[i] = false;
    d[0] = d[1] = true;
    ans = 0;
    for (int i = 2; i < n; i++) {
      if (d[i])
        continue;
      for (int j = i + i; j < n; j += i)
        d[j] = true;
    }
    for (int i = 0; i < n; i++) {
      // cout << d[i] << endl;
      ans += !d[i] ? 1 : 0;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;

  while (cin >> n) {
    bool prime[1000000];
    for (int i = 2; i <= n; i++) {
      prime[i] = true;
    }
    for (int i = 2; i <= sqrt(n); i++) {
      for (int j = i * 2; j <= n; j += i) {
        prime[j] = false;
      }
    }
    int count = 0;
    for (int i = 2; i <= n; i++) {
      if (prime[i] == true)
        count++;
    }
    cout << count << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
void cprime(int, int &);

int main() {
  int n;

  while (cin >> n) {
    int count = 0;
    for (int i = 0; i < n; i++) {
      cprime(i + 1, count);
    }
    cout << count << endl;
  }
}

void cprime(int n, int &count) {
  if (n == 2) {
    count++;
    return;
  }
  if (n == 1 || n % 2 == 0)
    return;
  for (int i = 3; i <= sqrt(n); i += 2) {
    if (n % i == 0)
      return;
  }
  count++;

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

//
//  main.cpp
//  CppTest
//
//  Created by Ryu on 2017/02/08.
//  Copyright ?? 2017??´ Ryu. All rights reserved.
//

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

int main(int argc, const char *argv[]) {
  // insert code here...
  cin.tie(0);
  ios::sync_with_stdio(false);

  long in[30];
  long out[30];
  int n = 0;
  long max = 0;
  while (cin >> in[n]) {
    if (max < in[n]) {
      max = in[n];
    }
    n++;
  }
  // printf("%d", n);

  long p[100000];
  p[0] = 2;
  int m = 1;
  for (int i = 3; i <= max; i += 2) {
    bool prime = true;
    for (int j = 0; j < m; j++) {
      if (i % p[j] == 0) {
        prime = false;
        break;
      }
      if (i < pow(p[j], 2)) {
        break;
      }
    }
    if (prime) {
      p[m] = i;
      // printf("%ld\\n", p[m]);
      m++;
    }
  }

  for (int i = 0; i < n; i++) {
    int s = 0;
    while (in[i] >= p[s] && p[s] != 0) {
      // printf("in: %ld, p: %ld\\n", in[i], p[s]);
      s++;
    }
    out[i] = s;
    printf("%ld\\n", out[i]);
  }

  return 0;
}

Error: TLE

Fixed submission:

//
//  main.cpp
//  CppTest
//
//  Created by Ryu on 2017/02/08.
//  Copyright ?? 2017??´ Ryu. All rights reserved.
//

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

int main(int argc, const char *argv[]) {
  // insert code here...
  cin.tie(0);
  ios::sync_with_stdio(false);

  long in[30];
  long out[30];
  int n = 0;
  long max = 0;
  while (cin >> in[n]) {
    if (max < in[n]) {
      max = in[n];
    }
    n++;
  }
  // printf("%d", n);

  long p[100000];
  p[0] = 2;
  int m = 1;
  for (int i = 3; i <= max; i += 2) {
    bool prime = true;
    for (int j = 0; j < m; j++) {
      if (i % p[j] == 0) {
        prime = false;
        break;
      }
      if (i / 5 < p[j]) {
        break;
      }
    }
    if (prime) {
      p[m] = i;
      // printf("%ld\\n", p[m]);
      m++;
    }
  }

  for (int i = 0; i < n; i++) {
    int s = 0;
    while (in[i] >= p[s] && p[s] != 0) {
      // printf("in: %ld, p: %ld\\n", in[i], p[s]);
      s++;
    }
    out[i] = s;
    printf("%ld\\n", out[i]);
  }

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

#include <cmath>
#include <iostream>

using namespace std;

const int M = 1e7 + 10;
bool isPrime[M];

void sieve() {
  for (int i = 2; i < M; i++)
    isPrime[i] = true;
  for (int i = 2; i * i < M; i++) {
    if (!isPrime[i])
      continue;
    for (int j = 2 * i; j < M; j += i) {
      isPrime[j] = false;
    }
  }
}

int main() {
  sieve();
  int n;
  while (cin >> n) {
    int c = 0;
    for (int i = 0; i <= n; i++) {
      if (isPrime[i])
        c++;
    }
    cout << c << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

int solve(int x) {
  int m = 0;
  for (int i = 0; i <= x; i++) {
    if (isprime(i)) {
      m++;
    }
  }
  return m;
}
int main() {
  int n;
  while ((cin >> n)) {
    cout << solve(n) << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int a;
  int i, j, k;
  int past = 1, a_sqrt;
  int count;
  bool chk[1000000];

  for (i = 1; i < 1000000; i++)
    chk[i] = true;
  while (cin >> a) {
    count = 0;
    if (past >= a) {
      for (i = 2; i <= a; i++) {
        if (chk[i])
          count++;
      }
      printf("%d\\n", count);
    } else {
      a_sqrt = (int)sqrt((double)a);
      for (i = 2; i <= a_sqrt; i++) {
        for (j = i; j <= a / i; j++) {
          chk[j * i] = false;
        }
      }
      past = a;
      for (i = 2; i <= a; i++) {
        if (chk[i])
          count++;
      }
      printf("%d\\n", count);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int a;
  int i, j;
  int past = 1, a_sqrt;
  int count;
  bool chk[1000000];

  for (i = 1; i < 1000000; i++)
    chk[i] = true;
  while (cin >> a) {
    count = 0;
    if (past >= a) {
      for (i = 1; i <= a; i++) {
        if (chk[i])
          count++;
      }
      printf("%d\\n", count);
    } else {
      a_sqrt = (int)sqrt((double)a);
      for (i = 2; i <= a_sqrt; i++) {
        for (j = past + 1; j <= a; j++) {
          if (j % i == 0)
            chk[j] = false;
        }
      }
      past = a;
      for (i = 1; i <= a; i++) {
        if (chk[i])
          count++;
      }
      printf("%d\\n", count);
    }
  }

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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long ll;
#define MAX 1000000

bool is_prime[MAX];
ll prime[MAX];

ll found(ll a) {
  ll p = 0;
  for (ll i = 0; i <= a; i++) {
    is_prime[i] = true;
  }
  is_prime[0] = false;
  is_prime[1] = false;
  for (ll i = 2; i <= a; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (ll k = 2 * i; k <= a; k = k + i) {
        is_prime[k] = false;
      }
    }
  }
  return p;
}

int main() {
  ll n;
  while (cin >> n) {
    ll q;
    q = found(n);
    cout << q << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
int main() {
  using namespace std;
  int a[31] = {0, 0, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5};
  int ans = 0, t = 0, n, i, j;

  while (scanf("%d", &n) != EOF) {
    if (n <= 11)
      cout << a[n] << endl;
    else
      for (j = 12; j <= n; ++j) {
        for (i = 2; i <= (int)sqrt(n); ++i)
          if (j % i == 0) {
            t = 1;
            break;
          }

        if (t == 0)
          a[j] = a[j - 1] + 1;
        else {
          t = 0;
          a[j] = a[j - 1];
        }
      }

    if (n > 11)
      cout << a[n] << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <ctype.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <vector>
using namespace std;
const int N = 1e6;
int isprime[N];
int prefixsum[N];
int SQRT = ceil(sqrt((double)N)); // 返回大于或者等于指定表达式的最小整数
int prime() {
  memset(isprime, 1, sizeof(isprime));
  int i;
  isprime[0] = isprime[1] = 0;
  for (i = 0; i <= SQRT; i++) {
    if (isprime[i]) {
      for (int j = i * i; j <= N; j += i) {
        isprime[j] = 0;
      }
    }
  }
}
void maketable() {
  for (int i = 1; i < N; i++) {
    prefixsum[i] = isprime[i] ? prefixsum[i - 1] + 1 : prefixsum[i - 1];
  }
}
int main() {
  prime();
  maketable();
  //	printf("%.2f\\n",(double)clock()/CLOCKS_PER_SEC);
  int n;
  while (scanf("%d", &n) != EOF) {
    printf("%d\\n", prefixsum[n]);
  }
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1e6 + 5;
int prime[maxn];
bool p[maxn];
int cnt = 0;
void is_prime() {
  memset(p, true, sizeof(p));
  p[0] = p[1] = false;
  for (int i = 2; i <= maxn; i++)
    if (p[i]) {
      prime[cnt++] = i;
      for (int j = i * i; j <= maxn; j += i)
        p[j] = false;
    }
}
int main() {
  is_prime();
  int n;
  while (scanf("%d", &n) == 1) {
    int k = lower_bound(prime, prime + cnt, n) - prime;
    if (prime[k] == n)
      k++;
    printf("%d\\n", k);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

const int N = 1e6;
const int SQRTN = ceil(sqrt((double)N));
bool isPrime[N + 1];
int prefixsum[N + 1];

// Eratosthenes筛选法
void esieve(void) {
  memset(isPrime, true, sizeof(isPrime));

  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i <= SQRTN; i++) {
    if (isPrime[i]) {
      for (int j = i * i; j <= N; j += i) // 筛选
        isPrime[j] = false;
    }
  }
}

void maketable() {
  prefixsum[0] = 0;
  for (int i = 1; i <= N; i++)
    prefixsum[i] = isPrime[i] ? prefixsum[i - 1] + 1 : prefixsum[i - 1];
}

int main() {
  esieve();

  maketable();

  int n;
  while (~scanf("%d", &n))
    printf("%d\\n", prefixsum[n]);

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 1000000 + 10;
const int maxn = 100000 + 5;
int prime[maxn];
bool p[MAXN];
int cnt = 0;
void getPrime() {
  memset(p, true, sizeof(p));
  p[0] = p[1] = false;
  for (int i = 2; i <= MAXN; i++)
    if (p[i]) {
      prime[cnt++] = i;
      for (int j = i * i; j <= MAXN; j += i)
        p[j] = false;
    }
}
int main() {
  getPrime();
  /*int k=lower_bound(prime,prime+cnt,999999)-prime;
  for(int i=k;i>=0;i--)
     if(prime[i]<999999) {
          printf("%d\\n",i+1);
          break;
     }*/
  for (int i = 0; i < cnt; i++)
    cout << prime[i] << " ";
  int n;
  while (scanf("%d", &n) == 1) {
    int k = lower_bound(prime, prime + cnt, n) - prime;
    if (prime[k] == n)
      k++;
    printf("%d\\n", k);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

#define maxn 999999 + 5
typedef long long ll;

int prime[maxn];

void init() {
  for (int i = 2; i < maxn - 4; i++)
    prime[i] = i % 2;
  prime[2] = 1;
  prime[1] = 0;
  for (int i = 2; i * i <= maxn; i++) {
    if (!prime[i])
      continue;
    for (int j = i; j * i <= maxn; j++)
      prime[i * j] = 0;
  }
  for (int i = 2; i <= maxn; i++)
    prime[i] += prime[i - 1];
}
int main() {
  int n;
  init();

  while (~scanf("%d", &n))
    printf("%d\\n", prime[n]);
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstring>
int prime(long long num) {
  for (int i = 2; i <= sqrt(num); i++)
    if (num % i == 0)
      return 0;
  return 1;
}
int main() {
  long long n;
  while (scanf("%lld", &n) != EOF) {
    int sum = 0;
    for (int i = 2; i <= n; i++)
      if (prime(i))
        sum++;
    printf("%lld\\n", sum);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
int main() {
  int a[999999];
  a[0] = 2;
  long long k = 1;
  for (long long i = 2; i <= 999999; i++) {
    int flag = 1;
    for (long long j = 2; j <= sqrt(i) + 1; j++) {
      if (i % j == 0) {
        flag = 0;
        break;
      }
    }
    if (flag)
      a[k] = i, k++;
  }
  int n;
  while (cin >> n) {
    long long ans = 0;
    for (long long x = 0; x < k; x++) {
      if (n >= a[x])
        ans++;
      else
        break;
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
bool primenum(int a) {
  int i;
  int s;
  s = (int)sqrt(a) + 1;
  if (a == 2)
    return true;
  for (i = 2; i <= s; i++) {
    if (a % i == 0) {
      break;
    }
  }
  if (i == s + 1) {
    return true;
  } else
    return false;
}
int main() {
  int n;
  while (cin >> n) {

    int counter = 0;
    for (int j = 2; j <= n; j++) {
      if (primenum(j)) {
        counter++;
      }
    }
    cout << counter << endl;
  }
  // system("pause");
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

/// BISMILLAHIR RAHMANIR RAHMIM
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000002
int pri[MAX + 2];
int cnt[MAX + 2];
void sieve(int n) {
  pri[1] = 1;
  for (int i = 2; i <= n; i++) {
    if (pri[i] == 0) {
      for (int j = i + i; j <= n; j += i) {
        pri[j] = 1;
      }
    }
  }
  int tot = 0;
  for (int i = 1; i <= n; i++) {
    if (pri[i] == 0)
      tot++;
    cnt[i] = tot;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  sieve(MAX);
  int n;
  while (cin >> n) {
    cout << cnt[n] << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
using namespace std;
int prime(int n) {
  int a, b;
  if (n == (0 || 1))
    return 0;
  else if (n == 2)
    return 1;
  else if (n % 2 == 0)
    return 0;
  a = sqrt(n);
  for (b = 3; b <= a; b += 2) {
    if (n % b == 0)
      return 0;
  }
  return 1;
}
int main() {
  int n, i, j, k;
  for (k = 1; k <= 30; k++) {
    cin >> n;
    j = 0;
    for (i = 1; i <= n; i++) {
      if (prime(i) == 1)
        j++;
    }
    cout << j << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1 << 30
#define LINF 1LL << 60

/*
<url:>
問題文============================================================

=================================================================

解説=============================================================

================================================================
*/
vector<int> prime;

void Prime() {
  prime.emplace_back(2);
  for (int i = 3; i <= 999999; i += 2) {
    bool f = false;
    for (int j = 3; j <= sqrt(i); j += 2) {
      if (i % j == 0) {
        f = true;
        break;
      }
    }
    if (!f)
      prime.emplace_back(i);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  Prime();
  int n;
  while (cin >> n) {
    cout << upper_bound(prime.begin(), prime.end(), n) - prime.begin() << endl;
  }
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
int judge(int x) {
  for (int i = 3; i < sqrt(x) + 1; i++)
    if (x % i == 0)
      return 0;
  return 1;
}
int main() {
  int a, num;
  while (scanf("%d", &a) != EOF) {
    num = 1;
    if (a < 2)
      num = 0;
    for (int i = 3; i <= a; i += 2)
      if (judge(i))
        num++;
    printf("%d\\n", num);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 1e6 + 7;
int prime[maxn];
bool is_prime[maxn];
int n, p = -1;

void Eular() {
  for (int i = 1; i <= maxn - 7; i++)
    is_prime[i] = true;
  for (int i = 2; i <= maxn - 7; i++) {
    if (is_prime[i])
      prime[++p] = i;
    for (int j = 0; j <= p && i * prime[j] <= maxn - 7; j++)
      is_prime[i * prime[j]] = false;
  }
}

int main() {
  Eular();
  while (~scanf("%d", &n)) {
    int i = 0, ans = 0;
    while (i <= p && prime[i] <= n)
      i++, ans++;
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 1e6 + 7;
int prime[maxn];
bool is_prime[maxn];
int n, p = -1;

void Eular() {
  for (int i = 1; i <= maxn - 7; i++)
    is_prime[i] = true;
  for (int i = 2; i <= maxn - 7; i++) {
    if (is_prime[i])
      prime[++p] = i;
    for (int j = 0; j <= p && i * prime[j] <= maxn - 7; j++)
      is_prime[i * prime[j]] = false;
  }
}

int main() {
  Eular();
  while (~scanf("%d", &n)) {
    int i = 0, ans = 0;
    while (prime[i] <= n)
      i++, ans++;
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  while (~scanf("%d", &n)) {
    int prime[n + 1];
    for (int i = 0; i <= n; i++)
      prime[i] = 1;
    prime[0] = 0;
    prime[1] = 0;
    for (int i = 2; i * i <= n; i++)
      if (prime[i] == 1) {
        for (int j = 2; j * i <= n; j++)
          prime[i * j] = 0;
      }
    int c = 0;
    for (int i = 0; i <= n; i++) {
      if (prime[i] == 1)
        c++;
    }
    printf("%d\\n", c);
  }
}

Error: 0

Fixed submission:

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

int arr[123456];

int main() {

  int n;
  while (scanf("%d", &n) == 1) {
    memset(arr, 0, sizeof arr);
    int k = 0;
    int s = sqrt(n + 1);

    for (int i = 4; i <= n; i = i + 2) {
      arr[i] = 1;
    }
    for (int i = 3; i <= s; i++) {
      if (arr[i] == 0) {
        for (int j = i * i; j <= n; j = j + i) {
          arr[j] = 1;
        }
      }
    }

    for (int i = 2; i <= n; i++) {
      if (arr[i] == 0) {
        k++;
      }
    }
    cout << k << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define inf 0x3f3f3f3f
#define inff -30000000
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
const int MAX = 1000000 + 10;
bool notprime[MAX];
int num = 0;
void init() {
  memset(notprime, 0, sizeof(notprime));
  for (int i = 2; i * i <= MAX; i++) {
    if (!notprime[i]) {
      for (int j = i * i; j <= MAX; j += i)
        notprime[j] = 1;
    }
  }
}
int main() {
#ifdef SIYU
  freopen("in.txt", "r", stdin);
#endif // SIYU
  int n;
  init();
  while (cin >> n) {
    int ans = 0;
    for (int i = 2; i <= n; i++) {
      if (!notprime[i])
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
int main() {
  int n;
  int data = 0;
  while (scanf("%d", &n) != EOF && data <= 30) {
    int a[n];
    int num = 0;
    for (int i = 0; i <= n; i++) {
      a[i] = i;
    }
    a[1] = 0;
    for (int i = 2; i < sqrt(n); i++) {
      for (int j = i + 1; j <= n; j++) {
        if (a[j] != 0 && a[j] % i == 0) {
          a[j] = 0;
        }
      }
    }
    for (int i = 2; i <= n; i++)
      if (a[i] != 0)
        num++;
    cout << num << endl;
    data++;
  }
  return 0;
}