Problem p02968 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define reg register
#define pr std::pair<int, int>
#define fi first
#define se second
#define FIN(s) freopen(s, "r", stdin)
#define FOUT(s) freopen(s, "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define lep(i, l, r) for (int i = l; i < r; ++i)
#define irep(i, r, l) for (int i = r; i >= l; --i)
#define ilep(i, r, l) for (int i = r; i > l; --i)
#define Rep(i, n) rep(i, 1, n)
#define Lep(i, n) lep(i, 1, n)
#define IRep(i, n) irep(i, n, 1)
#define ILep(i, n) ilep(i, n, 1)
typedef long long ll;
typedef long double ld;

namespace modular {
int MOD;
inline int add(int x, int y) { return (x += y) >= MOD ? x -= MOD : x; }
inline void inc(int &x, int y) { (x += y) >= MOD ? x -= MOD : 0; }
inline int mul(int x, int y) { return 1LL * x * y % MOD; }
inline int qpow(int x, int y) {
  int ans = 1;
  for (; y; y >>= 1, x = mul(x, x))
    if (y & 1)
      ans = mul(ans, x);
  return ans;
}
}; // namespace modular
using namespace modular;

namespace Base {
template <typename Tp> inline Tp input() {
  Tp x = 0, y = 1;
  char c = getchar();
  while ((c < '0' || '9' < c) && c != EOF) {
    if (c == '-')
      y = -1;
    c = getchar();
  }
  if (c == EOF)
    return 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x *= y;
}
template <typename Tp> inline void read(Tp &x) { x = input<Tp>(); }
template <typename Tp> inline void chmax(Tp &x, Tp y) { x < y ? x = y : 0; }
template <typename Tp> inline void chmin(Tp &x, Tp y) { x > y ? x = y : 0; }
}; // namespace Base
using namespace Base;
/*----------------------------------------------------------------------------*/

#define MAX_N 407

int N;
int p[MAX_N], f[MAX_N][MAX_N];

struct seg {
  int l, r;
} s[MAX_N];

inline int sqr(int x) { return x * x; }

pr kx, ky;
bool cmp(int x, int y) {
  if (x > N)
    kx.fi = s[x].r, kx.se = 0;
  else
    kx.fi = s[x].l, kx.se = s[x].r;
  if (y > N)
    ky.fi = s[y].r, ky.se = 0;
  else
    ky.fi = s[y].l, ky.se = s[y].r;
  return kx == ky ? x > y : kx < ky;
}

int solve(int k) {
  memset(f, 0, sizeof(f));
  f[0][0] = 1;
  int t = 0;
  Rep(i, N + N) {
    int x = p[i], ed = std::min(i, k);
    rep(j, 0, ed) {
      if (x > N) {
        inc(f[i][j], mul(f[i - 1][j], s[x].r + 1 - (i - 1 - (t - j))));
      } else {
        inc(f[i][j],
            mul(f[i - 1][j], s[x].r + 1 - (N * 2 - 1 - (x - 1) + (k - j))));
        if (j > 0)
          inc(f[i][j],
              mul(f[i - 1][j - 1], s[x].l + 1 - (i - 1 - (t - (j - 1)))));
      }
      // if (f[i][j]) printf("%d %d %d\n", i, j, f[i][j]);
    }
    if (x <= N)
      t++;
  }
  return f[N + N][k];
}

