Problem p03642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

#define re_ return
#define in_ inline
#define op_ operator
#define inc(l, i, r) for (i = l; i < r; ++i)
#define st_ static
typedef long long ll;

const int mxn = 1 << 9;

int n, m, a[mxn], b[mxn << 1], cnt[2], h[mxn << 1];
struct edge {
  int v, o;
} e[mxn << 2];

in_ ll dP(ll a, ll b, ll p) {
  st_ ll r;
  for (r = 1; b; a = a * a % p, b >>= 1)
    b & 1 ? r = r * a % p : 0;
  re_ r;
}

in_ char chk(ll a, ll p) {
  st_ ll s, r, i;
  for (r = p - 1, s = 0; ~r & 1; ++s, r /= 2)
    ;
  if (i = dP(a, r, p), i == 1 || i == p - 1)
    re_ 1;
  for (; s--;)
    if ((i = i * i % p) == p - 1)
      re_ 1;
  re_ 0;
}

in_ char isP(int p) {
  if (p == 2)
    re_ 1;
  if (p == 1 || ~p & 1)
    re_ 0;
  st_ int i;
  //    inc(0, i, 48)
  //        if(!chk(rand()%(p-2)+1, p))
  //            re_ 0;

  for (i = 2; i * i <= p; ++i)
    if (p % i == 0)
      re_ 0;
  re_ 1;
}

in_ void aE(int u, int v) {
  st_ int tot;
  e[++tot] = (edge){v, h[u]}, u[h] = tot;
}

char vis[mxn << 1];
char Hun(int u) {
  st_ int f[mxn << 1];
  int i, v;
  for (i = u[h]; v = i[e].v; i = i[e].o)
    if (!vis[v])
      if (vis[v] = 1, !f[v] || Hun(f[v]))
        re_ f[v] = u, 1;
  re_ 0;
}

int main() {
  srand(998244353);
  int i, j;
  scanf("%d", &n);
  inc(0, i, n) scanf("%d", a + i);
  sort(a, a + n);
  inc(0, i, n) {
    if (!i || a[i] > a[i - 1] + 1)
      b[++m] = a[i], ++cnt[a[i] & 1];
    if (i > n - 2 || a[i] < a[i + 1] - 1)
      b[++m] = a[i] + 1, ++cnt[a[i] + 1 & 1];
  }
  inc(1, i, m + 1) if (b[i] & 1)
      inc(1, j, m + 1) if (~b[j] & 1 && isP(abs(b[i] - b[j]))) aE(i, j);

  j = 0;
  inc(1, i, m + 1) if (b[i] & 1) memset(vis, 0, sizeof(vis)), j += Hun(i);

  printf("%d\n", cnt[0] + cnt[1] - j + (cnt[0] - j & 1));
  //    printf("%d\n", m);
  //    for(;;) scanf("%d", &i), printf("%d\n", isP(i));
  0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

#define re_ return
#define in_ inline
#define op_ operator
#define inc(l, i, r) for (i = l; i < r; ++i)
#define st_ static
typedef long long ll;

const int mxn = 1 << 9;

int n, m, a[mxn], b[mxn << 1], cnt[2], h[mxn << 1];
struct edge {
  int v, o;
} e[1 << 20];

in_ ll dP(ll a, ll b, ll p) {
  st_ ll r;
  for (r = 1; b; a = a * a % p, b >>= 1)
    b & 1 ? r = r * a % p : 0;
  re_ r;
}

in_ char chk(ll a, ll p) {
  st_ ll s, r, i;
  for (r = p - 1, s = 0; ~r & 1; ++s, r /= 2)
    ;
  if (i = dP(a, r, p), i == 1 || i == p - 1)
    re_ 1;
  for (; s--;)
    if ((i = i * i % p) == p - 1)
      re_ 1;
  re_ 0;
}

in_ char isP(int p) {
  if (p == 2)
    re_ 1;
  if (p == 1 || ~p & 1)
    re_ 0;
  st_ int i;
  //    inc(0, i, 48)
  //        if(!chk(rand()%(p-2)+1, p))
  //            re_ 0;

  for (i = 2; i * i <= p; ++i)
    if (p % i == 0)
      re_ 0;
  re_ 1;
}

in_ void aE(int u, int v) {
  st_ int tot;
  e[++tot] = (edge){v, h[u]}, u[h] = tot;
}

char vis[mxn << 1];
char Hun(int u) {
  st_ int f[mxn << 1];
  int i, v;
  for (i = u[h]; v = i[e].v; i = i[e].o)
    if (!vis[v])
      if (vis[v] = 1, !f[v] || Hun(f[v]))
        re_ f[v] = u, 1;
  re_ 0;
}

int main() {
  srand(998244353);
  int i, j;
  scanf("%d", &n);
  inc(0, i, n) scanf("%d", a + i);
  sort(a, a + n);
  inc(0, i, n) {
    if (!i || a[i] > a[i - 1] + 1)
      b[++m] = a[i], ++cnt[a[i] & 1];
    if (i > n - 2 || a[i] < a[i + 1] - 1)
      b[++m] = a[i] + 1, ++cnt[a[i] + 1 & 1];
  }
  inc(1, i, m + 1) if (b[i] & 1)
      inc(1, j, m + 1) if (~b[j] & 1 && isP(abs(b[i] - b[j]))) aE(i, j);

  j = 0;
  inc(1, i, m + 1) if (b[i] & 1) memset(vis, 0, sizeof(vis)), j += Hun(i);

  printf("%d\n", cnt[0] + cnt[1] - j + (cnt[0] - j & 1));
  //    printf("%d\n", m);
  //    for(;;) scanf("%d", &i), printf("%d\n", isP(i));
  0;
}