Problem p03755 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ext/pb_ds/priority_queue.hpp>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

const int maxn = 5005;
const int maxm = 1E4 + 10;
typedef long long LL;
const LL mo = 1000000007;

int n, k, x, tail, Ans = 1, s[maxn], f[maxn][maxm];

inline int Mul(const LL &x, const LL &y) { return x * y % mo; }
inline int Add(const int &x, const int &y) {
  return x + y < mo ? x + y : x + y - mo;
}
inline int Dec(const int &x, const int &y) {
  return x - y >= 0 ? x - y : x - y + mo;
}

int main() {
#ifdef DMC
  freopen("DMC.txt", "r", stdin);
#endif

  f[0][1] = 1;
  cin >> n >> k >> x;
  for (int i = 0; i < k; i++)
    scanf("%d", &s[i]);
  for (int i = 1; i < k; i++)
    tail = max(tail, s[i] - s[i - 1]);
  for (int i = 1; i <= 2 * x; i++) {
    int sum = 0, Max = i + (i & 1);
    for (int j = 1; j <= tail; j++) {
      sum = Add(sum, f[i - 1][j - 1]);
      if (j - Max - 1 >= 0)
        sum = Dec(sum, f[i - 1][j - Max - 1]);
      f[i][j] = sum;
      if (i + 1 >= j)
        f[i][j] = Add(f[i][j], 1);
    }
  }
  for (int i = 1; i < k; i++)
    Ans = Mul(Ans, f[2 * x - 2 * i + 1][s[i] - s[i - 1]]);
  cout << Ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ext/pb_ds/priority_queue.hpp>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

const int N = 303;
typedef long long LL;
const LL mo = 1000000007;

int n, k, x, Ans = 1, s[N], f[N][N], q[N];

inline int Mul(const LL &x, const LL &y) { return x * y % mo; }
inline int min(const int &x, const int &y) { return x < y ? x : y; }
inline int max(const int &x, const int &y) { return x > y ? x : y; }
inline int Add(const int &x, const int &y) {
  return x + y < mo ? x + y : x + y - mo;
}
inline int Dec(const int &x, const int &y) {
  return x - y >= 0 ? x - y : x - y + mo;
}

int ksm(int x, int y) {
  int ret = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      ret = Mul(ret, x);
    x = Mul(x, x);
  }
  return ret;
}

inline int Solve(int tot, int L, int R, int sum) {
  f[1][1] = 1;
  for (int j = 2; j <= sum; j++) {
    int head, tail, cnt;
    cnt = f[j - 1][j - 1];
    q[head = tail = 1] = j - 1;
    for (int i = j, TT = tot - sum + j; i <= TT; i++) {
      while (head <= tail) {
        int l = q[head], r = i;
        int nl = max(L - j + 2, R - sum + j - 1);
        if (nl < 0) {
          cnt = Dec(cnt, f[q[head]][j - 1]);
          ++head;
          continue;
        }
        int nr = max(L - j + 1, R - sum + j);
        if (nr < 0) {
          cnt = Dec(cnt, f[q[head]][j - 1]);
          ++head;
          continue;
        }
        if (nl + nr < r - l - 1) {
          cnt = Dec(cnt, f[q[head]][j - 1]);
          ++head;
          continue;
        }
        break;
      }
      f[i][j] = cnt;
      if (f[i][j - 1])
        q[++tail] = i, cnt = Add(cnt, f[i][j - 1]);
    }
  }
  return f[tot][sum];
}

inline int Calc(int tot, int L, int R) {
  if (L < 0 || R < 0)
    return 0;
  if (L + R >= tot - 2)
    return ksm(2, tot - 2);
  int ret = 0;
  for (int i = 1; i <= tot - 2; i++)
    ret = Add(ret, Solve(tot, L, R, i + 2));
  return ret;
}

int main() {
#ifdef DMC
  freopen("DMC.txt", "r", stdin);
#endif

  cin >> n >> k >> x;
  for (int i = 0; i < k; i++)
    scanf("%d", &s[i]);
  for (int i = 1; i < k; i++) {
    if (s[i] - s[i - 1] == 1)
      continue;
    int L = max(x - i + 1, k - i);
    int R = max(x - i, k - 1 - i);
    Ans = Mul(Ans, Calc(s[i] - s[i - 1] + 1, L, R));
    if (!Ans) {
      cout << 0 << endl;
      return 0;
    }
  }
  cout << Ans << endl;
  return 0;
}