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

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

int prime[1000000];
void initTbl() {
  // sieve of Eratosthenes
  //  0 : prime
  //  1 : not prime
  memset(prime, 0, sizeof(prime));
  prime[0] = 1;
  prime[1] = 1;

  int limit = 1000000;
  for (int i = 4; i < limit; i += 2) {
    prime[i] = 1;
  }

  for (int j = 3; j * j <= limit; j += 2) {
    for (int k = j; j * k <= limit; k += 2) {
      prime[j * k] = 1;
    }
  }
}

// 0009
int main() {
  int n = 10;
  initTbl();

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

    for (; n > 0; n--) {
      if (!prime[n])
        ++cnt;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int isPrime(int n) {
  // sieve of Eratosthenes
  if (n == 2)
    return 1;
  if (n % 2 == 0 || n < 2) {
    return 0;
  }

  for (int i = 3; i * i <= n; i += 2) {
    if (n % i == 0) {
      return 0;
    }
  }
  return 1;
}

// 0009
int main() {
  int n;

  while (scanf("%d", &n)) {

    int cnt = 0;
    for (; n > 1; n--) {
      if (isPrime(n))
        ++cnt;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int d[1000000] = {}, p[1000000] = {};
void calc() {
  for (int i = 2; i < 1000000; i++) {
    if (d[i] == 0) {
      for (int j = 2; j < 1000000; j++) {
        if (i * j > 1000000)
          break;
        d[i * j] = 1;
      }
    }
  }
  for (int i = 2; i < 1000000; i++) {
    if (d[i] == 0)
      p[i] = p[i - 1] + 1;
    else
      p[i] = p[i - 1];
  }
}
int main() {
  calc();
  int n;
  while (cin >> n, n) {
    if (cin.eof())
      break;
    cout << p[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int d[1000000] = {}, p[1000000] = {};
void calc() {
  for (int i = 2; i < 1000000; i++) {
    if (d[i] == 0) {
      for (int j = 2; j < 1000000; j++) {
        if (i * j > 1000000)
          break;
        d[i * j] = 1;
      }
    }
  }
  for (int i = 2; i < 1000000; i++) {
    if (d[i] == 0)
      p[i] = p[i - 1] + 1;
    else
      p[i] = p[i - 1];
  }
}
int main() {
  calc();
  int n;
  while (cin >> n, n) {
    cout << p[n] << endl;
    ;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n = 1000000;
  vector<int> v;
  v.pb(2);
  for (int i = 3; i <= n; i += 2) {
    bool add = true;
    rep(j, 0, v.size()) {
      if (v[j] * v[j] > i)
        break;
      if (i % v[j] == 0) {
        add = false;
        break;
      }
    }
    if (add)
      v.pb(i);
  }

  int m;
  while (cin >> m) {
    cout << upper_bound(v.begin(), v.end(), m) - v.begin() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

int main() {
  int n;
  while (cin >> n) {
    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: Memory Limit Exceeded

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>

static int const MAX(1000000);

class Mod {
  const int n;

public:
  Mod(const int _n) : n(_n) {}
  bool operator()(const int _src) { return !(_src % n); }
};

const std::vector<int> Eratosthenes(std::set<int> &src) {
  std::vector<int> result;
  int cnt(0);
  while (!src.empty()) {
    int n(*src.begin());
    result.push_back(n);
    Mod mod(n);
    // std::set<int>::iterator it= std::remove_if( src.begin(), src.end(), mod
    // );
    //  n>(MAX/2) ? std::remove( src.begin(), src.end(), n ) :
    for (int i(n); i < MAX; i += n) {
      std::set<int>::iterator it = src.find(i);
      if (it != src.end())
        src.erase(it);
    }
    // src.erase(it,src.end());
  }
  return result;
}

int main(int, char **) {
  std::set<int> vec;
  int max_2(MAX - MAX / 2 - MAX / 3 - MAX / 5 + MAX / 6 + MAX / 10 + MAX / 15 -
            MAX / 30 + 3);
  //	vec.reserve(max_2);
  for (int i(3); i <= MAX; i += 2) {
    if (i != 3 && i % 3 == 0)
      continue;
    if (i != 5 && i % 5 == 0)
      continue;
    vec.insert(i);
  }
  std::vector<int> primes;
  primes.reserve(10000);
  primes = Eratosthenes(vec);
  int n;
  while (std::cin >> n) {
    std::vector<int>::const_iterator const it =
        std::upper_bound(primes.begin(), primes.end(), n);
    int off = it - primes.begin();
    if (n >= 2) {
      ++off;
    }
    std::cout << off << std::endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>

class Mod {
  const int n;

public:
  Mod(const int _n) : n(_n) {}
  bool operator()(const int _src) { return !(_src % n); }
};

const std::vector<int> Eratosthenes(std::vector<int> &src) {
  std::vector<int> result;
  int cnt(0);
  while (!src.empty()) {
    int n(src[0]);
    result.push_back(n);
    Mod mod(n);
    std::vector<int>::iterator it = std::remove_if(src.begin(), src.end(), mod);
    src.erase(it, src.end());
    if (cnt < 10 || cnt % 10 == 0) {
      src.swap(src);
    }
  }
  return result;
}
static int const MAX(1000000);

int main(int, char **) {
  std::vector<int> vec;
  int max_2(MAX - MAX / 2 - MAX / 3 - MAX / 5 + MAX / 6 + MAX / 10 + MAX / 15 -
            MAX / 30 + 3);
  vec.reserve(max_2);
  for (int i(3); i <= MAX; i + 2) {
    if (i != 3 && i % 3 == 0)
      continue;
    if (i != 5 && i % 5 == 0)
      continue;
    vec.push_back(i);
  }
  std::vector<int> primes;
  primes.reserve(10000);
  primes = Eratosthenes(vec);
  int n;
  while (std::cin >> n) {
    std::vector<int>::const_iterator const it =
        std::upper_bound(primes.begin(), primes.end(), n);
    int off = it - primes.begin();
    if (n >= 2) {
      ++off;
    }
    std::cout << off << std::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 = 100000;
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: Runtime Error

#include <iostream>

int main(void) {
  int input;
  while (std::cin >> input) {
    bool *table = new bool[input + 1]();
    for (int i = 0; i <= input; ++i) {
      if (i < 2) {
        table[i] = true;
        continue;
      }
      if (table[i] == false) {
        for (int j = 2; i * j <= input; ++j) {
          table[i * j] = true;
        }
      }
    }
    int count = 0;
    for (int i = 0; i <= input; ++i) {
      if (table[i] == false) {
        ++count;
      }
    }
    std::cout << count << std::endl;
    delete[] table;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

bool Check(int);

int main(void) {
  int input;
  int count;
  while (std::cin >> input) {
    count = 0;
    for (int j = 2; j <= input; ++j) {
      if (Check(j)) {
        ++count;
      }
    }
    std::cout << count << std::endl;
  }
  return 0;
}
bool Check(int value) {
  bool result = true;
  for (int i = value / 2; i >= 2; --i) {
    if (value % i == 0) {
      result = false;
      break;
    }
  }
  return result;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n, cnt;

  while (cin >> n) {
    bool prime[1000000] = {false};
    prime[1] = true; //1???????????\\

    // ??¨??????????????????????????????
    for (int j = 2; j * j <= n; j++) {
      if (prime[j] == false) {
        for (int k = j; k * j <= n; k++) {
          prime[j * k] = true;
        }
      }
    }
    cnt = 0;
    for (int i = 1; i <= n; i++) {
      if (prime[i] == false)
        cnt++;
    }

    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n;

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

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

      if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0)
        continue;

      int yakusuu = 0;

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

      if (yakusuu == 2)
        cnt++;
    }

    cout << cnt + 3 - 1 << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

// AOJ 009

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

bool isPrime[MAX_NUMBER];

void SetFalseToMultiples(int prime) {
  for (int i = prime * 2; i < MAX_NUMBER; i += prime) {
    isPrime[i] = false;
  }
  return;
}

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

int main() {
  for (int i = 0; i < MAX_NUMBER; i++) {
    isPrime[i] = true;
  }
  isPrime[1] = false;

  for (int i = 1; i < MAX_NUMBER; i++) {
    if (isPrime[i]) {
      SetFalseToMultiples(i);
    }
  }
  int n;
  while (cin >> n) {
    cout << CountPrimes(n) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

// AOJ 009

#include <iostream>
#define MAX_NUMBER 50 // 1000000
using namespace std;

bool isPrime[MAX_NUMBER];

void SetFalseToMultiples(int prime) {
  for (int i = prime * 2; i < MAX_NUMBER; i += prime) {
    isPrime[i] = false;
  }
  return;
}

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

int main() {
  for (int i = 0; i < MAX_NUMBER; i++) {
    isPrime[i] = true;
  }
  isPrime[1] = false;

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

#include <cmath>
#include <iostream>

typedef long long LL;
using namespace std;

const int X = 1000001;
bool Find[X];

void init() {
  for (int i = 0; i < X; i++) {
    Find[i] = false;
  }
  for (int i = 2; i <= sqrt(X); i++) {
    if (Find[i])
      continue;
    for (int j = i + 1; j < X; j++) {
      if (Find[j])
        continue;
      if (j % i == 0)
        Find[j] = true;
    }
  }
}

int main() {
  init();
  int n;
  while (cin >> n) {
    int answer = 0;
    for (int i = 2; i < n + 1; i++) {
      if (!Find[i])
        answer++;
    }
    cout << answer << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

typedef long long LL;
using namespace std;

const int X = 1000001;
bool Find[X];

void init() {
  for (int i = 0; i < X; i++) {
    Find[i] = false;
  }
  for (int i = 2; i <= sqrt(X); i++) {
    if (Find[i])
      continue;
    for (int j = i + 1; j < X; j++) {
      if (Find[j])
        continue;
      if (j % i == 0)
        Find[j] = true;
    }
  }
}

int main() {
  init();
  int n;
  while (cin >> n) {
    int answer = 0;
    for (int i = 2; i < n + 1; i++) {
      cout << Find[i] << endl;
      if (!Find[i])
        answer++;
    }
    cout << answer << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define PI 4 * atan(1);

vector<bool> a;
void func(int n) {

  for (int i = 0; i <= n; i++) {
    a.push_back(true);
  }
  for (int i = 2; i * i <= n; i++) {
    if (a[i]) {
      for (int j = 2; i * j <= n; j++) {
        a[i * j] = 0;
      }
    }
  }
  int cnt = 0;
  for (int i = 2; i <= n; i++) {
    if (a[i]) {
      cnt++;
    }
  }
  cout << cnt << endl;
}

int main() {
  int n;
  while (cin >> n) {
    func(n);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define PI 4 * atan(1);

vector<int> a;
void func(int n) {

  for (int i = 0; i <= n; i++) {
    a.push_back(1);
  }
  for (int i = 2; i * i <= n; i++) {
    if (a[i]) {
      for (int j = 2; i * j <= n; j++) {
        a[i * j] = 0;
      }
    }
  }
  int cnt = 0;
  for (int i = 2; i <= n; i++) {
    if (a[i]) {
      cnt++;
    }
  }
  cout << cnt << endl;
}

int main() {
  int n;
  while (cin >> n) {
    func(n);
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

using namespace std;

int main() {
  long NUM = 1000000;
  long n;
  long counter = 0;
  bool primeNum[NUM + 10];

  long i, ii, j, len = NUM + 10;

  primeNum[0] = primeNum[1] = false;
  for (i = 2; i < len; i++)
    primeNum[i] = true;

  ii = 4;
  for (i = 2; ii < NUM;) {
    if (primeNum[i]) {
      for (j = ii; j < NUM; j += i) {
        primeNum[j] = false;
      }
    }

    i++;
    ii = i * i;
  }

  while (scanf("%ld", &n) != EOF) {
    counter = 0;

    for (i = 0; i <= n; i++) {
      if (primeNum[i])
        counter++;
    }

    printf("%ld\\n", counter);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

using namespace std;

int main() {
  long NUM = 1000000;
  long n = NUM;
  long m = 2;
  bool flag;
  long counter = 0;
  bool primeNum[NUM + 10];

  long i, len = NUM + 10;

  for (i = 0; i < len; i++)
    primeNum[i] = false;

  while (m <= n) {
    flag = true;
    len = m / 2;

    for (i = 2; i <= len; i++) {
      if (m % i == 0) {
        flag = false;

        break;
      }
    }

    if (flag)
      primeNum[m] = true;

    m++;
  }

  while (scanf("%ld", &n) != EOF) {
    counter = 0;

    for (i = 0; i <= n; i++) {
      if (primeNum[i])
        counter++;
    }

    printf("%ld\\n", counter);
  }

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

#include <bits/stdc++.h>

using namespace std;

static const long MAX = 1000000L;

int main() {
  vector<long> prime;
  vector<long> dp;
  bool isPrime;
  long n, m;

  long i, len = sqrt(MAX) + 1;
  long j, lenJ;
  long k;

  prime.push_back(2);
  dp.push_back(0);
  dp.push_back(0);
  dp.push_back(1);

  for (i = 3; i <= len; i += 2) {
    isPrime = true;
    lenJ = prime.size();
    k = sqrt(i) + 1;
    for (j = 0; j < lenJ; j++) {
      if (k <= prime[j])
        break;

      if (i % prime[j] == 0) {
        isPrime = false;

        break;
      }
    }

    if (isPrime)
      prime.push_back(i);
  }

  lenJ = prime.size();

  while (cin >> n) {
    if (n < dp.size()) {
      cout << dp[n] << endl;

      continue;
    }

    m = dp.size();
    for (i = m; i <= n; i += 2) {
      isPrime = true;

      k = sqrt(i) + 1;
      for (j = 0; j < lenJ; j++) {
        if (k <= prime[j])
          break;

        if (i % prime[j] == 0) {
          isPrime = false;

          break;
        }
      }

      if (isPrime) {
        dp.push_back(dp.back() + 1);
        dp.push_back(dp.back());
      } else {
        dp.push_back(dp.back());
        dp.push_back(dp.back());
      }
    }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

static const long MAX = 1000000L;

int main() {
  vector<long> prime;
  bool isPrime;
  long ans = 0;
  long n;

  long i, len = sqrt(MAX) + 1;
  long j, lenJ;
  long k;

  prime.push_back(2);

  for (i = 3; i <= len; i += 2) {
    isPrime = true;
    lenJ = prime.size();
    k = sqrt(i) + 1;
    for (j = 0; j < lenJ; j++) {
      if (k <= prime[j])
        break;

      if (i % prime[j] == 0) {
        isPrime = false;

        break;
      }
    }

    if (isPrime)
      prime.push_back(i);
  }

  lenJ = prime.size();

  while (cin >> n) {
    ans = 1;

    if (n < 2)
      ans = 0;

    for (i = 3; i <= n; i += 2) {
      isPrime = true;

      k = sqrt(i) + 1;
      for (j = 0; j < lenJ; j++) {
        if (k <= prime[j])
          break;

        if (i % prime[j] == 0) {
          isPrime = false;

          break;
        }
      }

      if (isPrime)
        ans++;
    }

    cout << ans << endl;
  }

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

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

int main() {
  int n;
  while (cin >> n) {
    int cnt = 0;
    vector<bool> v(n + 1, true);
    for (int i = 2; i <= sqrt(n); i++) {
      if (v[i]) {
        for (int j = 2; i * j <= n; j++) {
          v[i * j] = false;
        }
      }
    }
    for (int i = 2; i <= n; i++) {
      if (v[i] == true)
        cnt++;
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  while (cin >> n) {
    int cnt = 0;
    vector<bool> v(n + 1, true);
    for (int i = 2; i <= sqrt(n); i++) {
      if (v[i]) {
        for (int j = i + 1; j <= n; j++) {
          if (j % i == 0)
            v[j] = false;
        }
      }
    }
    for (int i = 2; i <= n; i++) {
      if (v[i] == true)
        cnt++;
    }
    cout << cnt << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main() {
  int a, t[1000000], u[1000000];
  for (int i = 0; i < 1000000; i++) {
    t[i] = 1;
  }
  t[0] = t[1] = u[0] = u[1] = 0;
  for (int i = 2; i <= 1000000; i++) {
    if (t[i] == 1) {
      for (int j = i + i; j < 1000000; j += i)
        t[j] = 0;
      u[i] = u[i - 1] + 1;
    } else {
      u[i] = u[i - 1];
    }
  }
  while (cin >> a) {
    cout << u[a] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int isprime(int x) {
  int a[x], b = 0;
  for (int i = 0; i < x; i++) {
    a[i] = i + 1;
  }
  for (int i = 0; i < x; i++) {
    if (a[i] == 2 || a[i] == 3)
      b++;
    if (a[i] < 5 || a[i] % 2 == 0 || a[i] % 3 == 0)
      goto exit;
    else {
      int j = 5;
      while (j < sqrt(x)) {
        if (a[i] % j == 0)
          goto exit;
        j = j + 2;
      }
      b++;
    }
  exit:;
  }
  return b;
}
int main() {
  int n;
  while (cin >> n) {
    cout << isprime(n) << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#define max_n 999999
#define INF 1000000
int prime[max_n];
bool is_prime[max_n + 1];
int main() {
  for (int i = 0; i < max_n; i++) {
    prime[i] = INF;
    is_prime[i] = true;
  }
  int p = 0;
  is_prime[1] = false;
  is_prime[0] = 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;
    }
  }
  int n;
  while (scanf("%d", &n) != EOF) {
    // printf("%d\\n",n);
    int ans = 0;
    for (int i = 0; i < max_n; i++) {
      if (prime[i] > n) {
        ans = i;
        break;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#define max_n 999999
int prime[max_n];
bool is_prime[max_n + 1];
int main() {
  for (int i = 0; i < max_n; i++) {
    is_prime[i] = true;
  }
  int p = 0;
  is_prime[1] = false;
  is_prime[0] = 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;
    }
  }

  while (true) {
    int n;
    scanf("%d", &n);
    int ans = 0;
    for (int i = 0; i < max_n; i++) {
      if (prime[i] > n) {
        ans = i;
        break;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded


//  test.cpp

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <string>

using namespace std;

int main() {
  long int j, s[1000000] = {0}, d = 1, i, n, k;

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

  for (i = 2; i < 1000000; i++) {
    if (s[i] == 0) {

      for (j = 2; j * j <= i; j += 2) {
        if (i % j == 0)
          break;
        if (j == 2)
          j = 1;
      }

      if (j * j > i) {
        d++;
        if (i < 1000) {
          for (k = 2; k * i < 1000000; k++) {
            s[k * i] = -1;
          }
        }
      }
    }
    s[i] = d;
  }

  while (EOF != scanf("%ld", &n)) {
    printf("%ld\\n", s[n] - 1);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <string>

using namespace std;

int main() {
  int j, s[1000000];
  long i, n, d = 0;

  for (i = 3; i < 1000000; i++) {
    for (j = 2; j * j < i; j += 2) {
      if (j % i == 0) {
        d--;
        break;
      }
      if (j == 2)
        j = 1;
    }
    s[i] = ++d;
  }

  while (EOF != scanf("%d", &n)) {
    printf("%d\\n", &d);
  }

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

#include <iostream>
using namespace std;
int s[80000], m[1000000], i, j, n = 0;
int main() {
  for (i = 2; i < 1000000; i++) {
    if (m[i] == 0) {
      s[n++] = i;
      for (j = i; j < 1000000; j += i)
        m[j] = 1;
    }
  }
  while (cin >> i) {
    for (j = 0; s[j] <= i && j < n; j++)
      ;
    cout << j << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;
int s[80000], m[10000000], i, j, n = 0;
int main() {
  for (i = 2; i < 1000000; i++) {
    if (m[i] == 0) {
      s[n++] = i;
      for (j = i; j < 10000000; j += i)
        m[j] = 1;
    }
  }
  while (cin >> i) {
    for (j = 0; s[j] <= i && j < n; j++)
      ;
    cout << j << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    int count = 0;
    bool *flag = new bool[n + 1]();
    for (int i = 2; i <= n; ++i) {
      if (flag[i]) {
        continue;
      }
      for (int j = i * 2; j <= n; j += i) {
        flag[j] = true;
      }
    }
    flag[0] = true;
    flag[1] = true;
    for (int i = 0; i <= n; ++i) {
      if (!flag[i]) {
        ++count;
      }
    }
    cout << count << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

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

int main() {
  int n;
  while (cin >> n) {
    int count = 0;
    for (int i = 0; i <= n; ++i) {
      if (Prime(i)) {
        ++count;
      }
    }
    cout << count << endl;
  }
  return 0;
}