Problem p00590 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

// container util
//------------------------------------------
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define iter(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)

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

// constant
//--------------------------------------------
const double eps = 1e-10;
const double pi = acos(-1.0);
const double inf = (int)1e8;

// output vector
// ------------------------------------------
namespace std {
template <typename T> ostream &operator<<(ostream &os, vector<T> const &v) {
  for (auto it = v.begin(); it != v.end(); ++it) {
    os << *it << (it + 1 == v.end() ? "" : ",");
  }
  return os;
}
} // namespace std

// clear memory
#define clr(a) memset((a), 0, sizeof(a))

int gcd(int a, int b) {
  int tmp;
  while (a % b != 0) {
    tmp = a % b;
    a = b;
    b = tmp;
  }
}

int lcm(int a, int b) { return a * b / gcd(a, b); }
vb pn((int)1e4 + 10);
void prime_number(int n) {
  if (n == 0 || n == 1) // return false;
    fill(all(pn), true);
  pn[0] = false;
  pn[1] = false;
  loop(i, 2, n / 2 + 1) {
    if (!pn[i])
      continue;
    for (int j = 2; i * j <= n; j++) {
      pn[i * j] = false;
    }
  }
  // return pn[n];
}

int main() {
  int a, ans = 0;
  while (cin >> a) {
    ans = 0;
    prime_number(a);
    rep(i, a) {
      if (pn[i + 1] && pn[a - i])
        ans++;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

// container util
//------------------------------------------
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define iter(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)

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

// constant
//--------------------------------------------
const double eps = 1e-10;
const double pi = acos(-1.0);
const double inf = (int)1e8;

// output vector
// ------------------------------------------
namespace std {
template <typename T> ostream &operator<<(ostream &os, vector<T> const &v) {
  for (auto it = v.begin(); it != v.end(); ++it) {
    os << *it << (it + 1 == v.end() ? "" : ",");
  }
  return os;
}
} // namespace std

// clear memory
#define clr(a) memset((a), 0, sizeof(a))

int gcd(int a, int b) {
  int tmp;
  while (a % b != 0) {
    tmp = a % b;
    a = b;
    b = tmp;
  }
}

int lcm(int a, int b) { return a * b / gcd(a, b); }
vb a((int)1e4 + 10);
bool prime_number(int n) {
  if (n == 0 || n == 1)
    return false;
  fill(all(a), true);
  loop(i, 2, n / 2 + 1) {
    if (!a[i])
      continue;
    for (int j = 2; i * j <= n; j++) {
      a[i * j] = false;
    }
  }
  return a[n];
}

int main() {
  int a, ans = 0;
  while (cin >> a) {
    ans = 0;
    rep(i, a) {
      if (prime_number(i + 1) && prime_number(a - i))
        ans++;
    }
    cout << ans << endl;
  }
}