Problem p02653 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) static_cast<int>((x).size())

using LL = long long;
template <class T, size_t D> struct vec : vector<vec<T, D - 1>> {
  template <class... Args>
  vec(size_t n = 0, Args... args)
      : vector<vec<T, D - 1>>(n, vec<T, D - 1>(args...)) {}
};
template <class T> struct vec<T, 1> : vector<T> {
  template <class... Args> vec(Args... args) : vector<T>(args...) {}
};

template <class T> inline bool asMn(T &a, const T &b) {
  return a > b ? a = b, true : false;
}
template <class T> inline bool asMx(T &a, const T &b) {
  return a < b ? a = b, true : false;
}
inline int nex(int i, int n) { return i == n - 1 ? 0 : i + 1; }
inline int pre(int i, int n) { return !i ? n - 1 : i - 1; }

const int mod = 1e9 + 7;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

struct ModInt {
  int a;
  ModInt(int _a = 0) : a(_a) {}
  friend istream &operator>>(istream &in, ModInt &_) {
    in >> _.a;
    return in;
  }
  friend ostream &operator<<(ostream &out, const ModInt &_) {
    out << _.a;
    return out;
  }

  bool operator<(const ModInt &_) const { return a < _.a; }
  bool operator>(const ModInt &_) const { return a > _.a; }
  bool operator<=(const ModInt &_) const { return a <= _.a; }
  bool operator>=(const ModInt &_) const { return a >= _.a; }
  bool operator==(const ModInt &_) const { return a == _.a; }
  bool operator!=(const ModInt &_) const { return a != _.a; }
  bool operator!() const { return !a; }

  ModInt operator+(const ModInt &_) const {
    int ret = a + _.a;
    return ret < mod ? ModInt(ret) : ModInt(ret - mod);
  }
  ModInt operator-(const ModInt &_) const { return *this + ModInt(mod - _.a); }
  ModInt operator*(const ModInt &_) const {
    return ModInt((int)((LL)a * _.a % mod));
  }
  ModInt operator/(const ModInt &_) const { return *this * _.binPow(mod - 2); }
  ModInt &operator+=(const ModInt &_) { return *this = *this + _; }
  ModInt &operator-=(const ModInt &_) { return *this = *this - _; }
  ModInt &operator*=(const ModInt &_) { return *this = *this * _; }
  ModInt &operator/=(const ModInt &_) { return *this = *this / _; }
  ModInt &operator++() { return *this = *this + ModInt(1); }
  ModInt &operator--() { return *this = *this - ModInt(1); }
  ModInt operator-() const { return ModInt() - *this; }

  template <class T> ModInt binPow(T exponent) const {
    ModInt ret(1), c = *this;
    for (; exponent; exponent >>= 1, c *= c)
      if (exponent & 1)
        ret *= c;
    return ret;
  }
};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, a, b;
  cin >> n >> a >> b;
  if (a > b) {
    swap(a, b);
  }

  vec<ModInt, 1> pw2(n + 1);
  pw2[0] = ModInt(1);
  for (int i = 1; i < sz(pw2); ++i) {
    pw2[i] = pw2[i - 1] + pw2[i - 1];
  }

  ModInt ans;

  array<array<array<ModInt, 2>, 5001>, 5001> f;
  vec<ModInt, 1> g(n + 1);
  f[0][0][1] = g[0] = ModInt(1);
  for (int i = 1; i <= n; ++i) {
    f[i][1][1] +=
        (i >= 2 ? g[i - 2] : ModInt()) - (i >= a + 1 ? g[i - a - 1] : ModInt());

    for (int j = 0; j <= n; ++j) {
      if (i >= a && j >= a) {
        f[i][j][0] += f[i - a][j - a][1];
      }

      if (j) {
        f[i][j][0] += f[i - 1][j - 1][0];

        f[i][j][1] += f[i - 1][j - 1][0];
        f[i][j][1] += f[i - 1][j - 1][1];
      }
    }

    for (int j = 0; j < b; ++j) {
      g[i] += f[i][j][1];
    }
    g[i] += g[i - 1];
  }

  for (int i = 1; i < n; ++i) {
    ModInt sum;
    for (int j = b; j <= n; ++j) {
      sum += f[i][j][1];
    }

    for (int j = i + 1; j <= n && j - i < a; ++j) {
      ans += sum * pw2[max(0, n - j - 1)];
    }
  }
  for (int j = b; j <= n; ++j) {
    ans += f[n][j][0] + f[n][j][1];
  }

  cout << ans << '\\n';

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) static_cast<int>((x).size())

