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

#include <iostream>

using namespace std;

bool flag[10000001];

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

  for (int i = 0; i <= 1000000; i++) {
    if (i <= 1)
      flag[i] = false;
    else
      flag[i] = true;
  }
  for (int i = 2; i <= 1001; i++) {
    if (flag[i] == true) {
      for (int j = i * 2; j <= 1000000; j += i) {
        flag[j] = false;
      }
    }
  }
  while (cin >> n) {
    ans = 0;
    for (int i = 0; i <= n; i++) {
      if (flag[i] == true) {
        ans++;
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;

  while (cin >> n) {
    int pn = 0;
    for (int i = 2; i <= n; i++) {
      bool flag = true;
      for (int j = 2; j <= n; j++) {
        if (i % j == 0 && i != j) {
          flag = false;
          break;
        }
      }
      if (flag)
        pn++;
    }
    cout << pn << endl;
  }

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

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

using namespace std;

#define max 999999 + 1

int main(void) {

  vector<bool> table(max, true);
  vector<int> p(max, 0);
  table[0] = false;
  table[1] = false;

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

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

  int n;

  while (scanf("%d", &n) != EOF) {
    cout << p[n] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

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

int main(void) {

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

    int cnt = 0;
    for (int i = 2; i <= n; i++)
      if (isPrime(i))
        cnt++;
    cout << cnt << endl;
  }

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

#include <iostream>
using namespace std;
int main() {
  int prime[80000];
  int ptr = 2;
  prime[0] = 2;
  prime[1] = 3;
  for (int num = 5; num < 1000000; num += 2) {
    bool f = false;
    for (int i = 0; i < ptr; i++) {
      if (prime[i] * prime[i] > num)
        break;
      if (num % prime[i] == 0) {
        f = true;
        break;
      }
    }
    if (!f)
      prime[ptr++] = num;
  }
  prime[ptr] = 1000001;
  int n;
  while (cin >> n) {
    int k = 0;
    for (; prime[k] <= n; k++)
      ;
    cout << k << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int a, pp[100001];
  pp[0] = -1, pp[1] = -1;
  for (int i = 2; i < 100001; i++) {
    if (pp[i] == 0) {
      pp[i] = 1;
      for (int j = i * 2; j < 100001; j += i) {
        pp[j] = -1;
      }
    }
  }
  int now;
  while (cin >> now) {
    int ans = 0;
    for (int i = 1; i < now + 1; i++) {
      if (pp[i] == 1) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

/*
 * main.cpp
 *
 *  Created on: 2011/04/08
 *      Author: satoki
 *      5:09 ~ 6:03
 *      Wrong Answer : 2
 *      nÈºÌfð¢ÅÀ
 *      Runtime Error : 4
 *      PÂ½ßÉfðZbg
 */
#include <iostream>
#define MAX_iP 1000004 // 664,580 ÔÚÌf = 10,000,019
#define MAX_nP                                                                 \\
  78499 // ÔºÌÓ¡àßÄ (KvÈfÌ + 1)
        // ðmÛ
using namespace std;

int iN;

// fe[u
bool bPrime[MAX_iP];
int iPrime[MAX_nP];
int nPrime;

void setPrime() {
  // fe[uðì¬
  int i, j;

  nPrime = 0;
  fill(bPrime, bPrime + MAX_iP, true);
  bPrime[0] = bPrime[1] = false;

  for (i = 2; i < MAX_iP; i++) {
    if (bPrime[i]) {
      iPrime[nPrime++] = i;
      for (j = 2; i * j < MAX_iP; j++) {
        bPrime[i * j] = false;
      }
      // if (nPrime == MAX_nP) break;
    }
  }
  // printf("nPrime = %d i = %d\\n", nPrime, i);
}

void init() { setPrime(); }

bool input() {
  if (scanf("%d", &iN) == EOF)
    return false;
  return true;
}

int manage() {
  int i;
  for (i = 0; iPrime[i] <= iN; i++)
    ;
  return i;
}

int main() {
  init();
  while (1) {
    if (!input())
      break;
    printf("%d\\n", manage());
  }
  return 0;
}

Error: 0

Fixed submission:

/*
 * main.cpp
 *
 *  Created on: 2011/04/08
 *      Author: satoki
 *      5:09 ~ 6:03
 *      Wrong Answer : 2
 *      nÈºÌfð¢ÅÀ
 *      Runtime Error : 4
 *      PÂ½ßÉfðZbg
 */
#include <iostream>
#define MAX_iP 1000000 // 664,580 ÔÚÌf = 10,000,019
#define MAX_nP                                                                 \\
  78498 // ÔºÌÓ¡àßÄ (KvÈfÌ + 1)
        // ðmÛ
using namespace std;

int iN;

// fe[u
bool bPrime[MAX_iP];
int iPrime[MAX_nP];
int nPrime;

void setPrime() {
  // fe[uðì¬
  int i, j;

  nPrime = 0;
  fill(bPrime, bPrime + MAX_iP, true);
  bPrime[0] = bPrime[1] = false;

  for (i = 2; i < MAX_iP; i++) {
    if (bPrime[i]) {
      iPrime[nPrime++] = i;
      for (j = 2; i * j < MAX_iP; j++) {
        bPrime[i * j] = false;
      }
      // if (nPrime == MAX_nP) break;
    }
  }
  // printf("nPrime = %d i = %d\\n", nPrime, i);
}

void init() { setPrime(); }

bool input() {
  if (scanf("%d", &iN) == EOF)
    return false;
  return true;
}

int manage() {
  int i;
  for (i = 0; iPrime[i] <= iN; i++)
    ;
  return i;
}

int main() {
  init();
  while (1) {
    if (!input())
      break;
    printf("%d\\n", manage());
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
const int MAX_N = 1000000; // いくつまで調べるか
bool prime[MAX_N + 1];     // 添字が素数ならtrue、それ以外はfalse
void sieve() {
  prime[0] = prime[1] = false; // 例外処理
  // 配列prime の初期化
  for (int i = 2; i <= MAX_N; ++i)
    prime[i] = true;
  // 素数でないものを調べる
  for (int i = 2; i <= MAX_N; ++i)
    if (prime[i])
      for (int j = i * 2; j <= MAX_N; j += i)
        prime[j] = false;
}

int is_prime(int n) { return prime[n]; }

/*bool is_prime(int n)
{
  if(n == 1)return false;
  for(int i = 2; i < n; ++i)
    if(n % i == 0)return false;
  return true;
  }
*/
int main() {
  sieve();
  int a;
  while (cin >> a) {
    // cin >> a;
    // if(a==EOF)return 0;
    int count = 0;
    // int tmp;
    for (int i = 1; i <= a; i++) {
      if (is_prime(i))
        count++;
    }
    printf("%d\\n", count);
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
const int MAX_N = 1000000; // いくつまで調べるか
bool prime[MAX_N + 1];     // 添字が素数ならtrue、それ以外はfalse
/*void sieve()
{
  prime[0] = prime[1] = false; // 例外処理
  // 配列prime の初期化
  for(int i = 2; i <= MAX_N; ++i)
    prime[i] = true;
  // 素数でないものを調べる
  for(int i = 2; i <= MAX_N; ++i)
    if(prime[i])
      for(int j = i * 2; j <= MAX_N; j += i)
        prime[j] = false;
        }*/

/*int is_prime(int n)
{
  return prime[n];
  }*/

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

int main() {
  // sieve();
  int a;
  while (cin >> a) {
    // cin >> a;
    // if(a==EOF)return 0;
    int count = 0;
    // int tmp;
    for (int i = 1; i <= a; i++) {
      if (is_prime(i))
        count++;
    }
    printf("%d\\n", count);
  }
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  bool a[1000000];
  for (int abc = 2; abc <= 999999; abc++) {
    a[abc] = true;
  }
  for (int abc = 2; abc <= 999999; abc++) {
    if (a[abc] == true) {
      for (int ab = abc * 2; ab <= 999999; ab += abc) {
        a[ab] = false;
      }
    }
  }
  int ad;
  while (cin >> ad) {
    int ansa = 0;
    for (int abc = 2; abc <= ad; abc++) {
      if (a[abc] == true) {
        ansa++;
      }
    }
    cout << ansa << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int time;
  vector<int> v;
  v.push_back(2);
  v.push_back(3);
  while (cin >> time) {
    int abcd = 0;
    if (time > v[v.size() - 1]) {
      for (int abc = 1 + v[v.size() - 1]; abc <= time; abc += 2) {
        for (int bcd = 0; bcd < (int)v.size(); bcd++) {
          if (abc % v[bcd] == 0) {
            goto end;
          }
        }
        v.push_back(abc);
      end:;
      }
    }
    for (int abc = 0; v[abc] <= time; abc++) {
      abcd++;
    }
    cout << abcd << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define rFOR(i, a, b) for (int i = a - 1; i >= b; --i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define rREP(i, n) for (int i = n - 1; i >= 0; --i)
#define all(a) a.begin(), a.end()
#define MAXNUM 1000000
using namespace std;

bool num[MAXNUM];
void erast(int n) {
  FOR(i, 2, floor(sqrt(n)) + 1) {
    if (num[i]) {
      FOR(j, 2, n) {
        if (i * j >= MAXNUM)
          break;
        num[i * j] = false;
      }
    }
  }
}

int main() {
  int n;
  REP(i, MAXNUM) num[i] = true;
  num[0] = false;
  num[1] = false;
  while (cin >> n) {
    int ans = 0;
    erast(n);
    REP(i, n + 1) {
      if (num[i]) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define rFOR(i, a, b) for (int i = a - 1; i >= b; --i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define rREP(i, n) for (int i = n - 1; i >= 0; --i)
#define all(a) a.begin(), a.end()
using namespace std;

int main() {
  int n;
  while (cin >> n) {
    int ans = 0;
    vector<int> vec;
    vec.push_back(2);
    for (int i = 3; i <= n; i += 2) {
      bool isPrimeN = true;
      REP(j, vec.size()) {
        if (i % vec[j] == 0) {
          false;
        }
      }
      for (int j = 3; j <= floor(sqrt(i)); j++) {
        if (i % j == 0) {
          isPrimeN = false;
          break;
        }
      }
      if (isPrimeN) {
        vec.push_back(i);
      }
    }
    cout << vec.size() << endl;
    ;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX (1000000)

int main(void) {
  char prime[MAX + 1];
  int n;
  char buf[256];

  memset(prime, 1, sizeof(prime));

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

  while (fgets(buf, 256, stdin) != NULL) {
    int ans;

    n = atoi(buf);

    ans = 0;
    for (int i = 2; i <= n; i++) {
      ans += prime[i];
    }

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

  return (0);
}

Error: 0

Fixed submission:

#include <stdio.h>

#define MAX (100000)

int main(void) {
  int prime[MAX + 1] = {1};
  int n;
  int ans;
  int i;
  char str[100];

  for (int i = 0; i <= MAX; i++) {
    prime[i] = 1;
  }

  prime[0] = prime[1] = 0;
  i = 0;
  while (i * i <= MAX) {
    while (prime[++i] != 1 && i * i <= MAX)
      ;
    // printf("%d ", i);
    for (int j = 2; j * i <= MAX; j++) {
      // printf("%d\\n", i * j);
      prime[i * j] = 0;
    }
  }

  while (fgets(str, 100, stdin)) {
    sscanf(str, "%d", &n);

    ans = 0;
    for (int i = 2; i <= n; i++) {
      if (prime[i] == 1) {
        ans++;
      }
    }

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

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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main() {
  int n;
  while (std::cin >> n) {
    std::vector<bool> v(n + 1, false);
    int c = 0;
    v[0] = true, v[1] = true;
    for (int i = 2; i * i <= n; ++i) {
      if (!v[i]) {
        for (int j = i * i; j <= n; j += i) {
          if (!v[j])
            c++;
          v[j] = true;
        }
      }
    }

    std::cout << n - c - 1 << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

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

#include <cmath>
#include <iostream>

bool IsPrimeNumber(int number) {
  if (number < 2)
    return false;
  if (number == 2)
    return true;
  if (number % 2 == 0)
    return false;
  int max = (int)std::sqrt(number);
  for (int i = 3; i <= max; i += 2)
    if (number % i == 0)
      return false;
  return true;
}

int main() {
  int N[1000000] = {0};
  for (int i = 1; i <= 999999; i++) {
    if (IsPrimeNumber(i)) {
      N[i] = N[i - 1] + 1;
    } else {
      N[i] = N[i - 1];
    }
  }
  int n;
  while (std::cin >> n) {
    std::cout << N[n] << std::endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

bool CheckPrimeNumber(int number) {
  int max = (int)std::sqrt(number);
  for (int i = 3; i <= max; i += 2)
    if (number % i == 0)
      return false;
  return true;
}

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

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

typedef long int lint;
typedef long long int llint;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
typedef pair<long, long> pll;

#define EACH(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, e) for (int i = (s); i < (int)(e); ++i)
#define REP(i, n) FOR(i, 0, n)
#define endl "\\n"

const int INF = 1000000;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <typename T> void print(const std::vector<T> &v, char sep = ' ') {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i != 0)
      cout << sep;
    cout << v[i];
  }
  cout << endl;
}

int prime[INF];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;

  REP(i, INF) prime[i] = 1;

  prime[0] = prime[1] = 0;

  for (int i = 2; i * i <= INF; i++) {
    if (prime[i]) {
      for (int j = 2; i * j <= INF; j++) {
        prime[i * j] = 0;
      }
    }
  }

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

Error: MLE

Fixed submission:

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

typedef long int lint;
typedef long long int llint;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
typedef pair<long, long> pll;

#define EACH(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, e) for (int i = (s); i < (int)(e); ++i)
#define REP(i, n) FOR(i, 0, n)
#define endl "\\n"

const int INF = 100000000;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <typename T> void print(const std::vector<T> &v, char sep = ' ') {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i != 0)
      cout << sep;
    cout << v[i];
  }
  cout << endl;
}

int prime[INF];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;

  REP(i, INF) prime[i] = 1;

  prime[0] = prime[1] = 0;

  for (int i = 2; i * i <= INF; i++) {
    if (prime[i]) {
      for (int j = 2; i * j <= INF; j++) {
        prime[i * j] = 0;
      }
    }
  }

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

#include <iostream>

using namespace std;

#define LN 1000001

int main() {
  int n;
  static int pn[LN];

  for (int i = 0; i < LN; i++) {
    pn[i] = 1;
  }

  pn[0] = pn[1] = 0;

  for (int i = 2; i * i < LN; i++) {
    if (pn[i] == 1) {
      for (int j = i * i; j < LN; j += i) {
        pn[j] = 0;
      }
    }
  }

  for (int i = 1; i < LN - 1; i++) {
    pn[i + 1] += pn[i];
  }

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

  return (0);
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main(void) {
  int n;
  int pnum;

  while (cin >> n) {
    int ct = 0;
    for (int i = 2; i <= n; i++) {
      for (pnum = 2; pnum <= n; pnum++) {
        if (i % pnum == 0) {
          break;
        }
      }
      if (i == pnum) {
        ct++;
      }
    }
    cout << ct << endl;
  }

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

#include <stdio.h>

const int MAX_V = 1000000;
int prime[MAX_V + 1];

void init() {
  int i, k, v;
  for (i = 2; i < MAX_V; i++) {
    prime[i] = 1;
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] = 0;
      }
    }
  }
}

int isprime(int n) { return prime[n]; }

int main() {
  init();
  int num;
  while (scanf("%d", &num) != EOF) {
    int count = 0;
    for (int i = 2; i <= num; i++) {
      if (isprime(i) == 1)
        count++;
    }
    printf("%d\\n", count);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

const int MAX_V = 10000;
int prime[MAX_V + 1];

void init() {
  int i, k, v;
  for (i = 2; i < MAX_V; i++) {
    prime[i] = 1;
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] = 0;
      }
    }
  }
}

int isprime(int n) { return prime[n]; }

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

#include <iostream>
#include <vector>
using namespace std;
#define SIZE 1000000
int main() {
  vector<bool> bo(SIZE, false);
  for (int i = 2; i < SIZE; i++) {
    if (bo[i] == false) {
      for (int j = i * 2; j < SIZE; j += i) {
        bo[j] = true;
      }
    }
  }
  vector<int> ans(SIZE);

  for (int i = 2; i < SIZE; i++) {
    if (bo[i] == false) {
      ans[i] = ans[i - 1] + 1;
    } else {
      ans[i] = ans[i - 1];
    }
  }
  int n;
  while (cin >> n) {
    cout << ans[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <stack>
using namespace std;
int dp[999999];
void shokika();
int main() {
  int n, ans = 0, i, count = 0, count2 = 0, insq;
  double sq;
  bool bo = false;
  shokika();
  while (cin >> n) {
    for (i = 2; i <= n; i++) {
      if (dp[i] == 1) {
        ans++;
        bo = true;
      } else {
        bo = false;
      }
      // sq = sqrt(i);
      // insq = (int)sq;
      for (int j = 2; j <= i; j++) {
        if (bo == true) {
          break;
        } else if (bo == false && dp[i] != -1) {
          count = 0;
          break;
        }
        /*
        if ( (i % 2 == 0 || i % 3 == 0 || i % 5 == 0 || i % 7 == 0 || i % 11 ==
        0)) { count = 0; break;

        }
        */
        if (i % j == 0) {
          count++;
        }
        if (count >= 2) {
          break;
        }
        /*
        else {
                cout << i <<" "<<count<< endl;
        }
        */
      }
      if (bo == true) {
        continue;
      }
      if (dp[i] == -1) {
        dp[i] = count;
      }
      if (dp[i] == 1) {
        //	cout << i << endl;
        ans++;
      }
      count = 0;
    }
    cout << ans << endl;
    ans = 0;
  }

  /*
  for (int i = 1; i < n; i++) {
          if (n%i == 0) {
                  count++;
          }
          if (count >= 2) {
                  count = 0;
                  continue;
          }
          else  {
                  ans++;
          }
  }
  cout << ans << endl;
  ans = 0;
}
*/
  return 0;
}
void shokika() {
  for (int i = 0; i < 999999; i++) {
    dp[i] = -1;
  }
  return;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
int main() {
  const long N = 1000000;
  vector<bool> primes(N, true);
  primes[0] = primes[1] = false;
  for (int i = 2; i * i < N; i++) {
    if (!primes[i])
      continue;
    for (int j = i * i; j < N; j += i)
      primes[j] = false;
  }
  vector<long> answer(N);
  answer[0] = 0;
  for (long i = 1; i < N; i++)
    answer[i] = answer[i - 1] + primes[i];
  long n;
  while (cin >> n)
    cout << answer[n] << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  long n;
  while (cin >> n) {
    if (n == 1) {
      cout << 0 << endl;
      continue;
    }
    int x = 1;
    if (n == 2) {
      cout << x << endl;
      continue;
    }
    for (int i = 3; i <= n; i += 2) {
      bool prime = true;
      int limit = i / 2;
      for (int j = 2; j <= limit; j++) {
        if (i % j == 0) {
          prime = false;
          break;
        }
      }
      if (prime)
        x++;
    }
    cout << x << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  bool isPrime[n_size];
  isPrime[0] = false;
  isPrime[1] = false;

  for (unsigned i = 2; i < n_size; i++)
    isPrime[i] = true;

  for (unsigned i = 2; i * i < n_size; i++) {
    if (isPrime[i]) {
      for (unsigned j = (i * i); j < n_size; j += i)
        isPrime[j] = false;
    }
  }

  unsigned n_prime[n_size];

  unsigned cnt = 0;
  for (unsigned i = 0; i < n_size; i++) {
    if (isPrime[i])
      cnt++;
    n_prime[i] = cnt;
  }

  unsigned n;
  while (cin >> n) {
    cout << n_prime[n] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

bool isPrimeNumber(const unsigned long n);

int main() {
  unsigned long n;
  while (cin >> n) {
    unsigned ans = 0;
    for (unsigned long i = n; i > 0; i--) {
      if (isPrimeNumber(i)) {
        ans++;
      }
    }
    cout << ans << endl;
  }
}

bool isPrimeNumber(const unsigned long n) {
  static const unsigned long carn[]{
      561,     1105,    1729,    2465,    2821,    6601,    8911,    10585,
      15841,   29341,   41041,   46657,   52633,   62745,   63973,   75361,
      101101,  115921,  126217,  162401,  172081,  188461,  252601,  278545,
      294409,  314821,  334153,  340561,  399001,  410041,  449065,  488881,
      512461,  530881,  552721,  656601,  658801,  670033,  748657,  825265,
      838201,  852841,  997633,  1024651, 1033669, 1050985, 1082809, 1152271,
      1193221, 1461241, 1569457, 1615681, 1773289, 1857241, 1909001, 2100901,
      2113921, 2433601, 2455921, 2508013, 2531845, 2628073, 2704801, 3057601,
      3146221, 3224065, 3581761, 3664585, 3828001, 4335241, 4463641, 4767841,
      4903921, 4909177, 5031181, 5049001, 5148001, 5310721, 5444489, 5481451,
      5632705, 5968873, 6049681, 6054985, 6189121, 6313681, 6733693, 6840001,
      6868261, 7207201, 7519441, 7995169, 8134561, 8341201, 8355841, 8719309,
      8719921, 8830801, 8927101, 9439201, 9494101, 9582145, 9585541, 9613297,
      9890881};

  if (n == 2)
    return true;
  else if (n < 2 || n % 2 == 0)
    return false;
  else {
    for (unsigned long i = 0; i < sizeof(carn) / sizeof(carn[0]); i++) {
      if (n < carn[i])
        break;
      if (n == carn[i])
        return false;
    }
    for (unsigned long i = n - 1; i > 0; i--) {
      if (fmodl(powl(i, n), n) != i)
        return false;
    }
    return true;
  }
}