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

#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <int MOD> struct Fp {
  ll val;
  constexpr Fp(ll v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    ll a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      ll t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, ll n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};
constexpr ll MOD = 998244353;
using mint = Fp<MOD>;

#define pb push_back
#define mp make_pair
#define mt make_tuple

// ll solve_slow(ll A, ll B, ll C, ll D) {
//     int y_diff = C - A;
//     int x_diff = D - B;
//     vector<int> shff;
//     REP(y, y_diff) shff.pb(0);
//     REP(x, x_diff) shff.pb(1);

//     do {
//         set<vector<vector<int>>> cells_vec;
//         vector<vector<int>> cells;
//         REP(a, A) {
//             vector<int> line;
//             REP(b, B) {
//                 line.pb(9);
//             }
//             cells.pb(line);
//         }
//         cells_vec.insert(cells);

//         for(auto s: shff) {
//             set<vector<vector<int>>> next_cells_vec;
//             if (s == 0) {
//                 for (auto& cells: cells_vec) {
//                     auto width = SIZE(cells[0]);

//                     REP(w, width) {
//                         vector<vector<int>> next_cells = cells;
//                         vector<int> line;
//                         REP(w2, width) {
//                             if (w == w2) line.pb(1);
//                             else line.pb(0);
//                         }
//                         next_cells.pb(line);
//                         next_cells_vec.insert(next_cells);
//                     }
//                 }
//             }
//             else {

//             }
//             cells_vec =
//         }

//     } while(next_permutation(ALL(shff)));

//     return 777;
// }

vector<pair<mint, mint>> cache(3000 * 3000, mp(0, 0));
vector<int> seen(3000 * 3000);

int to_index(ll C, ll D) {
  --C;
  --D;
  return C * 3000 + D;
}

pair<mint, mint> solve_fast(ll A, ll B, ll C, ll D) {
  if (seen[to_index(C, D)] != 0) {
    return cache[to_index(C, D)];
  }

  mint ans = 0;
  mint leftnum = 0;

  if (C == A && B == D) {
    ans = 1;
    leftnum = 0;
  }
  // 横に伸ばす
  else if (C == A) {
    auto ret = solve_fast(A, B, C, D - 1);
    ans = ret.first * A;
    leftnum = ans;
  }
  // 縦に伸ばす
  else if (D == B) {
    auto ret = solve_fast(A, B, C - 1, D);
    ans = ret.first * B;
    leftnum = ret.first; // ?
  } else {
    // 横に伸ばす
    auto ret1 = solve_fast(A, B, C, D - 1);
    // 縦に伸ばす
    auto ret2 = solve_fast(A, B, C - 1, D);

    mint ans1 = ret1.first * C;
    mint ans2 = ret2.first * D;

    ans = ans1 + ans2;
    ans -= ret2.second * (D - 1); // ?
    // cout << "ans = " << ans1 << " + " << ans2
    //      << " - " << ret2.second << " = " << ans << endl;

    leftnum = ans1; // ?
  }

  // cout << "dp(" << A << "," << B << "," << C << "," << D << ") = "
  //      << ans << "," << leftnum << endl;

  cache[to_index(C, D)] = mp(ans, leftnum);
  seen[to_index(C, D)] = 1;

  return mp(ans, leftnum);
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  // if (1) {
  //     cache.reset(0);

  //     int size = 1000;
  //     // int size = 100;
  //     FOR(a, 1, size) {
  //         FOR(b, 1, size) {
  //             FOR(c, a, size + 1) {
  //                 FOR(d, b, size + 1) {
  //                     auto ret = solve_fast(a, b, c, d);
  //                     cout << a << ", " << b << ", " << c << ", " <<
  //                         d << ": " << ret.first << endl;
  //                     if (ret.first == 0) {
  //                         return 0;
  //                     }
  //                 }
  //             }
  //         }
  //     }
  // }

  ll A, B, C, D;
  cin >> A >> B >> C >> D;

  auto ret = solve_fast(A, B, C, D);
  cout << ret.first << endl;

  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <int MOD> struct Fp {
  ll val;
  constexpr Fp(ll v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    ll a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      ll t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, ll n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};
constexpr ll MOD = 998244353;
using mint = Fp<MOD>;

#define pb push_back
#define mp make_pair
#define mt make_tuple

// ll solve_slow(ll A, ll B, ll C, ll D) {
//     int y_diff = C - A;
//     int x_diff = D - B;
//     vector<int> shff;
//     REP(y, y_diff) shff.pb(0);
//     REP(x, x_diff) shff.pb(1);

//     do {
//         set<vector<vector<int>>> cells_vec;
//         vector<vector<int>> cells;
//         REP(a, A) {
//             vector<int> line;
//             REP(b, B) {
//                 line.pb(9);
//             }
//             cells.pb(line);
//         }
//         cells_vec.insert(cells);

//         for(auto s: shff) {
//             set<vector<vector<int>>> next_cells_vec;
//             if (s == 0) {
//                 for (auto& cells: cells_vec) {
//                     auto width = SIZE(cells[0]);

//                     REP(w, width) {
//                         vector<vector<int>> next_cells = cells;
//                         vector<int> line;
//                         REP(w2, width) {
//                             if (w == w2) line.pb(1);
//                             else line.pb(0);
//                         }
//                         next_cells.pb(line);
//                         next_cells_vec.insert(next_cells);
//                     }
//                 }
//             }
//             else {

//             }
//             cells_vec =
//         }

//     } while(next_permutation(ALL(shff)));

//     return 777;
// }

vector<pair<mint, mint>> cache(3000 * 3000, mp(0, 0));
vector<int> seen(3000 * 300);

int to_index(ll C, ll D) {
  --C;
  --D;
  return C * 3000 + D;
}

pair<mint, mint> solve_fast(ll A, ll B, ll C, ll D) {
  if (seen[to_index(C, D)] != 0) {
    return cache[to_index(C, D)];
  }

  mint ans = 0;
  mint leftnum = 0;

  if (C == A && B == D) {
    ans = 1;
    leftnum = 0;
  }
  // 横に伸ばす
  else if (C == A) {
    auto ret = solve_fast(A, B, C, D - 1);
    ans = ret.first * A;
    leftnum = ans;
  }
  // 縦に伸ばす
  else if (D == B) {
    auto ret = solve_fast(A, B, C - 1, D);
    ans = ret.first * B;
    leftnum = ret.first; // ?
  } else {
    // 横に伸ばす
    auto ret1 = solve_fast(A, B, C, D - 1);
    // 縦に伸ばす
    auto ret2 = solve_fast(A, B, C - 1, D);

    mint ans1 = ret1.first * C;
    mint ans2 = ret2.first * D;

    ans = ans1 + ans2;
    ans -= ret2.second * (D - 1); // ?
    // cout << "ans = " << ans1 << " + " << ans2
    //      << " - " << ret2.second << " = " << ans << endl;

    leftnum = ans1; // ?
  }

  // cout << "dp(" << A << "," << B << "," << C << "," << D << ") = "
  //      << ans << "," << leftnum << endl;

  cache[to_index(C, D)] = mp(ans, leftnum);
  seen[to_index(C, D)] = 1;

  return mp(ans, leftnum);
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll A, B, C, D;
  cin >> A >> B >> C >> D;

  auto ret = solve_fast(A, B, C, D);
  cout << ret.first << endl;

  return 0;
}