Problem:
Pair of Primes

We arrange the numbers between 1 and N (1 <= N <= 10000) in increasing
order and decreasing order like this:

1 2 3 4 5 6 7 8 9 . . . N
N . . . 9 8 7 6 5 4 3 2 1

Two numbers faced each other form a pair. Your task is to compute the number of pairs P such that both
numbers in the pairs are prime.

Input

Input contains several test cases. Each test case consists of an integer N in
one line.

Output

For each line of input, output P .

Sample Input

1
4
7
51

Output for the Sample Input

0
2
2
6

Solution in C (judge verdict: Time Limit Exceeded)

#include<stdio.h>
#include<stdlib.h>

int *SoE (int n,int *plist) {
  int list[n-1];
  int i,j;

  for(i=2; i<n+1; i++) list[i-2] = i;

  for(j=2; j*j<n+2; j++) {
    if(list[j-2]!=0) {
      for(i=1; j-2+list[j-2]*i < n; i++) {
	list[j-2+list[j-2]*i] = 0;
      }
    }
  }

  j=1;
  for(i=0; i<n; i++) {
    if(list[i]!=0) *(plist+j) = list[i],j++;
  }

  *plist = j-1;

  return plist;
}

int isPrime (int n, int *plist) {

  int p = *plist;
  int i;
  for(i=1; i<=p; i++) if(*(plist+i) == n) return 1;

  return 0;
}

int main() {

  int *plist;
  plist = malloc(sizeof(int)*10001);
  if(plist==NULL) return -1;

  plist = SoE(10001,plist);

  int j,k=0;
  int N=0;
  int c=0;
  while(scanf("%d",&N) != EOF) {
    for(j=1; j<=(N+1)/2; j++) {
      if( (isPrime(j,plist)==1)&&(isPrime(N-j+1,plist) == 1) ) c++;
    }
    printf("%d\n",c*2);
    c=0;
  }
  return 0;
}