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

#include <bits/stdc++.h>

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
  return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;
template <long long Mod = MOD> class Modint {
public:
  long long v;

  Modint(const Modint &x) { v = x.v; }
  explicit Modint(int x) {
    if (x < 0 or Mod <= x)
      x %= Mod;
    if (x < 0)
      x += Mod;
    v = x;
  }
  explicit Modint(long long x) {
    if (x < 0 or Mod <= x)
      x %= Mod;
    if (x < 0)
      x += Mod;
    v = x;
  }
  explicit Modint(unsigned x) {
    if (Mod <= x)
      x %= Mod;
    v = x;
  }
  explicit Modint(unsigned long long x) {
    if (Mod <= x)
      x %= Mod;
    v = x;
  }
  template <typename T> explicit Modint(T x) {
    if (x < 0 or Mod <= x)
      x %= Mod;
    v = x;
  }
  Modint() : v(0) {}
  long long get() const { return v; }
  Modint mpow(Modint &n) const { return mpow(n.v); }
  Modint mpow(long long n) const {
    long long i = 1, p = v;
    Modint ret(1);
    while (i <= n) {
      if (i & n)
        ret *= p;
      i = (i << 1);
      p = (p * p) % Mod;
    }
    return ret;
  }
  Modint operator-() const { return (v ? Modint(Mod - v) : Modint(0)); }
  explicit operator int() const { return v; }
  explicit operator long long() const { return v; }
  Modint &operator+=(const Modint &a) {
    v = (v + a.v);
    if (v >= Mod)
      v -= Mod;
    return *this;
  }
  Modint &operator-=(const Modint &a) {
    v = (v - a.v);
    if (v < 0)
      v += Mod;
    return *this;
  }
  Modint &operator*=(const Modint &a) {
    v = (v * a.v) % Mod;
    return *this;
  }
  Modint &operator/=(const Modint &a) {
    v = (v * a.mpow(Mod - 2).v) % Mod;
    return *this;
  }
  template <class T> Modint &operator+=(const T &a) {
    v = (v + Modint(a).v);
    if (v >= Mod)
      v -= Mod;
    return *this;
  }
  template <class T> Modint &operator-=(const T &a) {
    v = (v - Modint(a).v);
    if (v < 0)
      v += Mod;
    return *this;
  }
  template <class T> Modint &operator*=(const T &a) {
    v = (v * Modint(a).v) % Mod;
    return *this;
  }
  template <class T> Modint &operator/=(const T &a) {
    v = (v * Modint(a).mpow(Mod - 2).v) % Mod;
    return *this;
  }
  friend Modint operator+(const Modint &a, const Modint &b) {
    return Modint(a) += b;
  }
  friend Modint operator-(const Modint &a, const Modint &b) {
    return Modint(a) -= b;
  }
  friend Modint operator*(const Modint &a, const Modint &b) {
    return Modint(a) *= b;
  }
  friend Modint operator/(const Modint &a, const Modint &b) {
    return Modint(a) /= b;
  }
  template <class T, class U> friend Modint operator+(const T &a, const U &b) {
    return Modint(a) += Modint(b);
  }
  template <class T, class U> friend Modint operator-(const T &a, const U &b) {
    return Modint(a) -= Modint(b);
  }
  template <class T, class U> friend Modint operator*(const T &a, const U &b) {
    return Modint(a) *= Modint(b);
  }
  template <class T, class U> friend Modint operator/(const T &a, const U &b) {
    return Modint(a) /= Modint(b);
  }
};

template <long long M> ostream &operator<<(ostream &os, const Modint<M> m) {
  return os << m.v;
}
LL dp[5001][5001][2];
class Solver2 {
public:
  int n, a, b;

  Solver2(int n, int a, int b) : n(n), a(a), b(b){};

