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

#include <iostream>

using namespace std;

bool F[1000003] = {false};
int DP[80000] = {0};
void prepa_Prime();
int count_Prime(int);

int main() {
  int n;

  prepa_Prime();
  while (cin >> n) {
    cout << count_Prime(n) << endl;
    ;
  }
  return 0;
}

void prepa_Prime() {
  int i, c = 2;

  F[0] = F[1] = true;
  DP[0] = 1;

  DP[1] = 2;
  for (i = 4; i <= 1000003; i += 2) {
    F[i] = true;
  }

  for (i = 3; i <= 1000003; i += 2) {
    if (F[i] == false) {
      DP[c++] = i;
      for (int j = i * 2; j <= 1000003; j += i) {
        F[j] = true;
      }
    }
  }
}

int count_Prime(int n) {
  int i = 0;
  for (i; DP[i + 1] <= n; i++) {
  }
  return i;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

bool isPrime(int x);

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

    int count = 0;
    for (int i = 1; i < n; i++) {
      if (isPrime(i))
        count++;
    }

    cout << count << endl;
  }

  return 0;
}

bool isPrime(int x) {
  if (x < 2)
    return false;
  else if (x == 2)
    return true;

  if (x % 2 == 0)
    return false;
  for (int i = 3; i * i <= x; i += 2) {
    if (x % i == 0)
      return false;
  }
  return true;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
const int MAX_N = 1000000;
int n;
int num[MAX_N];
int ans = 0;

int main() {

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

  while (scanf("%d", &n) != -1) {
    ans = 0;
    for (int i = 0; i < n + 1; i++) {
      if (num[i] == 1) {
        ans++;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int n;
int num[1000000] = {0};
int ans = 0;

int del(int x) {
  for (int i = x; i < 999999; i++) {
    if (i % x == 0) {
      num[i] = 1;
    }
  }
  return 0;
}

int main() {
  while (scanf("%d", &n) != -1) {
    for (int i = 0; i < 999999; i++) {
      num[i] = 0;
    }
    ans = 0;
    for (int i = 1; i < n + 1; i++) {
      if (i == 1) {

      } else if (num[i] == 0) {
        ans++;
        del(i);
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  vector<int> prime(MAX, 1);
  vector<int> ans;

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

  for (int i = 4; i < MAX; i += 2)
    prime[i] = 0;

  for (int i = 3; i < sqrt(MAX); i += 2)
    for (int j = i + i; j < MAX; j += i)
      prime[j] = 0;

  int n;

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

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {

  int n;

  while (cin >> n) {

    int cnt = 0;

    if (n <= 1) {
      cout << 0 << endl;
      continue;
    }

    else
      cnt++;

    for (int i = 3; i <= n; i += 2) {
      int f = true;
      for (int j = 3; j <= sqrt(i); j += 2) {
        if (i % j == 0) {
          f = false;
          break;
        }
      }
      if (f == true)
        cnt++;
    }

    cout << cnt << endl;
  }

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

#include <cstdio>
#include <iostream>

using namespace std;

int main() {
  int prime[1000000];
  bool is_prime[1000000];
  int i = 2, j = 2, n, count = 0;
  while (scanf("%d", &n) != EOF) {
    int p = 0;
    for (i = 0; i <= n; i++) {
      is_prime[i] = true;
    }
    is_prime[0] = is_prime[1] = false;
    for (i = 2; i <= n; i++) {
      if (is_prime[i]) {
        prime[p++] = i;
        for (j = 2 * i; j <= n; j += i)
          is_prime[j] = false;
      }
    }
    cout << p << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int main() {
  int prime[40];
  bool is_prime[40];
  int i = 2, j = 2, n, count = 0;
  while (scanf("%d", &n) != EOF) {
    int p = 0;
    for (i = 0; i <= n; i++) {
      is_prime[i] = true;
    }
    is_prime[0] = is_prime[1] = false;
    for (i = 2; i <= n; i++) {
      if (is_prime[i]) {
        prime[p++] = i;
        for (j = 2 * i; j <= n; j += i)
          is_prime[j] = false;
      }
    }
    cout << p << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

int f(int a) {
  if (a == 1)
    return 0;
  for (int i = 2; i <= sqrt((double)a); i++) {
    if (!(a % i)) {
      return 0;
    }
  }
  return 1;
}
bool solve() {
  int n;
  int nmax = 0;
  vector<int> k;
  while (cin >> n) {
    k.push_back(n);
    if (nmax < n)
      nmax = n;
  }

  vector<int> res(nmax + 1);
  int cnt = 0;
  for (int j = 2; j <= nmax; j++) {
    cnt += f(j);
    res[j] = cnt;
  }
  for (int i = 0; i < k.size(); i++) {
    cout << res[k[i]] << endl;
  }

  return true;
}

int main() {
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();

  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

int f(int a) {
  if (a == 1)
    return 0;
  for (int i = 2; i <= sqrt((double)a); i++) {
    if (!(a % i)) {
      return 0;
    }
  }
  return 1;
}
bool solve() {
  int n;
  while (cin >> n) {
    int cnt = 0;
    for (int j = 2; j <= n; j++) {
      cnt += f(j);
    }
    cout << cnt << endl;
  }

  return true;
}

int main() {
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();

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

#include <iostream>
using namespace std;

int main() {
  int a, m[1000000], cou = 1, j;
  m[1] = 0;
  m[2] = 1;
  for (int i = 3; i < 1000000; i++) {
    if (i % 2 == 0) {
      m[i] = cou;
      continue;
    }
    for (j = 3; j * j <= i; j += 2)
      if (i % j == 0)
        break;
    if (j * j > i)
      cou++;
    m[i] = cou;
  }
  while (cin >> a) {
    cout << m[a] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int a, j, cou;
  while (cin >> a) {
    cou = 0;
    for (int i = 2; i <= a; i++) {
      if (i == 2) {
        cou++;
        continue;
      } else if (i % 2 == 0)
        continue;
      for (j = 3; j * j <= i; j += 2)
        if (i % j == 0)
          break;
      if (j * j > i)
        cou++;
    }
    cout << cou << 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 *isPrime = new bool[n + 1];
    isPrime[0] = false;
    isPrime[1] = false;
    for (int i = 2; i < n + 1; i++) {
      isPrime[i] = true;
    }
    for (int i = 2; i <= (int)sqrt(n); i++) {
      if (isPrime[i]) {
        for (int j = 2; j <= n / i; j++) {
          isPrime[i * j] = false;
        }
      }
    }
    int ans = 0;
    for (int i = 2; i <= n; i++) {
      if (isPrime[i]) {
        ans++;
      }
    }
    cout << ans << endl;
    delete[] isPrime;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

int main() {
  int n;

  while (cin >> n) {
    bool *isPrime = new bool[n + 1];
    isPrime[0] = false;
    isPrime[1] = false;
    for (int i = 2; i < n + 1; i++) {
      isPrime[i] = true;
    }
    for (int i = 2; i <= (int)sqrt(n); i++) {
      if (isPrime[i]) {
        for (int j = i + 1; j <= n; j++) {
          if (isPrime[j]) {
            if (!(j % i)) {
              isPrime[j] = false;
            }
          }
        }
      }
    }
    int ans = 0;
    for (int i = 2; i <= n; i++) {
      if (isPrime[i]) {
        ans++;
      }
    }
    cout << ans << endl;
    delete[] isPrime;
  }

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

#include <iostream>
using namespace std;

int main() {
  bool a[1000000];

  a[0] = a[1] = false;
  for (int i = 2; i < 1000000; i++) {
    a[i] = true;
  }

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

  int n[1000000];

  int tmp = 0;
  for (int i = 0; i < 1000000; i++) {
    if (a[i]) {
      tmp++;
    }
    n[i] = tmp;
  }

  while (true) {
    int m;
    cin >> m;

    if (cin.eof()) {
      break;
    }

    cout << n[m] << endl;
  }

  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  bool a[1000000];

  a[0] = a[1] = false;
  for (int i = 2; i < 1000000; i++) {
    a[i] = true;
  }

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

  int n[1000000];

  int tmp = 0;
  for (int i = 0; i < 1000000; i++) {
    if (a[i]) {
      tmp++;
    }
    n[i] = tmp;
  }

  while (true) {
    int m;
    cin >> m;

    if (cin.eof()) {
      break;
    }

    cout << n[m] << endl;
  }

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

#include <iostream>

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:

#define _CRT_SECURE_NO_WARNINGS
#define M_PI 3.14159265358979
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int yakusuu(int i) {
  int counter = 0;
  for (int j = 1; j <= i; j++) {
    if (i % j == 0)
      counter++;
  }
  return counter;
}

int main() {
  int n;
  while (scanf("%d", &n) != -1) {
    int counter1 = 0;
    for (int i = 2; i <= n; i++) {
      if (yakusuu(i) == 2)
        counter1++;
    }
    cout << counter1 << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int main() {
  int i, j, n, num;
  bool prime[1000000];
  bool flag = true;
  for (i = 0; i < 999999; i++)
    prime[i] = true;
  while (scanf("%d", &n) != EOF) {
    num = 0;
    for (i = 2; i < n + 1; i++) {
      for (j = i * 2; j < n + 1; j = j + i)
        prime[j] = false;
    }
    for (i = 2; i < n + 1; i++) {
      if (prime[i] == true)
        num++;
    }
    printf("%d\\n", num);
  }
}

Error: 0

Fixed submission:

#include <stdio.h>
int main() {
  int i, j, n, num = 0;
  bool prime[100000];
  bool flag = true;
  for (i = 0; i < 99999; i++)
    prime[i] = true;
  while (scanf("%d", &n) != EOF) {
    num = 0;
    for (i = 2; i < n + 1; i++) {
      for (j = i * 2; j < n + 1; j = j + i)
        prime[j] = false;
    }
    for (i = 2; i < n + 1; i++) {
      if (prime[i] == true)
        num++;
    }
    printf("%d\\n", num);
  }
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int a, o = 0;
  bool n[1000000];
  for (int i = 2; i <= 999999; i++) {
    n[i] = true;
  }
  n[0] = n[1] = false;
  for (int j = 2; j * j <= 999999; j++) {
    if (n[j]) {
      for (int k = j * 2; k <= 999999; k += j) {
        n[k] = false;
      }
    }
  }
  while (cin >> a) {
    for (int k = 2; k <= a; k++)
      if (n[k])
        o++;
    cout << o << endl;
    o = 0;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int a, j, o = 0;
  bool n[100000];
  for (int i = 2; i <= 99999; i++) {
    n[i] = true;
  }
  n[0] = n[1] = false;
  for (j = 2; j <= 99999; j++) {
    if (n[j]) {
      for (int k = j * 2; k <= 9999; k += j) {
        n[k] = false;
      }
    }
  }
  while (cin >> a) {
    for (int k = 2; k <= a; k++)
      if (n[k])
        o++;
    cout << o << endl;
    o = 0;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); 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 PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;

int main() {
  vector<int> primes;
  primes.PB(2);
  FOR(i, 3, 1000000) {
    bool flg = true;
    for (int j = 0; primes[j] * primes[j] <= i; j++) {
      if (i % primes[j] == 0) {
        flg = false;
        break;
      }
    }
    if (flg)
      primes.PB(i);
    i++;
  }

  int n;
  while (true) {
    cin >> n;
    if (cin.eof())
      break;
    int res = upper_bound(ALL(primes), n) - primes.begin();
    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  while (true) {
    int n;
    cin >> n;
    if (cin.eof())
      break;

    int res = 0;
    bool flg = true;
    for (int i = 2; i <= n; i++) {
      flg = true;
      for (int j = 2; j * j <= i; j++) {
        if (i % j == 0) {
          flg = false;
          break;
        }
      }
      if (flg)
        res++;
    }
    cout << res << endl;
  }

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

#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

int arr[1000005] = {0};

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

int main() {
  int n;
  while (cin >> n) {
    if (n == 1)
      cout << "0" << endl;
    else if (n == 2)
      cout << "1" << endl;
    else if (arr[n] != 0)
      cout << arr[n] << endl;
    else {
      int count = 1;
      for (int i = 3; i <= n; i++) {
        if (isPrime(i)) {
          count++;
          arr[i] = count;
        } else
          arr[i] = count;
      }
      cout << count << endl;
    }
  }
  // while(1);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

int arr[1000005] = {0};

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

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

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

bool nonprime[1000000];

int main() {
  long i, j, n;
  for (i = 2; i <= 999999; i++) {
    for (j = 2; i * j <= 999999; j++)
      nonprime[i * j] = 1;
  }
  while (scanf("%ld", &n) != EOF) {
    j = 0; // j?????????????????¨?????????????????????
    for (i = 2; i <= n; i++)
      if (!nonprime[i])
        j++;
    cout << j << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool nonprime[1000000];

int main() {
  long i, j, n;
  for (i = 2; i <= 999999; i++) {
    for (j = 2; i * j <= 999999; j++)
      nonprime[i * j] = 1;
  }
  while (1) {
    cin >> n;
    j = 0; // j?????????????????¨?????????????????????
    for (i = 2; i <= n; i++)
      if (!nonprime[i])
        j++;
    cout << j << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  int n;
  bool p[1000000];
  while (cin >> n) {
    int n_sq = (int)sqrt(n);
    for (int i = 0; i < n; i++) {
      p[i] = true;
    }
    p[0] = false;

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

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

    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, i;
  while (scanf("%d", &n) != EOF) {
    int cnt = 0;
    for (; n > 1; n--) {
      for (i = 2; i <= sqrt(n) + 1; i++) {
        if (n % i == 0)
          break;
      }
      if (i > sqrt(n))
        cnt++;
    }
    cout << cnt << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
using namespace std;

//??¨??????????????????????????§?´???°??¨?????????
//?´???°??¨??¨?´???????????????£?????????????????§??¨????????????

const int MAX_V = 999999;
int prime[MAX_V + 1]; //?´???°??¨
int sum[MAX_V + 1];   //?´???°?????°???????¨?

int main() {
  int i, k, v;
  prime[0] = prime[1] = 0;
  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;
      }
    }
  }

  sum[0] = 0;
  for (i = 0; i < MAX_V + 1; i++) {
    sum[i + 1] = sum[i] + prime[i + 1];
  }

  int input;
  while (cin >> input) {
    cout << sum[input] << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;

//??¨??????????????????????????§?´???°??¨?????????
//?´???°??¨??¨?´???????????????£?????????????????§??¨????????????

const int MAX_V = 10000000;
int prime[MAX_V + 1]; //?´???°??¨
int sum[MAX_V + 1];   //?´???°?????°???????¨?

int main() {
  int i, k, v;
  prime[0] = prime[1] = 0;
  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;
      }
    }
  }

  sum[0] = 0;
  for (i = 0; i < MAX_V + 1; i++) {
    sum[i + 1] = sum[i] + prime[i + 1];
  }

  int input;
  while (cin >> input) {
    cout << sum[input] << endl;
  }

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

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

const int C = (int)1e7 + 10;

bool isprime[C];
int prime[C];

void init() {
  fill(isprime, isprime + C, true);
  isprime[0] = isprime[1] = false;
  for (auto i = 2; i < C; i++) {
    if (isprime[i]) {
      for (auto j = 2; i * j < C; j++) {
        isprime[i * j] = false;
      }
    }
  }
  prime[0] = 0;
  for (auto i = 1; i < C; i++) {
    prime[i] = ((isprime[i]) ? prime[i - 1] + 1 : prime[i - 1]);
  }
}

int main() {
  init();
  int n;
  while (cin >> n) {
    cout << prime[n] << endl;
  }
}

Error: MLE

Fixed submission:

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

const int C = (int)1e8;

bool isprime[C];
int prime[C];

void init() {
  fill(isprime, isprime + C, true);
  isprime[0] = isprime[1] = false;
  for (auto i = 2; i < C; i++) {
    if (isprime[i]) {
      for (auto j = 2; i * j < C; j++) {
        isprime[i * j] = false;
      }
    }
  }
  prime[0] = 0;
  for (auto i = 1; i < C; i++) {
    prime[i] = ((isprime[i]) ? prime[i - 1] + 1 : prime[i - 1]);
  }
}

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, cnt = 0, ans, sosu[1000003] = {0};

  for (int i = 2; i < 1000002; i++) {
    bool flag = true;
    for (int j = 2; j <= sqrt(i); j++) {
      if (i % j == 0) {
        flag = false;
        break; // 素数でない時点で打ち切り
      }
    }
    /*if(flag){
    cnt++;
    sosu[i]=cnt;
    }*/
    if (flag)
      cnt++;
    sosu[i] = cnt;
  }
  // for(int i=0;i<31;i++)cout<<sosu[i]<<" ";
  while (cin >> n) {
    cout << sosu[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, cnt = 0, ans, sosu[1000003] = {0};

  for (int i = 2; i < 1000002; i++) {
    bool flag = true;
    for (int j = 2; j <= sqrt(i); j++) {
      if (i % j == 0)
        flag = false;
    }
    /*if(flag){
    cnt++;
    sosu[i]=cnt;
    }*/
    sosu[i] = (flag) ? cnt++ : cnt;
  }
  // for(int i=0;i<30;i++)cout<<sosu[i]<<" ";
  while (cin >> n) {
    cout << sosu[n] << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <math.h>
#include <vector>
#define REP(i, n) for (int(i) = 0; i <= n; ++i)
#define N 1000000
using namespace std;

vector<int> prime;

void Eratosthenes() {
  bool f[N];
  for (int i = 0; i < N; i++) {
    f[i] = true;
  }
  f[0] = f[1] = false;

  for (int i = 2; i < sqrt(N); i++) {
    if (f[i]) {
      for (int j = 0; i * (j + 2) < N; j++) {
        f[i * (j + 2)] = false;
      }
    }
  }

  for (int i = 2; i < N; i++) {
    if (f[i]) {
      prime.push_back(i);
    }
  }
}

int main() {
  int n;
  Eratosthenes();
  while (cin >> n) {
    int ret = 0;
    for (int j = 0; j < prime.size(); j++) {
      if (prime[j] > n)
        break;
      ret++;
    }
    cout << ret << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define REP(i, n) for (int(i) = 0; i <= n; ++i)
using namespace std;

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

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

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

const int MAX = 1000000;
int N;
vector<int> ip(1000001, 1);
int ans = 0;

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

  while (cin >> N) {

    for (int i = 0; i <= N; i++) {
      if (ip[i])
        ans++;
    }

    cout << ans << endl;
    ans = 0;
  }
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

int N, ans = 1;

int IsPrime(int p) {
  double n = sqrt(p);
  for (int i = 3; i <= n; i++) {
    if (p % i == 0)
      return 0;
  }
  return 1;
}

int main() {
  while (cin >> N) {
    // double n = sqrt(N);
    for (int i = 3; i <= N; i = i + 2) {
      ans += IsPrime(i);
    }
    cout << ans << endl;
    ans = 1;
  }
  return 0;
}