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

// AOJ 0009 Prime Number
// 2016.6.25
// Algorithm:Prime
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int maxn = 1000006;
bool prime[maxn];
int n;
int cnt[maxn];

int main() {
  for (int i = 1; i < maxn; i++)
    prime[i] = true;
  prime[1] = false;
  for (int i = 2; i < maxn; i++)
    if (prime[i])
      for (int j = i * 2; j < maxn; j += i)
        prime[j] = false;
  for (int i = 1; i < maxn; i++)
    cnt[i] = cnt[i - 1] + prime[i];
  while (cin >> n) {
    cout << cnt[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

// int is_prime(int N){
//
//	int root = sqrt(N);
//	int i;
//	for(i = 3; i<= root; i+=2){
//		if(N%i==0)
//			return 0;
//	}
//	if(i>root)
//		return 1;
// }

bool isPrime(int num) {
  if (num <= 3) {
    return num > 1;
  } else if (num % 2 == 0 || num % 3 == 0) {
    return false;
  } else {
    int root = sqrt(num);
    for (int i = 5; i <= root; i += 6) {
      if (num % i == 0 || num % (i + 2) == 0) {
        return false;
      }
    }
    return true;
  }
}

int main() {

  int A;
  int count = 1;

  while (scanf("%d", &A) != EOF) {
    count = 1;

    if (A < 2)
      count = 0;
    else if (A == 2)
      count = 1;
    else {
      for (int i = 3; i <= A; i += 2) {
        if (isPrime(i) == true)
          count++;
      }
    }

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

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

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  int n;
  const int max = 1000000;
  vector<int> result;
  bool nums[max];
  for (int i = 2; i < max; i++)
    nums[i] = true;
  for (int i = 2; i < max; i++) {
    if (!nums[i])
      continue;
    else if (i * i > max)
      break;
    for (int j = i + 1; j < max; j++) {
      if (!nums[j])
        continue;
      else if (j % i == 0)
        nums[j] = false;
    }
  }

  while (cin >> n) {
    int piyo = 0;
    for (int i = 2; i <= n; i++)
      if (nums[i])
        piyo++;
    result.push_back(piyo);
  }

  for (int i = 0; i < result.size(); i++)
    cout << result[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  int n;
  vector<int> result;
  while (cin >> n) {
    vector<int> nums;
    nums.reserve(n + 1);
    for (int i = 0; i <= n; i++)
      nums.push_back(i);

    for (int i = 2; i <= n; i++) {
      if (nums[i] == 0)
        continue;
      int hoge = nums[i];
      for (int j = i + 1; j <= n; j++) {
        if (nums[j] == 0)
          continue;
        else if (nums[j] % hoge == 0)
          nums[j] = 0;
      }
    }

    int piyo = 0;
    for (int i = 2; i <= n; i++)
      if (nums[i])
        piyo++;
    result.push_back(piyo);
  }

  for (int i = 0; i < result.size(); i++)
    cout << result[i] << endl;
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn = 999999 + 5;
ll prime[maxn];
int main() {
  ll cnt = 0;
  for (ll i = 2; i < maxn; ++i) {
    if (!prime[i]) {
      prime[i] = ++cnt;
      for (ll j = i * i; j < maxn; j += i) {
        prime[j] = 1;
      }
    } else {
      prime[i] = prime[i - 1];
    }
  }
  ll n;
  while (~scanf("%lld", &n)) {
    printf("%lld\\n", prime[n]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main() {
  int n, i, m, sum;
  while (1) {
    sum = 0;
    scanf("%d", &n);
    for (m = 2; m <= n; m++) {
      for (i = 2; i <= m; i++) {
        if (m % i == 0 && i < m) {
          break;
        }
        if (i == m) {
          sum = sum + 1;
        }
      }
    }
    printf("%d\\n", sum);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(i, n) REP(i, 0, n)
#define mset(a, n) memset(a, n, sizeof(a))

using namespace std;

const int MAX_N = 1000000;
bool prime[MAX_N];
void eratos() {
  rep(i, MAX_N) prime[i] = true;
  prime[0] = false;
  prime[1] = false;
  REP(i, 2, sqrt(MAX_N)) {
    if (prime[i]) {
      for (int j = 0; i * (j + 2) < MAX_N; ++j) {
        prime[i * (j + 2)] = false;
      }
    }
  }
}

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

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(i, n) REP(i, 0, n)
#define mset(a, n) memset(a, n, sizeof(a))

using namespace std;

const int MAX_N = 100000000;
bool prime[MAX_N];
void eratos() {
  rep(i, MAX_N) prime[i] = true;
  prime[0] = false;
  prime[1] = false;
  REP(i, 2, sqrt(MAX_N)) {
    if (prime[i]) {
      for (int j = 0; i * (j + 2) < MAX_N; ++j) {
        prime[i * (j + 2)] = false;
      }
    }
  }
}

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define rep(x, to) for (x = 0; x < to; x++)
#define rep2(x, from, to) for (x = from; x < to; x++)
#define num 1000000

using namespace std;

int main(void) {

  int innum, i;

  char Find[num]; // 探索リスト
  for (int i = 2; i <= num; i++) {
    Find[i] = '0';
    if (i % 2 == 0)
      Find[i] = '1';
  }
  for (int i = 2; i * i < num; i++) {
    if (Find[i] > '0')
      continue; // すでに除いた数は飛ばす
    for (int j = i * 2; j < num; j += i) {
      if (Find[j] == '0')
        Find[j] = '1';
    }
  }
  Find[2] = '0';
  while (cin >> innum) {
    int cnt = 0;
    rep(i, innum + 1) if (Find[i] == '0') {
      //	cout << i << " ";
      cnt++;
    }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define rep(x, to) for (x = 0; x < to; x++)
#define rep2(x, from, to) for (x = from; x < to; x++)

using namespace std;

int main(void) {

  int num, i, cnt;

  while (cin >> num) {
    cnt = 1;
    for (i = 3; i <= num; i += 2) {
      bool swa = true;
      for (int j = 3; j * j <= i; j += 2) {
        if (i % j == 0) {
          swa = false;
          continue;
        }
      }
      if (swa == true) {
        cnt++;
        //				cout << i << " ";
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
vector<int> wow(1000001);

bool prime(int m) {
  if (m < 2)
    return false;
  if (m == 2)
    return true;
  if (m % 2 == 0)
    return false;

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

int main() {

  for (int i = 0; i < 1000001; ++i) {
    if (prime(i)) {
      wow[i] = 1;
    } else {
      wow[i] = 2;
    }
  }

  int n;

  while (cin >> n) {
    int a = 0;
    for (int i = 1; i <= n; ++i) {
      if (wow[i] == 1) {
        ++a;
      }
    }
    cout << a << '\\n';
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
vector<int> wow(100001);

bool prime(int m) {
  if (m < 2)
    return false;
  if (m == 2)
    return true;
  if (m % 2 == 0)
    return false;

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

int main() {

  for (int i = 0; i < 100001; ++i) {
    if (prime(i)) {
      wow[i] = 1;
    } else {
      wow[i] = 2;
    }
  }

  int n;

  while (cin >> n) {
    int a = 0;
    for (int i = 1; i <= n; ++i) {
      if (wow[i] == 1) {
        ++a;
      }
    }
    cout << a << '\\n';
  }
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int N;
  bool is_prime[1000000];
  is_prime[0] = 0;
  is_prime[1] = 0;
  for (int i = 2; i < 1000000; i++) {
    is_prime[i] = 1;
  }

  for (long s = 2; s < 1000000; s++) {
    if (is_prime[s] == 1) {
      for (long long i = s * s; i < 1000000; i += s) {
        is_prime[i] = 0;
      }
    }
  }

  while (cin >> N) {

    int ans = 0;
    for (int i = 2; i < N + 1; i++) {
      ans += is_prime[i];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int N;
  bool is_prime[1000000];
  is_prime[0] = 0;
  is_prime[1] = 0;
  for (int i = 2; i < 1000000; i++) {
    is_prime[i] = 1;
  }

  for (int s = 2; s < 1000000; s++) {
    if (is_prime[s] == 1) {
      for (long long i = s * s; i < 1000000; i += s) {
        is_prime[i] = 0;
      }
    }
  }

  while (cin >> N) {

    int ans = 0;
    for (int i = 2; i < N + 1; i++) {
      ans += is_prime[i];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

#define N_MAX 1000000

using namespace std;

int main(void) {
  int n = 0;
  bool isNotPrime[N_MAX] = {false};

  isNotPrime[0] = true;
  int rootN = sqrt(N_MAX);
  for (int i = 2; i <= rootN; i++) {
    for (int j = 2 * i; j < N_MAX; j += i) {
      isNotPrime[j - 1] = true;
    }
  }

  while (cin >> n) {
    // 素数の数をカウント
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (!isNotPrime[i]) {
        cnt++;
      }
    }
    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main(void) {
  int n = 1;
  int nMax = 1;

  vector<int> vecPrime;

  while (cin >> n) {
    if (n > nMax) {
      for (int i = nMax + 1; i <= n; i++) {
        vecPrime.push_back(i);
      }
      nMax = n;
    }

    int rootN = floor(sqrt(n));
    for (int i = 2; i <= rootN; i++) {
      for (int j = 0; j < vecPrime.size(); j++) {
        if (vecPrime[j] % i == 0 && vecPrime[j] != i) {
          vecPrime.erase(vecPrime.begin() + j);
          j--;
        }
      }
    }

    // 素数の数をカウント
    int cnt = 0;
    int sizeVec = vecPrime.size();
    for (int i = 0; i < sizeVec; i++) {
      if (vecPrime[i] > n) {
        break;
      }
      cnt++;
    }
    cout << cnt << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mem0(x) memset(x, 0, sizeof(x))
#define memff(x) memset(x, 0xff, sizeof(x))
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repp(i, a, b) for (int i = a; i <= b; i++)
#define per(i, b, a) for (int i = b; i > a; i--)
#define perr(i, b, a) for (int i = b; i >= a; i--)
#define MAX_N 5050
#define MAX_M 5050
#define MAX_P 1000010
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
map<int, int> m;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
int par[MAX_N];
int rank[MAX_N];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int GCD(int a, int b) {
  if (b == 0)
    return a;
  return GCD(b, b % a);
}
void init(int n) {
  rep(i, 0, n) {
    par[i] = i;
    rank[i] = 0;
  }
}
int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (rank[x] < rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rank[x] == rank[y])
      rank[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
struct node {
  int x, y;
};
bool cmp(int x, int y) { return x > y; }
bool ccmp(node x1, node x2) { return x1.x > x2.x; }
LL mod_pow(LL x, LL n, LL mod) {
  LL res = 0;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
int prime[MAX_P];
bool is_prime[MAX_P];
int x;
int sieve(int n) {
  int p = 0;
  repp(i, 0, n) { is_prime[i] = true; }
  is_prime[0] = is_prime[1] = false;
  repp(i, 2, n) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return p;
}
int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  while (~scanf("%d", &x)) {
    printf("%d\\n", sieve(x));
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int num;
  while (scanf("%d", &num) <= 30) {

    int a, b;
    int cot = 0;
    for (a = 2; a <= num; a++) {

      b = 0;
      for (int i = 2; i <= sqrt(a); i++) {
        if (a % i == 0) {
          b = 1;
          break;
        }
      }
      if (b == 0)
        cot++;
    }
    cout << cot << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
const int MAX_n = 999999;
bool isPrime[MAX_n + 1];
int sum[MAX_n + 1];
int main() {
  memset(isPrime, true, sizeof(isPrime));
  for (int i = 2; 2 * i <= MAX_n; ++i)
    if (isPrime[i])
      for (int j = 2 * i; j <= MAX_n; j += i)
        isPrime[j] = false;
  isPrime[0] = isPrime[1] = false;

  for (int i = 0; i < MAX_n; ++i)
    sum[i + 1] = sum[i] + isPrime[i + 1];

  int n;
  while (scanf("%d", &n) != EOF) {
    int ans = sum[n];
    printf("%d\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <cstdio>
#include <memory.h>
const int maxn = 100000;
int x;
bool isPrime[maxn];
void generateprime() {

  memset(isPrime, true, sizeof(isPrime));
  for (int i = 2; i < maxn; i++)
    for (int j = 2; j * i < maxn; j++)
      isPrime[j * i] = false;
}

int main() {
  generateprime();
  while (scanf("%d", &x) != EOF) {
    int res = 0;
    for (int i = 2; i <= x; i++)
      if (isPrime[i]) {
        res = res + 1;
      }
    printf("%d\\n", res);
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>

using namespace std;

const int INF = 1000000;

int main() {
  int pri[INF];
  int res[INF];
  for (int i = 0; i < INF; i++)
    pri[i] = 0;
  for (int i = 2; i * i < INF; i++) {
    if (pri[i] == 0) {
      for (int j = i * 2; j < INF; j += i) {
        pri[j] = 1;
      }
    }
  }
  int n = 0;
  for (int i = 2; i < INF; i++) {
    if (pri[i] == 0)
      n++;
    res[i] = n;
  }
  int C;
  while (cin >> C) {
    cout << res[C] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;
int phi[1000000];
bool vis[1000000];
int p[1000000];
int sieve(int n) {
  int i, j, cnt = 0;
  phi[1] = 1;
  for (i = 2; i < n; ++i) {
    if (!vis[i]) {
      p[cnt++] = i;
      phi[i] = i - 1;
    }
    for (j = 0; j < cnt && i * p[j] < n; ++j) {
      vis[i * p[j]] = true;
      if (i % p[j])
        phi[i * p[j]] = phi[i] * phi[p[j]];
      else {
        phi[i * p[j]] = phi[i] * p[j];
        break;
      }
    }
  }
  return cnt;
}

int main() {
  int n;
  while (scanf("%d", &n)) {
    printf("%d\\n", sieve(n));
  }

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

#include <bits/stdc++.h>

using namespace std;

#define MAX 1000100
#define LIM 1010

bool composite[MAX + 100];
int prime[MAX + 100];

void sieve() {
  for (int i = 2; i <= LIM; i++) {
    for (int j = i * i; j <= MAX; j += i)
      composite[j] = true;
  }
  int counter = 0;
  for (int i = 2; i <= MAX; i++) {
    if (!composite[i])
      counter += 1;
    prime[i] = counter;
  }
}

int main() {
  sieve();
  int n;
  while (cin >> n) {
    cout << prime[n] << "\\n";
  }
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

int main() {
  int n, flag, c = 1;
  long long a[99999];
  for (int i = 2; i <= 99999; i++) {
    flag = 0;
    for (int j = 2; j <= sqrt(i); j++) {
      if (i % j == 0) {
        flag = 1;
      }
    }
    if (flag == 0) {
      a[i] = 1;
    } else {
      a[i] = -1;
    }
  }

  while (scanf("%d", &n) != EOF) {
    int m = 0;
    for (int i = 2; i <= n; i++) {
      if (a[i] == 1)
        m++;
    }

    cout << m << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <iostream>
#define size 20000000
using namespace std;
int find_prime(int n) {
  int count = 0;
  bool *numberList = new bool[size + 1];
  for (unsigned long i = 3; i <= sqrt(n); i += 2) {
    for (unsigned long j = i; i * j <= n; j += 2)
      numberList[i * j] = true;
  }
  for (unsigned long i = 3; i <= n; i += 2) {
    if (!numberList[i])
      count++;
  }

  return count;
}
int main() {
  int n;
  while ((scanf("%d", &n)) != EOF) {
    if (n == 1 || n == 0)
      printf("0\\n");
    else if (n == 2)
      printf("1\\n");

    else {
      int v = 0;
      v = find_prime(n);
      printf("%d\\n", v + 1);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

int main(int argc, const char *argv[]) {
  int N = 100000;
  // cin >> N;
  bool mark[100000];
  memset(mark, true, 100000);

  mark[0] = mark[1] = false;
  for (int i = 4; i < N; i += 2)
    mark[i] = false;
  for (int i = 3; i * i <= N; i += 2) {
    if (mark[i] == true) {
      for (int j = i * i; j < N; j += i * 2) {
        mark[j] = false;
      }
    }
  }

  int n;
  int cnt;
  while (scanf("%d", &n) == 1) {
    cnt = 0;
    for (int i = 0; i <= n; i++) {
      if (mark[i] == true)
        cnt++;
    }
    cout << cnt << endl;
  }

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

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

// C

const int MAX_N = 1000000;
int prime[MAX_N];
bool is_prime[MAX_N + 1];

int sieve(int n) {
  int p = 0;
  for (int i = 0; i <= n; i++)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return p;
}

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

Error: TLE

Fixed submission:

#include <climits>
#include <cmath>
#include <iostream>

using namespace std;
//?数表
const unsigned long long maxn = 100000;       // 定?最大数
const unsigned long long mmax = maxn / 2 + 1; // 数?的大小
bool nprilist[mmax];                          // 注意false才是?数
void fsetupprime()                            //?数表建立函数
{
  // const unsigned long long half=mmax/2+1;//定?二分??界限
  unsigned long long prei = 1;                  // 用来?存上一个起始点
  for (unsigned long long i = 1; i < mmax; ++i) // 二分??前半段
  {
    if (!nprilist[i]) {
      unsigned long long j = 2 * prei * prei + 2 * prei;
      while ((2 * j + 1) % (2 * i + 1))
        ++j;
      for (
          ; j <= mmax;
          j +=
          2 * i +
          1) // j=((2i+1)^2-1)/2=2i^2+2i，i就是?里的prei，然后本来j???再加一个1的，但?了方便第一次i=3的循??一，故??里?界重?，若???要求?高，?在?里加1以?小????度
        nprilist[j] = 1;
      prei = i;
    }
  }
  /*for(unsigned long long i=half;i<mmax;++i)//二分??后半段
  {
      cout<<3<<endl;
      for(unsigned long long j=1;j<half;++j)
      {
          cout<<4<<endl;
          if(nprilist[j])
              continue;
          else if(!((2*i+1LL)%(2*j+1LL)))
          {
              nprilist[i]=1;
              break;
          }
      }
  }*/
  return;
}
bool fis_prime(const unsigned long long &a) //?数判断接口函数
{
  if (a > 2) {
    if (!(a & 1))
      return 0;
    return !nprilist[(a - 1) / 2];
  }
  if (a == 2)
    return 1;
  if (a == 0 || a == 1)
    return 0;
}
//?数表?束

int main() {
  fsetupprime();
  long long a = 0;
  while (cin >> a) {
    int sum = 0;
    if (a == 0 && 1)
      cout << 0 << endl;
    if (a == 2)
      cout << 1 << endl;
    else {
      for (long long i = 3; i <= a; i += 2)
        if (fis_prime(i))
          ++sum;
      cout << ++sum << endl;
    }
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int box[1000005];
int main() {
  for (int i = 0; i <= 999999; i++)
    box[i] = 1;
  box[1] = box[0] = 0;
  int n;
  int rooted = sqrt(999999);
  for (int i = 2; i <= rooted; i++)
    if (box[i])
      for (int j = 2; j * i <= 999999; j++)
        box[j * i] = 0;
  while (cin >> n) {
    int s = 0;
    for (int i = 1; i <= n; i++)
      s += box[i];
    cout << s << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int n, i, cnt, num[999999];

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

int count_prime() {
  for (i = 1; i <= 999999; i++) {
    if (is_prime(i))
      cnt++;
    num[i] = cnt;
  }
  return 0;
}

int main() {
  count_prime();
  while (scanf("%d", &n) != EOF) {
    printf("%d\\n", num[n]);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded


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

void Sieve(int n) {
  bool prime[n + 1];
  int c = 0;
  memset(prime, true, sizeof(prime));

  for (int p = 2; p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * 2; i <= n; i += p)
        prime[i] = false;
    }
  }
  for (int p = 2; p <= n; p++)
    if (prime[p])
      c++;
  cout << c << endl;
}

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cstdio>

using namespace std;

int pri[1000000];

int main() {

  pri[0] = 1;
  pri[1] = 1;

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

  for (int i = 2; i * i < 1000000; i++) {
    for (int j = 2; j * i < 1000000; j++) {
      pri[i * j] = 1;
    }
  }
  int n, c;
  while (scanf("%d", &n)) {
    c = 0;
    for (int i = 2; i <= n; i++) {
      if (pri[i] == 0) {
        c++;
      }
    }
    printf("%d\\n", c);
  }

  return 0;
}