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

#include <bits/stdc++.h>

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}

template <int mod> struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(int64_t x) : x(x >= 0 ? x % mod : (mod - -x % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int64_t)x * p.x % mod;
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt &operator^=(int64_t p) {
    ModInt res = 1;
    for (; p; p >>= 1) {
      if (p & 1)
        res *= *this;
      *this *= *this;
    }
    return *this = res;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  ModInt operator^(int64_t p) const { return ModInt(*this) ^= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  explicit operator int() const { return x; }
  ModInt &operator=(const int p) {
    x = p;
    return *this;
  }
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    return ModInt(u);
  }
  friend std::ostream &operator<<(std::ostream &stream, const ModInt<mod> &p) {
    return stream << p.x;
  }
  friend std::istream &operator>>(std::istream &stream, ModInt<mod> &a) {
    int64_t x;
    stream >> x;
    a = ModInt<mod>(x);
    return stream;
  }
};
typedef ModInt<1000000007> mint;

template <int mod> struct MComb {
  using mint = ModInt<mod>;
  std::vector<mint> fact;
  std::vector<mint> inv;
  MComb(int n) { // O(n + log(mod))
    fact = std::vector<mint>(n + 1, 1);
    for (int i = 1; i <= n; i++)
      fact[i] = fact[i - 1] * mint(i);
    inv.resize(n + 1);
    inv[n] = fact[n] ^ (mod - 2);
    for (int i = n; i--;)
      inv[i] = inv[i + 1] * mint(i + 1);
  }
  mint ncr(int n, int r) { return fact[n] * inv[r] * inv[n - r]; }
  mint npr(int n, int r) { return fact[n] * inv[n - r]; }
  mint nhr(int n, int r) {
    assert(n + r - 1 < (int)fact.size());
    return ncr(n + r - 1, r);
  }
};

int main() {
  // TODO : a == b
  int n = ri();

  int a = ri(), b = ri();
  if (a > b)
    std::swap(a, b);

  static mint dp[5001][5001][2]; // pos, len, flag
  dp[0][0][1] = 1;

  mint diff[n + 1];
  mint cur = 0;

  mint res = 0;
  for (int i = 0; i <= n; i++) {
    cur += diff[i];
    dp[i][1][1] += cur;

    mint beki2 = mint(2) ^ (n - i);
    for (int j = b; j <= n; j++)
      res += beki2 * (dp[i][j][0] + dp[i][j][1]);
    if (i == n)
      break;
    for (int j = 0; j < b; j++) {
      if (dp[i][j][0] != 0) {
        dp[i + 1][j + 1][0] += dp[i][j][0];
        dp[i + 1][j + 1][1] += dp[i][j][0];
      }
      if (dp[i][j][1] != 0) {
        dp[i + 1][j + 1][1] += dp[i][j][1];
        if (i + a <= n)
          dp[i + a][j + a][0] += dp[i][j][1];
        // for (int k = i + 2; k <= n && k <= i + a; k++) dp[k][1][1] +=
        // dp[i][j][1];
        if (i + 2 <= n) {
          diff[i + 2] += dp[i][j][1];
          if (i + a + 1 <= n)
            diff[i + a + 1] -= dp[i][j][1];
        }
      }
    }
  }
  /*
  for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) for (int k = 0; k <
  2; k++) if (dp[i][j][k] != 0) { std::cerr << "pos:" << i << " len:" << j << "
  next:" << k << " : " << dp[i][j][k] << std::endl;
  }*/
  std::cout << res << std::endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}

template <int mod> struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(int64_t x) : x(x >= 0 ? x % mod : (mod - -x % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int64_t)x * p.x % mod;
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt &operator^=(int64_t p) {
    ModInt res = 1;
    for (; p; p >>= 1) {
      if (p & 1)
        res *= *this;
      *this *= *this;
    }
    return *this = res;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  ModInt operator^(int64_t p) const { return ModInt(*this) ^= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  explicit operator int() const { return x; }
  ModInt &operator=(const int p) {
    x = p;
    return *this;
  }
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    return ModInt(u);
  }
  friend std::ostream &operator<<(std::ostream &stream, const ModInt<mod> &p) {
    return stream << p.x;
  }
  friend std::istream &operator>>(std::istream &stream, ModInt<mod> &a) {
    int64_t x;
    stream >> x;
    a = ModInt<mod>(x);
    return stream;
  }
};
typedef ModInt<1000000007> mint;

template <int mod> struct MComb {
  using mint = ModInt<mod>;
  std::vector<mint> fact;
  std::vector<mint> inv;
  MComb(int n) { // O(n + log(mod))
    fact = std::vector<mint>(n + 1, 1);
    for (int i = 1; i <= n; i++)
      fact[i] = fact[i - 1] * mint(i);
    inv.resize(n + 1);
    inv[n] = fact[n] ^ (mod - 2);
    for (int i = n; i--;)
      inv[i] = inv[i + 1] * mint(i + 1);
  }
  mint ncr(int n, int r) { return fact[n] * inv[r] * inv[n - r]; }
  mint npr(int n, int r) { return fact[n] * inv[n - r]; }
  mint nhr(int n, int r) {
    assert(n + r - 1 < (int)fact.size());
    return ncr(n + r - 1, r);
  }
};

int main() {
  // TODO : a == b
  int n = ri();

  int a = ri(), b = ri();
  if (a > b)
    std::swap(a, b);

  static mint dp[5001][5001][2]; // pos, len, flag
  dp[0][0][1] = 1;

  mint diff[n + 1];
  mint cur = 0;

  mint res = 0;
  for (int i = 0; i <= n; i++) {
    cur += diff[i];
    dp[i][1][1] += cur;

    mint beki2 = mint(2) ^ (n - i);
    for (int j = b; j <= n; j++)
      res += beki2 * (dp[i][j][0] + dp[i][j][1]);
    if (i == n)
      break;
    for (int j = 0; j < b; j++) {
      if (dp[i][j] == 0)
        continue;
      dp[i + 1][j + 1][0] += dp[i][j][0];
      dp[i + 1][j + 1][1] += dp[i][j][0];
      dp[i + 1][j + 1][1] += dp[i][j][1];
      if (i + a <= n)
        dp[i + a][j + a][0] += dp[i][j][1];
      // for (int k = i + 2; k <= n && k <= i + a; k++) dp[k][1][1] +=
      // dp[i][j][1];
      if (i + 2 <= n) {
        diff[i + 2] += dp[i][j][1];
        if (i + a + 1 <= n)
          diff[i + a + 1] -= dp[i][j][1];
      }
    }
  }
  /*
  for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) for (int k = 0; k <
  2; k++) if (dp[i][j][k] != 0) { std::cerr << "pos:" << i << " len:" << j << "
  next:" << k << " : " << dp[i][j][k] << std::endl;
  }*/
  std::cout << res << std::endl;
  return 0;
}