int main() {
  read(N), read(MOD);
  Rep(i, N + N) {
    s[i].l = (i > N ? 0 : (int)sqrt(N * N - sqr(i - 1)));
    s[i].r = (int)sqrt(4 * N * N - sqr(i - 1));
    if (sqr(i - 1) + sqr(s[i].l) < sqr(N))
      s[i].l++;
    chmin(s[i].r, N + N - 1);
    if (i <= N)
      s[i].l = std::max(0, s[i].l - 1);
    // printf("%d %d\n", s[i].l, s[i].r);
    p[i] = i;
  }
  std::sort(p + 1, p + N + N + 1, cmp);
  // Rep(i, N + N) printf("%d ", p[i]);
  // puts("");
  int res = 0;
  // printf("%d\n", solve(1));
  // return 0;
  rep(k, 0, N) {
    int tmp = solve(k);
    // printf("%d %d\n", k, tmp);
    inc(res, (k & 1) ? MOD - tmp : tmp);
  }
  printf("%d\n", res);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define reg register
#define pr std::pair<int, int>
#define fi first
#define se second
#define FIN(s) freopen(s, "r", stdin)
#define FOUT(s) freopen(s, "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define lep(i, l, r) for (int i = l; i < r; ++i)
#define irep(i, r, l) for (int i = r; i >= l; --i)
#define ilep(i, r, l) for (int i = r; i > l; --i)
#define Rep(i, n) rep(i, 1, n)
#define Lep(i, n) lep(i, 1, n)
#define IRep(i, n) irep(i, n, 1)
#define ILep(i, n) ilep(i, n, 1)
typedef long long ll;
typedef long double ld;

namespace modular {
int MOD;
inline int add(int x, int y) { return (x += y) >= MOD ? x -= MOD : x; }
inline void inc(int &x, int y) { (x += y) >= MOD ? x -= MOD : 0; }
inline int mul(int x, int y) { return 1LL * x * y % MOD; }
inline int qpow(int x, int y) {
  int ans = 1;
  for (; y; y >>= 1, x = mul(x, x))
    if (y & 1)
      ans = mul(ans, x);
  return ans;
}
}; // namespace modular
using namespace modular;

namespace Base {
template <typename Tp> inline Tp input() {
  Tp x = 0, y = 1;
  char c = getchar();
  while ((c < '0' || '9' < c) && c != EOF) {
    if (c == '-')
      y = -1;
    c = getchar();
  }
  if (c == EOF)
    return 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x *= y;
}
template <typename Tp> inline void read(Tp &x) { x = input<Tp>(); }
template <typename Tp> inline void chmax(Tp &x, Tp y) { x < y ? x = y : 0; }
template <typename Tp> inline void chmin(Tp &x, Tp y) { x > y ? x = y : 0; }
}; // namespace Base
using namespace Base;
/*----------------------------------------------------------------------------*/

#define MAX_N 507

int N;
int p[MAX_N], f[MAX_N][MAX_N];

struct seg {
  int l, r;
} s[MAX_N];

inline int sqr(int x) { return x * x; }

pr kx, ky;
bool cmp(int x, int y) {
  if (x > N)
    kx.fi = s[x].r, kx.se = 0;
  else
    kx.fi = s[x].l, kx.se = s[x].r;
  if (y > N)
    ky.fi = s[y].r, ky.se = 0;
  else
    ky.fi = s[y].l, ky.se = s[y].r;
  return kx == ky ? x > y : kx < ky;
}

int solve(int k) {
  memset(f, 0, sizeof(f));
  f[0][0] = 1;
  int t = 0;
  Rep(i, N + N) {
    int x = p[i], ed = std::min(i, k);
    rep(j, 0, ed) {
      if (x > N) {
        inc(f[i][j], mul(f[i - 1][j], s[x].r + 1 - (i - 1 - (t - j))));
      } else {
        inc(f[i][j],
            mul(f[i - 1][j], s[x].r + 1 - (N * 2 - 1 - (x - 1) + (k - j))));
        if (j > 0)
          inc(f[i][j],
              mul(f[i - 1][j - 1], s[x].l + 1 - (i - 1 - (t - (j - 1)))));
      }
      // if (f[i][j]) printf("%d %d %d\n", i, j, f[i][j]);
    }
    if (x <= N)
      t++;
  }
  return f[N + N][k];
}

int main() {
  read(N), read(MOD);
  Rep(i, N + N) {
    s[i].l = (i > N ? 0 : (int)sqrt(N * N - sqr(i - 1)));
    s[i].r = (int)sqrt(4 * N * N - sqr(i - 1));
    if (sqr(i - 1) + sqr(s[i].l) < sqr(N))
      s[i].l++;
    chmin(s[i].r, N + N - 1);
    if (i <= N)
      s[i].l = std::max(0, s[i].l - 1);
    // printf("%d %d\n", s[i].l, s[i].r);
    p[i] = i;
  }
  std::sort(p + 1, p + N + N + 1, cmp);
  // Rep(i, N + N) printf("%d ", p[i]);
  // puts("");
  int res = 0;
  // printf("%d\n", solve(1));
  // return 0;
  rep(k, 0, N) {
    int tmp = solve(k);
    // printf("%d %d\n", k, tmp);
    inc(res, (k & 1) ? MOD - tmp : tmp);
  }
  printf("%d\n", res);
  return 0;
}