using LL = long long;
template <class T, size_t D> struct vec : vector<vec<T, D - 1>> {
  template <class... Args>
  vec(size_t n = 0, Args... args)
      : vector<vec<T, D - 1>>(n, vec<T, D - 1>(args...)) {}
};
template <class T> struct vec<T, 1> : vector<T> {
  template <class... Args> vec(Args... args) : vector<T>(args...) {}
};

template <class T> inline bool asMn(T &a, const T &b) {
  return a > b ? a = b, true : false;
}
template <class T> inline bool asMx(T &a, const T &b) {
  return a < b ? a = b, true : false;
}
inline int nex(int i, int n) { return i == n - 1 ? 0 : i + 1; }
inline int pre(int i, int n) { return !i ? n - 1 : i - 1; }

const int mod = 1e9 + 7;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

struct ModInt {
  int a;
  ModInt(int _a = 0) : a(_a) {}
  friend istream &operator>>(istream &in, ModInt &_) {
    in >> _.a;
    return in;
  }
  friend ostream &operator<<(ostream &out, const ModInt &_) {
    out << _.a;
    return out;
  }

  bool operator<(const ModInt &_) const { return a < _.a; }
  bool operator>(const ModInt &_) const { return a > _.a; }
  bool operator<=(const ModInt &_) const { return a <= _.a; }
  bool operator>=(const ModInt &_) const { return a >= _.a; }
  bool operator==(const ModInt &_) const { return a == _.a; }
  bool operator!=(const ModInt &_) const { return a != _.a; }
  bool operator!() const { return !a; }

  ModInt operator+(const ModInt &_) const {
    int ret = a + _.a;
    return ret < mod ? ModInt(ret) : ModInt(ret - mod);
  }
  ModInt operator-(const ModInt &_) const { return *this + ModInt(mod - _.a); }
  ModInt operator*(const ModInt &_) const {
    return ModInt((int)((LL)a * _.a % mod));
  }
  ModInt operator/(const ModInt &_) const { return *this * _.binPow(mod - 2); }
  ModInt &operator+=(const ModInt &_) { return *this = *this + _; }
  ModInt &operator-=(const ModInt &_) { return *this = *this - _; }
  ModInt &operator*=(const ModInt &_) { return *this = *this * _; }
  ModInt &operator/=(const ModInt &_) { return *this = *this / _; }
  ModInt &operator++() { return *this = *this + ModInt(1); }
  ModInt &operator--() { return *this = *this - ModInt(1); }
  ModInt operator-() const { return ModInt() - *this; }

  template <class T> ModInt binPow(T exponent) const {
    ModInt ret(1), c = *this;
    for (; exponent; exponent >>= 1, c *= c)
      if (exponent & 1)
        ret *= c;
    return ret;
  }
};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, a, b;
  cin >> n >> a >> b;
  if (a > b) {
    swap(a, b);
  }

  vec<ModInt, 3> f(n + 1, n + 1, 2);
  vec<ModInt, 1> g(n + 1);
  f[0][0][1] = g[0] = ModInt(1);
  for (int i = 1; i <= n; ++i) {
    f[i][1][1] +=
        (i >= 2 ? g[i - 2] : ModInt()) - (i >= a + 1 ? g[i - a - 1] : ModInt());

    for (int j = 0; j <= n; ++j) {
      if (i >= a && j >= a) {
        f[i][j][0] += f[i - a][j - a][1];
      }

      if (j) {
        f[i][j][0] += f[i - 1][j - 1][0];

        f[i][j][1] += f[i - 1][j - 1][0];
        f[i][j][1] += f[i - 1][j - 1][1];
      }
    }

    for (int j = 0; j < b; ++j) {
      g[i] += f[i][j][1];
    }
    g[i] += g[i - 1];
  }

  vec<ModInt, 1> pw2(n + 1);
  pw2[0] = ModInt(1);
  for (int i = 1; i < sz(pw2); ++i) {
    pw2[i] = pw2[i - 1] + pw2[i - 1];
  }

  ModInt ans;
  for (int i = 1; i < n; ++i) {
    ModInt sum;
    for (int j = b; j <= n; ++j) {
      sum += f[i][j][1];
    }

    for (int j = i + 1; j <= n && j - i < a; ++j) {
      ans += sum * pw2[max(0, n - j - 1)];
    }
  }
  for (int j = b; j <= n; ++j) {
    ans += f[n][j][0] + f[n][j][1];
  }

  cout << ans << '\\n';

  return 0;
}