  void solve() {
    if (a < b)
      swap(a, b);
    dp[0][0][0] = LL(1);
    dp[2][1][0] = LL(MOD - 1);
    auto ans = Modint<>(0);
    for (int i = 0; i <= n; ++i) {
      if (i > 0)
        dp[i][1][0] += dp[i - 1][1][0];
      dp[i][1][0] %= MOD;
      for (int j = 0; j < a; ++j) {
        if (i + b <= n && j + b <= n)
          dp[i + b][j + b][1] += dp[i][j][0];
        if (i + 2 <= n)
          dp[i + 2][1][0] += dp[i][j][0];
        if (i + b + 1 <= n)
          dp[i + b + 1][1][0] -= dp[i][j][0];
        if (i + 1 <= n && j + 1 <= n)
          dp[i + 1][j + 1][0] += dp[i][j][0];
        if (i + 1 <= n && j + 1 <= n)
          dp[i + 1][j + 1][1] += dp[i][j][1];
        if (i + 1 <= n && j + 1 <= n)
          dp[i + 1][j + 1][0] += dp[i][j][1];

        if (i + b <= n && j + b <= n)
          dp[i + b][j + b][1] %= MOD;
        if (i + 2 <= n)
          dp[i + 2][1][0] %= MOD;
        if (i + b + 1 <= n)
          dp[i + b + 1][1][0] += MOD;
        if (i + b + 1 <= n)
          dp[i + b + 1][1][0] %= MOD;
        if (i + 1 <= n && j + 1 <= n)
          dp[i + 1][j + 1][0] %= MOD;
        if (i + 1 <= n && j + 1 <= n)
          dp[i + 1][j + 1][1] %= MOD;
      }
      auto cf = Modint<>(2).mpow(n - i);
      for (int j = a; j <= n; ++j) {
        ans += (dp[i][j][0] + dp[i][j][1]) * cf;
      }
    }
    cout << ans << endl;
  }
};
} // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0, a, b;
  std::cin >> n >> a >> b;

  Problem::Solver2 sol(n, a, b);
  sol.solve();
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
  return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;
template <long long Mod = MOD> class Modint {
public:
  long long v;

  Modint(const Modint &x) { v = x.v; }
  explicit Modint(int x) {
    if (x < 0 or Mod <= x)
      x %= Mod;
    if (x < 0)
      x += Mod;
    v = x;
  }
  explicit Modint(long long x) {
    if (x < 0 or Mod <= x)
      x %= Mod;
    if (x < 0)
      x += Mod;
    v = x;
  }
  explicit Modint(unsigned x) {
    if (Mod <= x)
      x %= Mod;
    v = x;
  }
  explicit Modint(unsigned long long x) {
    if (Mod <= x)
      x %= Mod;
    v = x;
  }
  template <typename T> explicit Modint(T x) {
    if (x < 0 or Mod <= x)
      x %= Mod;
    v = x;
  }
  Modint() : v(0) {}
  long long get() const { return v; }
  Modint mpow(Modint &n) const { return mpow(n.v); }
  Modint mpow(long long n) const {
    long long i = 1, p = v;
    Modint ret(1);
    while (i <= n) {
      if (i & n)
        ret *= p;
      i = (i << 1);
      p = (p * p) % Mod;
    }
    return ret;
  }
  Modint operator-() const { return (v ? Modint(Mod - v) : Modint(0)); }
  explicit operator int() const { return v; }
  explicit operator long long() const { return v; }
  Modint &operator+=(const Modint &a) {
    v = (v + a.v);
    if (v >= Mod)
      v -= Mod;
    return *this;
  }
  Modint &operator-=(const Modint &a) {
    v = (v - a.v);
    if (v < 0)
      v += Mod;
    return *this;
  }
  Modint &operator*=(const Modint &a) {
    v = (v * a.v) % Mod;
    return *this;
  }
  Modint &operator/=(const Modint &a) {
    v = (v * a.mpow(Mod - 2).v) % Mod;
    return *this;
  }
  template <class T> Modint &operator+=(const T &a) {
    v = (v + Modint(a).v);
    if (v >= Mod)
      v -= Mod;
    return *this;
  }
  template <class T> Modint &operator-=(const T &a) {
    v = (v - Modint(a).v);
    if (v < 0)
      v += Mod;
    return *this;
  }
  template <class T> Modint &operator*=(const T &a) {
    v = (v * Modint(a).v) % Mod;
    return *this;
  }
  template <class T> Modint &operator/=(const T &a) {
    v = (v * Modint(a).mpow(Mod - 2).v) % Mod;
    return *this;
  }
  friend Modint operator+(const Modint &a, const Modint &b) {
    return Modint(a) += b;
  }
  friend Modint operator-(const Modint &a, const Modint &b) {
    return Modint(a) -= b;
  }
  friend Modint operator*(const Modint &a, const Modint &b) {
    return Modint(a) *= b;
  }
  friend Modint operator/(const Modint &a, const Modint &b) {
    return Modint(a) /= b;
  }
  template <class T, class U> friend Modint operator+(const T &a, const U &b) {
    return Modint(a) += Modint(b);
  }
  template <class T, class U> friend Modint operator-(const T &a, const U &b) {
    return Modint(a) -= Modint(b);
  }
  template <class T, class U> friend Modint operator*(const T &a, const U &b) {
    return Modint(a) *= Modint(b);
  }
  template <class T, class U> friend Modint operator/(const T &a, const U &b) {
    return Modint(a) /= Modint(b);
  }
};

