alexjercan/bugnet huggingface dataset:
Problem p00587 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

string solve(char op, string t1, string t2) {
  string ret = "";
  unsigned int i = 0, j = 0;
  while (i < t1.size() && j < t2.size()) {
    if (t1[i] == t2[j]) {
      ret += t1[i];
      i++;
      j++;
    } else {
      bool comma = (t1[i] == ',' || t2[i] == ',');
      if ((comma && t1[i] == ',') || (!comma && t1[i] == ')')) {
        swap(i, j);
        swap(t1, t2);
      }
      int c = 0;
      unsigned int k = i;
      for (; k < t1.size(); ++k) {
        if (t1[k] == '(')
          c++;
        else if (t1[k] == ')')
          c--;
        if (c <= 0) {
          break;
        } else if (op == 'u') { // for union
          ret += t1[k];
        }
      }
      if (op == 'u')
        ret += t1[k]; // for union
      i = k + 1;
    }
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  char op;
  string t1, t2;
  while (cin >> op) {
    cin >> t1 >> t2;
    cout << solve(op, t1, t2) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int depth[2][101];

string pad(int height) {
  int len = (height <= 0 ? 0 : 3 * ((1 << height) - 1));
  string ret = "";
  for (int i = 0; i < len; ++i) {
    ret += " ";
  }
  return ret;
}

void pad(vector<string> &t, int height) {
  vector<string> ts(t.size(), ""); // tree with spaces;
  for (int i = 0; i < (int)t.size(); ++i) {
    for (int j = 0; j < (int)t[i].size(); ++j) {
      if (t[i][j] == ',') {
        if (t[i][j - 1] == '(')
          ts[i] += pad(height - depth[i][j]);
        ts[i] += ',';
        if (t[i][j + 1] == ')')
          ts[i] += pad(height - depth[i][j]);
      } else {
        ts[i] += t[i][j];
      }
    }
    t[i] = ts[i];
  }
}

string intersection(vector<string> &t, int height) {
  pad(t, height);
  string ret = "";
  for (int i = 0; i < (int)t[0].size(); ++i) {
    if (t[0][i] != ' ' && t[1][i] != ' ')
      ret += t[0][i];
  }
  return ret;
}

string unite(vector<string> &t, int height) {
  pad(t, height);
  string ret = "";
  for (int i = 0; i < (int)t[0].size(); ++i) {
    if (t[0][i] != ' ') {
      ret += t[0][i];
    } else if (t[1][i] != ' ') {
      ret += t[1][i];
    }
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  char op;
  vector<string> t(2);
  while (cin >> op) {
    cin >> t[0] >> t[1];

    vector<int> height(2, 0);
    for (int i = 0; i < 2; ++i) {
      int c = 0;
      for (int j = 0; j < (int)t[i].size(); ++j) {
        if (t[i][j] == '(')
          c++;
        depth[i][j] = c;
        if (t[i][j] == ')')
          c--;
        height[i] = max(height[i], c);
      }
    }

    switch (op) {
    case 'i':
      cout << intersection(t, max(height[0], height[1])) << endl;
      break;
    case 'u':
      cout << unite(t, max(height[0], height[1])) << endl;
      break;
    }
  }

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

#include <cstdio>

using namespace std;

const int nil = -1;
const int MAX_N = 256;

struct Node {
  int l, r, cnt;
  Node() {
    l = r = nil;
    cnt = 0;
  }
};

Node v[MAX_N];
int nodeIndex;

int parse(const char *s, int n, int root) {
  v[root].cnt++;
  if (s[++n] != ',') {
    if (v[root].l == nil)
      v[root].l = ++nodeIndex;
    n = parse(s, n, v[root].l);
  }
  if (s[++n] != ')') {
    if (v[root].r == nil)
      v[root].r = ++nodeIndex;
    n = parse(s, n, v[root].r);
  }
  return n + 1;
}

void print(int root, int cnt) {
  if (v[root].cnt < cnt)
    return;
  putchar('(');
  if (v[root].l != nil)
    print(v[root].l, cnt);
  putchar(',');
  if (v[root].r != nil)
    print(v[root].r, cnt);
  putchar(')');
}

int main() {
  char op[5], t1[1024], t2[1024];
  while (scanf("%s %s %s", op, t1, t2) == 3) {
    for (int i = 0; i < MAX_N; i++)
      v[i] = Node();
    nodeIndex = 0;
    parse(t1, 0, 0);
    parse(t2, 0, 0);
    print(0, (op[0] == 'u' ? 1 : 2));
    putchar('\\n');
  }
  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>

using namespace std;
/*
const int nil = -1;
const int MAX_N = 256;

struct Node {
  int l, r, cnt;
  Node() {
    l = r = nil;
    cnt = 0;
  }
};

Node v[MAX_N];
int nodeIndex;

int parse(const char *s, int n, int root) {
  v[root].cnt++;
  if (s[++n] != ',') {
    if (v[root].l == nil) v[root].l = ++nodeIndex;
    n = parse(s, n, v[root].l);
  }
  if (s[++n] != ')') {
    if (v[root].r == nil) v[root].r = ++nodeIndex;
    n = parse(s, n, v[root].r);
  }
  return n+1;
}

void print(int root, int cnt) {
  if (v[root].cnt < cnt) return;
  putchar('(');
  if (v[root].l != nil) print(v[root].l, cnt);
  putchar(',');
  if (v[root].r != nil) print(v[root].r, cnt);
  putchar(')');
}
*/
int main() {
  char op[10], t1[256], t2[256];
  while (scanf("%s %s %s", op, t1, t2) == 3) {
    printf("%s\\n", t1);
    /*
        for (int i=0;i<MAX_N;i++) v[i] = Node();
        nodeIndex = 0;
        parse(t1, 0, 0);
        parse(t2, 0, 0);
        print(0, (op=='u'?1:2)); putchar('\\n');
    */
  }
  return 0;
}
Problem p00587 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct Node {
  Node *left;
  Node *right;
  Node() { left = NULL, right = NULL; }
};

int findcc(string s) {
  int dd = 0;
  for (int i = 0; i < s.length(); ++i) {
    if (s[i] == '(')
      dd++;
    else if (s[i] == ')')
      dd--;
    else if (s[i] == ',' && dd == 1)
      return i;
  }
  return -1;
}

Node *parse(string s) {
  Node *r = new Node();
  int ff = findcc(s);
  if (ff != 1)
    r->left = parse(s.substr(1, ff - 1));
  if (ff != s.length() - 2) {
    string tmp = s.substr(ff + 1);
    r->right = parse(tmp.substr(0, tmp.length() - 1));
  }
  return r;
}

Node *intersect(Node *r1, Node *r2) {
  Node *ret = new Node();
  if (r1->left != NULL && r2->left != NULL)
    ret->left = intersect(r1->left, r2->left);

  if (r1->right != NULL && r2->right != NULL)
    ret->right = intersect(r1->right, r2->right);

  return ret;
}

Node *uni(Node *r1, Node *r2) {
  if (r1 == NULL && r2 == NULL)
    return NULL;

  Node *ret = new Node();
  ret->left =
      uni(((r1 == NULL) ? NULL : r1->left), (r2 == NULL) ? NULL : r2->left);
  ret->right =
      uni(((r1 == NULL) ? NULL : r1->right), (r2 == NULL) ? NULL : r2->right);
  return ret;
}

string toStr(Node *tree) {
  string res = "(";
  if (tree->left != NULL)
    res += toStr(tree->left);
  res += ",";
  if (tree->right != NULL)
    res += toStr(tree->right);
  res += ")";
  return res;
}

void treedel(Node *node) {
  if (node->left != NULL)
    treedel(node->left);
  if (node->right != NULL)
    treedel(node->right);
  delete node;
  return;
}

int main() {
  string op, t1, t2;
  while (cin >> op >> t1 >> t2) {
    Node *r1 = parse(t1);
    Node *r2 = parse(t2);
    Node *ans;
    if (op == "i")
      ans = intersect(r1, r2);
    else if (op == "u")
      ans = uni(r1, r2);

    cout << toStr(ans) << endl;
    treedel(r1);
    treedel(r2);
    treedel(ans);
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int a, b;
  while (1)
    ;
  while (cin >> a >> b) {
    cout << a + b << endl;
  }
  return 0;
}
Problem p00588 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int T, N;
  cin >> T;
  while (T--) {
    cin >> N;
    int book[10005] = {0};

    for (int k = 0; k < 2; k++) {
      string str;
      char c;
      for (int i = 0; i < 2 * N; i++) {
        cin >> c;
        str += c;
      }
      str = "N" + str + "N";

      for (int i = 0; i < N + 1; i++) {
        if (str[i * 2] == 'Y' || str[i * 2 + 1] == 'Y') {
          book[i] |= (1 << k);
        }
      }
    }

    int a = 0, b = 1, na, nb;
    for (int i = 0; i < N + 1; i++) {
      switch (book[i]) {
      case 0:
        na = a;
        nb = b;
        break;

      case 1:
        na = min(a + 1, b + 1);
        nb = min(b + 1, a + 1);
        break;

      case 2:
        na = min(a + 3, b + 2);
        nb = min(b + 1, a + 2);
        break;

      case 3:
        na = min(a + 3, b + 2);
        nb = min(b + 2, a + 2);
        break;
      }

      a = na + 1;
      b = nb + 1;
    }

    cout << a - 1 << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int T, N;
  cin >> T;
  while (T--) {
    cin >> N;
    int book[128] = {0};

    for (int k = 0; k < 2; k++) {
      string str;
      char c;
      for (int i = 0; i < 2 * N; i++) {
        cin >> c;
        str += c;
      }
      str = "N" + str + "N";

      for (int i = 0; i < N + 1; i++) {
        if (str[i * 2] == 'Y' || str[i * 2 + 1] == 'Y') {
          book[i] |= (1 << k);
        }
      }
    }

    int a = 0, b = 1, na, nb;
    for (int i = 0; i < N + 1; i++) {
      switch (book[i]) {
      case 0:
        na = a;
        nb = b;
        break;

      case 1:
        na = min(a + 1, b + 1);
        nb = min(b + 1, a + 1);
        break;

      case 2:
        na = min(a + 3, b + 2);
        nb = min(b + 1, a + 2);
        break;

      case 3:
        na = min(a + 3, b + 2);
        nb = min(b + 2, a + 2);
        break;
      }

      a = na + 1;
      b = nb + 1;
    }

    cout << a - 1 << endl;
  }
}
Problem p00589 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

vector<string> con(10);

int main() {
  con[0] = " ";
  con[1] = "',.!?";
  con[2] = "abcABC";
  con[3] = "defDEF";
  con[4] = "ghiGHI";
  con[5] = "jklJKL";
  con[6] = "mnoMNO";
  con[7] = "pqrsPQRS";
  con[8] = "tuvTUV";
  con[9] = "wxyzWXYZ";

  char in[1000000];
  while (~scanf("%s", in)) {
    int now;
    string res = "";
    int c = 0;
    now = in[0] - '0';
    c = -1;
    rep(i, strlen(in)) {
      int buf = in[i] - '0';
      if (now != 0 && now != buf || buf == 0)
        cout << con[now][c % con[now].size()], c = 0;
      else
        now ? c++ : c = 0;
      now = buf;
    }
    cout << con[now][c % con[now].size()] << endl;
  }
  return 0;
}

Error: -8

Fixed submission:

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

vector<string> con(10);

int main() {
  con[1] = "',.!?";
  con[2] = "abcABC";
  con[3] = "defDEF";
  con[4] = "ghiGHI";
  con[5] = "jklJKL";
  con[6] = "mnoMNO";
  con[7] = "pqrsPQRS";
  con[8] = "tuvTUV";
  con[9] = "wxyzWXYZ";

  char in[1000000];
  while (~scanf("%s", in)) {
    int now;
    string res = "";
    int c = 0;
    rep(i, strlen(in)) {
      //      printf("%d ", i);
      if (i == 0) {
        now = in[i] - '0';
        c = -1;
      }
      if (now == in[i] - '0') {
        if (now == 0) {
          res += " ";
        } else {
          c++;
          //	  c = (c+1)%con[now].size();
        }
      } else {
        res += con[now][c % con[now].size()];
        now = in[i] - '0';
        c = 0;
      }
      //      printf("%d\\n", now);
    }
    if (now)
      res += con[now][c];
    cout << res << endl;
  }
  return 0;
}
Problem p00590 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
bool prime[10001];
int res[10001];
vector<int> pid;
int main(void) {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  for (int i = 2; i <= 10000; i++) {
    if (prime[i]) {
      pid.push_back(i);
      for (int j = i * 2; j <= 10000; j += i)
        prime[j] = false;
    }
  }
  pid.push_back(10001);
  for (int n = 1; n <= 10000; n++) {
    for (int i = 0; pid[i] <= n; i++) {
      if (prime[pid[i]] && prime[n - pid[i] + 1])
        res[n]++;
    }
  }
  int n;
  while (~scanf("%d", &n)) {
    int cnt = 0;
    printf("%d\\n", res[n]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
bool prime[10001];
vector<int> pid;
int main(void) {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  for (int i = 2; i <= 10000; i++) {
    if (prime[i]) {
      pid.push_back(i);
      for (int j = i * 2; j <= 10000; j += i)
        prime[j] = false;
    }
  }
  while (1) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    int cnt = 0;
    for (int i = 0; pid[i] <= n; i++) {
      if (prime[pid[i]] && prime[n - pid[i] + 1])
        cnt++;
    }
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p00590 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n;
  int p[10002];
  fill(p, p + 10002, 1);
  p[0] = p[1] = 0;
  for (int i = 0; i * i < 10002; i++) {
    if (p[i])
      for (int j = i * 2; j < 10002; j += i)
        p[j] = 0;
  }

  while (cin >> n) {
    if (n == 0)
      break;
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      if (p[i] == 1 && p[n + 1 - i] == 1)
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n;
  int p[10002];
  fill(p, p + 10002, 1);
  p[0] = p[1] = 0;
  for (int i = 0; i * i < 10002; i++) {
    if (p[i])
      for (int j = i * 2; j < 10002; j += i)
        p[j] = 0;
  }

  while (cin >> n, n) {
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      if (p[i] == 1 && p[n + 1 - i] == 1)
        cnt++;
    }
    cout << cnt << endl;
  }
}
Problem p00590 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll prime(ll x) {
  ll i;
  if (x < 2)
    return 0;
  else if (x == 2)
    return 1;
  if (x % 2 == 0)
    return 0;
  for (i = 3; i * i <= x; i += 2) {
    if (x % i == 0)
      return 0;
  }
  return 1;
}
int main() {
  int N;
  while (cin >> N) {
    int cnt = 0;
    for (int i = 1; i <= N; i++) {
      if (prime(i) && prime(N - i + 1))
        cnt++;
    }
    cout << cnt << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll prime(ll x) {
  ll i;
  if (x < 2)
    return 0;
  else if (x == 2)
    return 1;
  if (x % 2 == 0)
    return 0;
  for (i = 3; i * i <= x; i += 2) {
    if (x % i == 0)
      return 0;
  }
  return 1;
}
int main() {
  int N;
  while (cin >> N, N) {
    int cnt = 0;
    for (int i = 1; i <= N; i++) {
      if (prime(i) && prime(N - i + 1))
        cnt++;
    }
    cout << cnt << endl;
  }
}
Problem p00590 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

bool isprime[10001];
void MakePrime(int n) {
  isprime[2] = true;
  for (int i = 3; i < n; i += 2) {
    bool a = false;
    for (int j = 3; j <= std::sqrt(i); j += 2) {
      if (i % j == 0) {
        a = true;
      }
    }
    if (a == false) {
      isprime[i] = true;
    }
  }
}

int main() {
  int n;
  MakePrime(10000);
  while (cin >> n) {
    int ans = 0;
    for (int i = 2; i < n; i++) {
      if (isprime[i] && isprime[n + 1 - i])
        ans++;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int main() {

  bool prime[10002];

  int i = 0;

  for (i = 0; i < 10002; i++) {
    prime[i] = true;
  }

  for (int p = 2; p * p <= 10001; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      // Update all multiples of p
      for (int i = p * 2; i <= 10001; i += p)
        prime[i] = false;
    }
  }

  int N;
  cin >> N;
  while (N != -1)

  {
    int count = 0;

    for (int i = 2; i <= N; i++) {
      if (prime[i] == true && prime[N + 1 - i] == true) {
        count++;
      }
    }

    cout << count << "\\n";
    cin >> N;
  }

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

#include <cmath>
#include <iostream>
using namespace std;
bool prime(int n);
int main() {
  int n, i, count;
  while (cin >> n) {
    count = 0;
    for (i = 2; i <= n; i++) {
      if (prime(i) && prime(n - i + 1)) {
        count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}

bool prime(int n) {
  int i, k = sqrt(n);
  if (n == 1) {
    return 0;
  }
  for (i = 2; i <= k; i++) {
    if (n % i == 0) {
      return 0;
      break;
    }
  }
  if (i == (k + 1)) {
    return 1;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
bool prime(int a) {
  if (a == 1)
    return 0;
  for (int i = 2; i <= a / 2; i++)
    if (a % i == 0)
      return 0;
  return 1;
}
int main() {
  int n;
  while (cin >> n) {
    int sum = 0;
    for (int i = 1; i <= n; i++)
      if (prime(i) && prime(n + 1 - i))
        sum++;
    cout << sum << endl;
  }
  return 0;
}
Problem p00590 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  bool prime[10001];
  int a[10001];
  int i, j, k;
  for (i = 0; i <= 10000; ++i) {
    prime[i] = true;
    a[i] = 0;
  }
  prime[0] = false;
  prime[1] = false;
  for (i = 2; i <= 5000; ++i)
    for (j = 2; j * i <= 10000; ++j) {
      if (prime[i] == true)
        prime[j * i] = false;
    }
  for (i = 1; i <= 10000; ++i) {
    for (k = 1; k <= i; ++k) {
      if (prime[k] == 1 && prime[i - k + 1] == 1)
        ++a[i];
    }
  }
  int n;
  while (cin >> n) {
    cout << a[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
bool prime(int n) {
  int i;
  for (i = 2; i <= n / 2; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int main() {
  int n, i, count = 0;
  while (cin >> n) {
    count = 0;
    for (i = 2; i < n; i++) {
      if (prime(i)) {
        if (prime(n + 1 - i))
          count++;
      } else
        continue;
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00590 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int power(int b, int e, int p) {
  int ret = 1;
  while (e > 0) {
    if ((e & 1) != 0) {
      ret = (ret * b) % p;
    }
    b = (b * b) % p;
    e >>= 1;
  }
  return ret % p;
}

bool fermat(int p) {
  if (p <= 1)
    return false;
  if (p != 2 && p % 2 == 0)
    return false;
  if (p != 3 && p % 3 == 0)
    return false;
  if (p != 5 && p % 5 == 0)
    return false;
  if (p != 7 && p % 7 == 0)
    return false;
  if (p != 11 && p % 11 == 0)
    return false;
  for (int x = 0; x < 50; ++x) {
    int a = rand() % (p - 1) + 1;
    if (power(a, p - 1, p) != 1)
      return false;
  }
  return true;
}

int main() {
  int n;
  map<int, int> table;
  for (int x = 2; x < 100000; ++x) {
    if (fermat(x))
      table[x] = 100;
  }
  while (scanf("%d", &n) == 1) {
    int t = 0;
    for (int x = 1, y = n; x <= n; x++, y--) {
      if (table[x] == 100 && table[y] == 100) {
        ++t;
      }
    }
    cout << t << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int power(int b, int e, int p) {
  int ret = 1;
  while (e > 0) {
    if ((e & 1) != 0) {
      ret = (ret * b) % p;
    }
    b = (b * b) % p;
    e >>= 1;
  }
  return ret % p;
}

bool fermat(int p) {
  if (p <= 1)
    return false;
  if (p != 2 && p % 2 == 0)
    return false;
  if (p != 3 && p % 3 == 0)
    return false;
  if (p != 5 && p % 5 == 0)
    return false;
  if (p != 7 && p % 7 == 0)
    return false;
  if (p != 11 && p % 11 == 0)
    return false;
  for (int x = 0; x < 50; ++x) {
    int a = rand() % (p - 1) + 1;
    if (power(a, p - 1, p) != 1)
      return false;
  }
  return true;
}

int main() {
  int n;
  while (scanf("%d", &n) == 1) {
    int t = 0;
    for (int x = 1, y = n; x <= n; x++, y--) {
      if (fermat(x) && fermat(y)) {
        ++t;
      }
    }
    cout << t << endl;
  }
  return 0;
}
Problem p00590 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  static int i, j, prime[1000000];
  static int si[1000000];
  int n;
  for (i = 0; i < 1000000; i++) {
    prime[i] = 1;
    si[i] = 0;
  }

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

  for (i = 2; i < 1000000; i++) {
    if (prime[i] == 1) {
      for (j = i + i; j < 1000000; j = j + i)
        prime[j] = 0;
    }
  }

  int arr1[10009];
  int arr2[10009];

  while (cin >> n) {

    for (i = 0; i < 10009; i++) {
      arr1[i] = 0;
      arr2[i] = 0;
    }

    for (i = 0; i < n; i++) {
      arr1[i] = i + 1;
    }
    int h = 0;
    for (i = n; i >= 1; i--) {
      arr2[h++] = i;
    }
    int count = 0;
    for (i = 1; i <= n; i++) {
      if (prime[arr1[i]] != 0 && prime[arr2[i]] != 0) {
        count++;
      }
    }
    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int is_prime(long long int j, long long int k) {
  long long int sum, x, p = 0, q = 0;
  for (x = j;;) {

    sum = 0;
    for (long long int i = 1; i <= x; i++) {
      if (x % i == 0)
        sum++;
      if (sum > 2)
        break;
    }
    if (sum == 2)
      p++;
    q++;
    x = k;
    if (q == 2)
      return p;
  }
}
int main() {
  long long int i, k, j, y, s, n;
  while (scanf("%lld", &n) != EOF) {
    s = 0;
    for (j = 1, k = n; j <= n && k >= 1; j++, k--) {
      y = is_prime(j, k);
      if (y == 2)
        s++;
    }
    cout << s << endl;
  }
  return 0;
}