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

#include <math.h>
#include <stdio.h>
bool flag[1000000];
int main(void) {
  long long i, j, p, n, count;
  p = (long long)sqrt(1000000.0);
  for (i = 3; i <= p; i = i + 2) {
    if (flag[i] == false) {
      for (j = i * i; j <= 1000000; j = j + 2 * i) {
        flag[j] = true;
      }
    }
  }

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

    if (n == 1 || n == 0)
      count = 0;
    else {
      count = 1;
      for (i = 3; i <= n; i = i + 2) {
        if (flag[i] == false)
          count++;
      }
    }
    printf("%lld\\n", count);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

long store[999999];
// long storage[999999];
// int index=1;
bool IsPrime(long prime) {
  if (prime == store[prime]) {
    /*if(storage[index]!=0){
        storage[index]=storage[index]+1;
        index++;
    }*/
    return true;
  }
  // storage[index]=storage[index-1];
  // index++;
  bool ans = false;
  if (prime == 2) {
    store[prime] = prime;
    return true;
  }
  if (prime == 3) {
    store[prime] = prime;
    return true;
  }
  for (long i = 2; i <= sqrt(prime); i++) {
    if (prime % i == 0) {
      return false;
    } else
      ans = true;
  }
  if (ans == true)
    store[prime] = prime;
  return ans;
}

int main() {
  long n;

  // memset(storage,0,999999);
  while (scanf("%d", &n) == 1) {
    memset(store, -1, 999999);
    long count = 0;
    for (long i = n; i > 0; i--) {
      /*if(storage[i]!=0){
          count=count+storage[i];
          break;
      }*/
      if (IsPrime(i) == true)
        count++;
    }
    printf("%ld\\n", count);
  }

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

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

using namespace std;

#define MAX 10000000
#define LMT 3163
int sieve();
int calculation();

unsigned flag[MAX >> 6];
int primes[5761455];

#define ifc(n) (flag[n >> 6] & (1 << ((n >> 1) & 31)))
#define isc(n) (flag[n >> 6] |= (1 << ((n >> 1) & 31)))

int main() {
  sieve();
  return 0;
}

int sieve() {
  unsigned i, j, k;
  for (i = 3; i < LMT; i += 2)
    if (!ifc(i))
      for (j = i * i, k = i << 1; j < MAX; j += k)
        isc(j);
  calculation();
  return 0;
}

int calculation() {
  int Rang, count;
  unsigned int i;
  int k = 1;
  primes[0] = 2;
  for (i = 3; i < MAX; i += 2) {
    if (ifc(i) == 0)
      primes[k++] = i;
  }
  while (scanf("%d", &Rang) == 1) {
    count = 0;
    for (i = 0; i < Rang; i++) {
      if (primes[i] <= Rang) {
        count++;
      } else if (primes[i] > Rang)
        break;
    }
    cout << count << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  int n;
  while (scanf("%d", &n)) {
    int sum = 1;
    int i;
    for (int m = 3; m <= n; m++) {
      if (m % 2 == 0)
        continue;
      int k = int(sqrt(double(m)));
      i = 2;
      while (m % i && i <= k)
        i++;
      if (i > k)
        sum++;
    }
    printf("%d\\n", sum);
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string.h>
int main() {
  for (int m, n, o, c, d, e, i; !(std::cin >> n).eof();) {
    if (n < 3)
      e = n > 1;
    else {
      bool *v = new bool[c = (n & 1 ? n : --n) / 2 + 1];
      memset(v, 1, c);
      for (i = m = e = 1; i < c; i++) {
        for (m = i; m < c && !v[m];)
          m++;
        if (m == c)
          break;
        e++;
        for (d = o = m * 2 + 1; d < n + 1; d += o * 2)
          v[d / 2] = 0;
        if (n < o * o)
          break;
      }
      for (i = 1; i < c;)
        e += v[i++];
      delete[] v;
    }
    printf("%d\\n", e);
  }
}

Error: TLE

Fixed submission:

#include <iostream>
int main() {
  for (int m, n, p; !(std::cin >> n).eof();) {
    if (n < 3)
      p = !(n - 2);
    else
      for (m = 3, p = 1; m <= n; m += 2) {
        p++;
        if (m < 2)
          p--;
        else {
          for (int i = 3; i * i <= m; i += 2)
            if (m % i == 0) {
              p--;
              break;
            }
        }
      }
    printf("%d\\n", p);
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  static bool cmp[1000000] = {true};
  static int npr[1000000];
  for (int i = 2; i < 1000000; i++) {
    if (!cmp[i]) {
      for (int j = 2; j * i < 1000000; j++) {
        cmp[i * j] = true;
      }
    }
    npr[i] = npr[i - 1] + !cmp[i];
  }
  for (int n; cin >> n; n++) {
    cout << npr[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n;
  int ans[100000];
  int count = 0;
  int ans_count = 0;
  while (cin >> n) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i % j == 0 && j != 1 && j != i) {
          count++;
        }
      }
      if (count == 0) {
        ans_count++;
      }

      count = 0;
    }
    cout << ans_count - 1 << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
#define N 500000
using namespace std;

int main() {

  int n;
  int a, b, i;
  int sosu[N];
  bool judge;

  sosu[0] = 2;
  for (a = 3, i = 1; a < 1000000; a++) {
    judge = true;
    for (b = 0; sosu[b] * sosu[b] <= a; b++) {
      if (a % sosu[b] == 0) {
        judge = false;
        break;
      }
    }
    if (judge) {
      sosu[i] = a;
      i++;
    }
  }
  sosu[i] = 1000000;
  while (cin >> n) {
    if (0 < n && n < 1000000) {
      for (i = 0; sosu[i] <= n; i++)
        ;
      cout << i << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  int n;
  int a, b, i;
  bool count[1000000];
  count[2] = true;
  for (a = 3; a < 1000000; a++) {
    i = 0;
    if (a % 2 != 0) {
      for (b = 3, i = 1; b <= a / 3; b++) {
        if (b % 2 != 0) {
          if (a % b == 0) {
            i = 0;
            break;
          }
        }
      }
    }
    if (i)
      count[a] = true;
    else
      count[a] = false;
  }

  while (cin >> n) {
    for (a = 2, b = 0; a < n; a++)
      if (count[a] == true)
        b++;
    cout << b << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define INF 2000000000
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
long long gcd(long long a, long long b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
long long MOD;
template <class T> void mod(T &n) { MOD = n; }
#define sym cout << "---------" << endl;
#define ll long long
#define PL pair<long long, long long>
#define P pair<int, int>
#define mk make_pair
#define en endl
#define WHITE 1
#define BLACK 2
#define GRAY 1
#define RE return 0
#define int ll

bool prime[1000005] = {true};

signed main() {
  int n;
  while (cin >> n) {
    if (n == 1) {
      cout << 0 << en;
      continue;
    }
    for (int i = 2; i <= n; i++)
      prime[i] = true;
    int ans = 0;
    for (int i = 2; i <= n; i++) {
      if (prime[i]) {
        ans++;
        for (int j = 2 * i; j <= n; j += i)
          prime[j] = false;
      }
    }
    cout << ans << en;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define INF 2000000000
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
long long gcd(long long a, long long b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
long long MOD;
template <class T> void mod(T &n) { MOD = n; }
#define sym cout << "---------" << endl;
#define ll long long
#define PL pair<long long, long long>
#define P pair<int, int>
#define mk make_pair
#define en endl
#define WHITE 1
#define BLACK 2
#define GRAY 1
#define RE return 0
#define int ll

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

  return true;
}

bool prime[1000005] = {true};

signed main() {
  int n;
  while (cin >> n) {
    if (n == 1) {
      cout << 0 << en;
      continue;
    }
    for (int i = 2; i <= n; i++)
      prime[i] = true;
    int ans = 0;
    for (int i = 2; i <= n; i++) {
      if (isprime(i) && prime[i]) {
        ans++;
        for (int j = 2 * i; j <= n; j += i)
          prime[j] = false;
      }
    }
    cout << ans << en;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// 素数かどうか判定する関数
bool judge(int x) {

  for (int i = 2; i <= sqrt(x); i++) // 与えられた数が10なら
  {                                  //
    if (x % i == 0)
      return false;
  }

  return true;
}

int main() {
  int n;

  while (cin >> n) {
    bool PN[n + 1]; // PrimeNumber
    int count = 0;  // countは素数の数

    PN[0] = false; // 0は素数ではない。
    PN[1] = false; // 1は素数ではない。

    for (int i = 2; i <= n;
         i++) // 0,1以外の全ては素数候補。ここから篩にかける。
    {
      PN[i] = true; // ex.100の場合、2~100を素数候補とする
      count++;
    }

    for (int i = 2; i <= sqrt(n); i++) {
      for (int j = 2; j <= n / i; j++) {
        if (PN[i * j] == true) {
          PN[i * j] = false;
          count--;
        }
      }
    }
    cout << count << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

// 素数かどうか判定する関数
bool judge(int x) {

  for (int i = 2; i <= sqrt(x); i++) // 与えられた数の平方根までに含まれる素数で
  { // 割り切ることが出来たらなら、その数は素数ではない
    if (x % i == 0)
      return false;
  }

  return true;
}

int main() {
  int n;

  while (cin >> n) // 2が素数であることを予め考慮
  {
    int count = 1; // countは素数の数なので、1

    if (n <= 1) // nが1以下なら素数の数は0
    {
      count = 0;
    }

    for (int i = 3; i <= n;
         i += 2) // 3以上の自然数について、奇数のみ素数判定を行う
    {
      if (judge(i))
        count++;
    }

    cout << count << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define MAX 1000000
#define LMT 1000
unsigned flag[MAX >> 6];
int primes[6000000]; // 5761455
#define ifc(n) (flag[n >> 6] & (1 << ((n >> 1) & 31)))
#define isc(n) (flag[n >> 6] |= (1 << ((n >> 1) & 31)))

int sieve() {
  unsigned i, j, k;
  for (i = 3; i < LMT; i += 2)
    if (!ifc(i))
      for (j = i * i, k = i << 1; j < MAX; j += k)
        isc(j);
  return 0;
}

int main() {
  sieve();
  unsigned int i, L;
  int k = 1, count;
  primes[0] = 2;
  while (scanf("%d", &L) == 1) {
    count = 1;
    for (i = 3; i <= L; i += 2) {
      if (ifc(i) == 0)
        count++;
    }
    if (L > 1)
      printf("%d\\n", count);
    else
      printf("0\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
int main() {
  long long int n, m, i, j, k, l, a[1000000];
  for (i = 0; i < 100000; i++) {
    a[i] = i + 1;
  }

  m = sqrt(1000000);

  for (i = 2; i <= m; i++) {
    for (j = i + 1; j <= 1000000; j++) {
      if (a[j] % i == 0)
        a[j] = 0;
    }
  }

  while (scanf("%lld", &n) != EOF) {
    k = 0;
    for (i = 0; i < n; i++) {
      if (a[i] == 0) {

      } else {
        k++;
      }
    }
    printf("%lld\\n", k - 1);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define max 1000000

int main() {
  int prime[max];

  fill(prime, prime + max, 1);
  prime[0] = 0;
  prime[1] = 0;
  for (int i = 2; i < max; i++) {
    if (prime[i]) {
      for (int j = 2 * i; j < max; j += i) {
        prime[j] = 0;
      }
    }
  }

  for (int i = 1; i < max; i++) {
    prime[i] = prime[i] + prime[i - 1];
  }

  int aaa;
  while (cin >> aaa) {
    cout << prime[aaa] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int max = 10000;
  int prime[max];

  fill(prime, prime + max, 1);
  prime[0] = 0;
  prime[1] = 0;
  for (int i = 2; i < max; i++) {
    if (prime[i]) {
      for (int j = 2 * i; j < max; j += i) {
        prime[j] = 0;
      }
    }
  }

  for (int i = 1; i < max; i++) {
    prime[i] = prime[i] + prime[i - 1];
  }

  int aaa;
  while (cin >> aaa) {
    cout << prime[aaa] << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
typedef long long LL;
const int MOD = int(1e9) + 7;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-9;
const double PI = acos(-1.0); // M_PI;

int n;
const int maxn = 10000000;
bool is[maxn];
int sum;
void f() {
  memset(is, true, sizeof(is));
  is[0] = is[1] = false;
  int m = sqrt(maxn);
  for (int i = 2; i <= m; i++) {
    if (is[i]) {
      for (int j = i * i; j <= maxn; j += i)
        is[j] = false;
    }
  }
}

int main() {
#ifdef DoubleQ
  freopen("in.txt", "r", stdin);
#endif
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  f();
  while (cin >> n) {
    sum = 0;
    for (int i = 0; i <= n; i++) {
      if (is[i])
        sum++;
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int main() {
  int m, i, k, n;
  while (cin >> n) {
    int j = 0;
    for (m = 2; m <= n; m++) {
      k = int(sqrt(double(m)));
      i = 2;
      while (m % i && i <= k)
        i++;
      if (i > k)
        j += 1;
    }
    cout << j << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 1e6;

bool vis[maxn];
vector<int> Prms(maxn);

void seive(int n) // sqrt(n)
{
  int m = sqrt(n + 0.5);
  for (int i = 2; i <= m; i++) {
    if (!vis[i])
      for (int j = i * i; j <= n; j += i) {
        vis[j] = true;
      }
  }
  Prms.clear();
  for (int i = 2; i <= n; i++)
    if (!vis[i])
      Prms.push_back(i);
}

// #define LOCAL
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  seive(maxn);
  int n;
  while (~scanf("%d", &n)) {
    int ans = upper_bound(Prms.begin(), Prms.end(), n) - Prms.begin();
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <math.h>
#include <stdio.h>
int a[20000];
int main() {
  int i, j, q, n, co;
  while (scanf("%d", &n) == 1) {
    co = 0;
    a[0] = 1;
    a[1] = 1;
    for (i = 4; i <= n; i += 2) {
      a[i] = 1;
    }
    q = sqrt(n);
    for (i = 3; i <= q; i += 2) {
      if (a[i] == 0) {
        for (j = i * i; j < n; j += i) {
          a[j] = 1;
        }
      }
    }
    for (i = 0; i <= n; i++) {
      if (a[i] == 0) {
        co++;
      }
    }
    printf("%d\\n", co);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

const static double de_PI = 3.14159265358979323846;
const static int de_MOD = 1000000007;
const static int de_MAX = 999999999;
const static int de_MIN = -999999999;

inline int Sieve_of_Eratothenes(const int n) {

  std::vector<bool> flg(n + 1, true);
  for (int i = 2; i < n; i++) {
    if (flg[i]) {
      for (int j = i + i; j <= n; j += i) {
        flg[j] = false;
      }
    }
  }

  return (std::count(flg.begin(), flg.end(), true) - 2);
}

int main(void) {

  // std::ifstream in("123.txt");	std::cin.rdbuf(in.rdbuf());

  int a = 0;
  std::vector<int> A;
  while (std::cin >> a) {
    A.push_back(a);
  }

  std::for_each(A.begin(), A.end(), [](int i) {
    std::cout << Sieve_of_Eratothenes(i) << std::endl;
  });
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

const static double de_PI = 3.14159265358979323846;
const static int de_MOD = 1000000007;
const static int de_MAX = 999999999;
const static int de_MIN = -999999999;

bool Judge_PrimeNumber(const int a) {

  if (a < 2) {
    return false;
  }
  if (a == 2) {
    return true;
  }
  if (a % 2 == 0) {
    return false;
  }

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

int main(void) {

  // std::ifstream in("123.txt");	std::cin.rdbuf(in.rdbuf());

  int n = 0, ans = 0;
  std::vector<int> A;

  while (std::cin >> n) {
    A.push_back(n);
  }

  for (unsigned int i = 0; i < A.size(); i++) {

    if (n == 1) {
      std::cout << 0 << std::endl;
      continue;
    }
    ans = 1;
    for (int j = 3; j <= A[i]; j += 2) {
      if (Judge_PrimeNumber(j)) {
        ans++;
      }
    }
    std::cout << ans << std::endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {

  int n, count;

  while (cin >> n) {
    vector<int> list(n + 1, 0);

    for (int i = 2; i < n + 1; i++) {
      list[i] = i;
    } //???????????????????????°???????????????

    //??????????????§?¨???????????????????????????\\??£???????????????????´???°?¢????
    for (int i = 2; i < sqrt(n) + 1; i++) {
      //??????????????\\??£??????????´???°???????????????????????????
      if (list[i] == i) { // 0???????????¢?????§?????°???????????°????????§??????
        //?????????????????????????????§????????°????´???°??§??????
        //????????°????????°???0?????£??\\??????
        for (int j = i * 2; j < n + 1; j += i) {
          list[j] = 0;
        }
      }
    }

    count = 0;
    for (int i = 2; i < n + 1; i++) {

      if (list[i] == i) {
        count++;
      }
    }
    cout << count << endl;

  } // while?????????

  return 0;
}

Error: TLE

Fixed submission:

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

// 2,3,5????????°????¨?????????????????????????????¨??????????????????¨??????

int main(void) {

  int n, count_num, count_pnum;

  while (cin >> n) {
    count_num = 0;
    for (int i = 2; i < n + 1; i++) {

      // 2,3,5?????¨????????????
      if (i == 2 || i == 3 || i == 5) {
        count_num++;
      }

      if (i % 2 != 0 && i % 3 != 0 && i % 5 != 0) {
        // 2,3,5??§?????????????????¨???????´???°??§??????????????§?¨?????????????
        count_pnum = 0;
        for (int j = 7; j < i; j++) {
          if (i % j != 0)
            count_pnum++;
        }
        if (count_pnum == i - 7)
          count_num++;
      }
    }
    cout << count_num << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int n;
int main() {
  while (cin >> n) {
    int sum = 0;
    if (n <= 10000) {
      for (int i = 2; i <= n; i++) {
        for (int j = 2; j <= sqrt(i); j++) {
          if (i % j == 0) {
            goto loopa;
          }
        }
        sum++;
      loopa:;
      }
      cout << sum << endl;
    } else if (n <= 900000) {
      for (int i = 10001; i <= n; i++) {
        for (int j = 2; j <= sqrt(i); j++) {
          if (i % j == 0) {
            goto loopb;
          }
        }
        sum++;
      loopb:;
      }
      cout << sum + 1229 << endl;
    } else {
      for (int i = 900001; i <= n; i++) {
        for (int j = 2; j <= sqrt(i); j++) {
          if (i % j == 0) {
            goto loopc;
          }
        }
        sum++;
      loopc:;
      }
      cout << sum + 71274 << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int n;
int main() {
  while (cin >> n) {
    int sum = 0;
    if (n <= 10000) {
      for (int i = 2; i <= n; i++) {
        for (int j = 2; j <= sqrt(i); j++) {
          if (i % j == 0) {
            goto loopa;
          }
        }
        sum++;
      loopa:;
      }
      cout << sum << endl;
    } else if (n <= 100000) {
      for (int i = 10001; i <= n; i++) {
        for (int j = 2; j <= sqrt(i); j++) {
          if (i % j == 0) {
            goto loopb;
          }
        }
        sum++;
      loopb:;
      }
      cout << sum + 1229 << endl;
    } else {
      for (int i = 100001; i <= n; i++) {
        for (int j = 2; j <= sqrt(i); j++) {
          if (i % j == 0) {
            goto loopc;
          }
        }
        sum++;
      loopc:;
      }
      cout << sum + 9592 << endl;
    }
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, count;
  bool num[1000000];
  while (scanf("%d", &n) != EOF) {
    for (int i = 2; i <= n; i++) {
      num[i] = true;
    }
    count = 0;
    for (int i = 2; i <= sqrt(n); i++) {
      if (num[i]) {
        for (int j = i; j <= n; j += i) {
          num[j] = false;
        }
        count++;
      }
    }
    for (int i = 2; i <= n; i++) {
      count += num[i];
    }
    printf("%d\\n", count);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, a, n) for (int i = a; i < n; i++)
using namespace std;

int main(void) {
  int i, j, k, n, a;
  while (cin >> n) {
    a = 1;
    if (n == 1) {
      cout << "0\\n";
    } else {
      for (i = 3; i <= n; i += 2) {
        k = 0;
        for (j = 3; j <= sqrt(i); j += 2) {
          if (i % j == 0) {
            k = 1;
            break;
          }
        }

        if (k == 0)
          a++;
      }
    }

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

#include <iostream>
using namespace std;
bool t[999999] = {false};
int main() {
  int n, c, i, j;
  t[0] = true;
  for (i = 2; i * i <= 999999; i++) {
    if (!t[i - 1]) {
      for (j = i + i; j <= 999999; j += i)
        t[j - 1] = true;
    }
  }
  while (cin >> n) {
    if (cin.eof())
      break;
    for (i = c = 0; i < n; i++)
      if (!t[i])
        c++;
    cout << c << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

bool isPrime(int);

int main() {
  int n, p;
  while (cin >> n) {
    p = 0;
    if (cin.eof())
      return 0;
    // O(n)
    for (int i = 2; i <= n; i++) {
      if (isPrime(i))
        p++;
    }
    cout << p << endl;
  }
  return 0;
}

// O(sqrt(n))
bool isPrime(int x) {
  if (x % 2 == 0)
    return true;
  for (int j = 3; j * j <= x; j += 2) {
    if (x % j == 0)
      return false;
  }
  return true;
}

// SÌÆµÄAO(n*sqrt(n))
// ÚÀÆµÄAOÌàÌ®ª1000ÈºÈçÜ¸ÔÉ¤A1­Â´¦éÆÜ¸ÔÉíÈ¢
// O(100*1000)=10­A