template <long long M> ostream &operator<<(ostream &os, const Modint<M> m) {
  return os << m.v;
}

class Solver2 {
public:
  int n, a, b;
  LL dp[5001][5001][2];

  Solver2(int n, int a, int b) : n(n), a(a), b(b){};

  void solve() {
    if (a < b)
      swap(a, b);
    dp[0][0][0] = LL(1);
    dp[2][1][0] = LL(MOD - 1);
    auto ans = Modint<>(0);
    for (int i = 0; i <= n; ++i) {
      if (i > 0)
        dp[i][1][0] += dp[i - 1][1][0];
      dp[i][1][0] %= MOD;
      for (int j = 0; j < a; ++j) {
        if (i + b <= n && j + b <= n)
          dp[i + b][j + b][1] += dp[i][j][0];
        if (i + 2 <= n)
          dp[i + 2][1][0] += dp[i][j][0];
        if (i + b + 1 <= n)
          dp[i + b + 1][1][0] -= dp[i][j][0];
        if (i + 1 <= n && j + 1 <= n)
          dp[i + 1][j + 1][0] += dp[i][j][0];
        if (i + 1 <= n && j + 1 <= n)
          dp[i + 1][j + 1][1] += dp[i][j][1];
        if (i + 1 <= n && j + 1 <= n)
          dp[i + 1][j + 1][0] += dp[i][j][1];

        if (i + b <= n && j + b <= n)
          dp[i + b][j + b][1] %= MOD;
        if (i + 2 <= n)
          dp[i + 2][1][0] %= MOD;
        if (i + b + 1 <= n)
          dp[i + b + 1][1][0] += MOD;
        if (i + b + 1 <= n)
          dp[i + b + 1][1][0] %= MOD;
        if (i + 1 <= n && j + 1 <= n)
          dp[i + 1][j + 1][0] %= MOD;
        if (i + 1 <= n && j + 1 <= n)
          dp[i + 1][j + 1][1] %= MOD;
      }
      auto cf = Modint<>(2).mpow(n - i);
      for (int j = a; j <= n; ++j) {
        ans += (dp[i][j][0] + dp[i][j][1]) * cf;
      }
    }
    cout << ans << endl;
  }
};
} // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0, a, b;
  std::cin >> n >> a >> b;

  Problem::Solver2 sol(n, a, b);
  sol.solve();
  return 0;
}