Problem p02257 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// ---------------------
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
#define IINF (1 << 21)
typedef long long ll;

int main() {
  int N;
  cin >> N;
  // ll q[N];
  ll q[10100];
  ll maximum = 0;

  REP(i, N) {
    cin >> q[i];
    maximum = max(maximum, q[i]);
  }

  // int table[maximum + 1];
  int table[12000];
  REP(i, 12000) {
    table[i] = 1; // possibly prime number
  }
  table[0] = 0;
  table[1] = 0;

  for (ll base = 2; base <= 10100; base++) {
    if (!table[base])
      continue;
    for (ll p = 2; base * p <= 10100; p++) {
      // dump(base * p);
      table[base * p] = 0;
    }
  }

  ll sum = 0;
  REP(i, N) {
    // dump(i);

    if (q[i] < 10100) {
      sum += table[q[i]];
    } else {
      int ok = 1;
      for (ll base = 2; base <= 10000 && base * base <= q[i]; base++) {
        if (q[i] % base == 0) {
          ok = 0;
          break;
        }
      }
      if (ok)
        sum++;
    }
  }
  //
  // REP(i, 100) {
  //   // dump(table[i]);
  //   if (i) cout << " ";
  //   cout << table[i];
  // }

  cout << sum << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// ---------------------
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
#define IINF (1 << 21)
typedef long long ll;

int main() {
  int N;
  cin >> N;
  // ll q[N];
  ll q[10100];
  ll maximum = 0;

  REP(i, N) {
    cin >> q[i];
    maximum = max(maximum, q[i]);
  }

  // int table[maximum + 1];
  int table[12000];
  REP(i, 12000) {
    table[i] = 1; // possibly prime number
  }
  table[0] = 0;
  table[1] = 0;

  for (ll base = 2; base <= 10100; base++) {
    if (!table[base])
      continue;
    for (ll p = 2; base * p <= 10100; p++) {
      // dump(base * p);
      table[base * p] = 0;
    }
  }

  ll sum = 0;
  REP(i, N) {
    sum += table[q[i]];

    if (q[i] < 10100) {
    } else {
      for (ll base = 2; base <= 10000; base++) {
        if (q[i] % base == 0) {
          sum++;
          break;
        }
      }
    }
  }
  //
  // REP(i, 100) {
  //   // dump(table[i]);
  //   if (i) cout << " ";
  //   cout << table[i];
  // }

  cout << sum << endl;
  return 0;
}