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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(), (x).end()

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <unsigned int mod> class ModInt {
private:
  unsigned int v;
  static unsigned int norm(const unsigned int &x) {
    return x < mod ? x : x - mod;
  }
  static ModInt make(const unsigned int &x) {
    ModInt m;
    return m.v = x, m;
  }
  static ModInt inv(const ModInt &x) { return make(inverse(x.v, mod)); }
  static unsigned int inverse(int a, int m) {
    int u[] = {a, 1, 0}, v[] = {m, 0, 1}, t;
    while (*v) {
      t = *u / *v;
      swap(u[0] -= t * v[0], v[0]), swap(u[1] -= t * v[1], v[1]),
          swap(u[2] -= t * v[2], v[2]);
    }
    return (u[1] % m + m) % m;
  }

public:
  ModInt() : v{0} {}
  ModInt(const long long val) : v{norm(val % mod + mod)} {}
  ModInt(const ModInt<mod> &n) : v{n()} {}
  explicit operator bool() const noexcept { return v != 0; }
  bool operator!() const noexcept { return !static_cast<bool>(*this); }
  ModInt &operator=(const ModInt &n) { return v = n(), (*this); }
  ModInt &operator=(const long long val) {
    return v = norm(val % mod + mod), (*this);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { return v == 0 ? make(0) : make(mod - v); }
  ModInt operator+(const ModInt &val) const { return make(norm(v + val())); }
  ModInt operator-(const ModInt &val) const {
    return make(norm(v + mod - val()));
  }
  ModInt operator*(const ModInt &val) const {
    return make((long long)v * val() % mod);
  }
  ModInt operator/(const ModInt &val) const { return *this * inv(val); }
  ModInt &operator+=(const ModInt &val) { return *this = *this + val; }
  ModInt &operator-=(const ModInt &val) { return *this = *this - val; }
  ModInt &operator*=(const ModInt &val) { return *this = *this * val; }
  ModInt &operator/=(const ModInt &val) { return *this = *this / val; }
  ModInt operator+(const long long val) const { return ModInt{v + val}; }
  ModInt operator-(const long long val) const { return ModInt{v - val}; }
  ModInt operator*(const long long val) const {
    return ModInt{(long long)v * (val % mod)};
  }
  ModInt operator/(const long long val) const {
    return ModInt{(long long)v * inv(val)};
  }
  ModInt &operator+=(const long long val) { return *this = *this + val; }
  ModInt &operator-=(const long long val) { return *this = *this - val; }
  ModInt &operator*=(const long long val) { return *this = *this * val; }
  ModInt &operator/=(const long long val) { return *this = *this / val; }
  bool operator==(const ModInt &val) const { return v == val.v; }
  bool operator!=(const ModInt &val) const { return !(*this == val); }
  bool operator==(const long long val) const {
    return v == norm(val % mod + mod);
  }
  bool operator!=(const long long val) const { return !(*this == val); }
  unsigned int operator()() const { return v; }
  friend ModInt operator+(const long long val, const ModInt &n) {
    return n + val;
  }
  friend ModInt operator-(const long long val, const ModInt &n) {
    return ModInt{val - n()};
  }
  friend ModInt operator*(const long long val, const ModInt &n) {
    return n * val;
  }
  friend ModInt operator/(const long long val, const ModInt &n) {
    return ModInt{val} / n;
  }
  friend bool operator==(const long long val, const ModInt &n) {
    return n == val;
  }
  friend bool operator!=(const long long val, const ModInt &n) {
    return !(val == n);
  }
  friend istream &operator>>(istream &is, ModInt &n) {
    unsigned int v;
    return is >> v, n = v, is;
  }
  friend ostream &operator<<(ostream &os, const ModInt &n) {
    return (os << n());
  }
  friend ModInt mod_pow(ModInt x, long long n) {
    ModInt ans = 1;
    while (n) {
      if (n & 1)
        ans *= x;
      x *= x, n >>= 1;
    }
    return ans;
  }
};

#define MOD 998244353
using mod = ModInt<MOD>;
mod dp[301][301];
mod dp2[301][301];
int main() {
  string s;
  cin >> s;
  int K;
  cin >> K;
  vector<int> a;
  int n = s.size();
  int c = 0;
  rep(i, n) {
    if (s[i] == '0') {
      a.push_back(c);
      c = 0;
    } else {
      c++;
    }
  }
  a.push_back(c);
  vector<int> b;
  int m = a.size();
  b.resize(m);
  for (int i = 0; i < m; i++) {
    b[i] += a[i];
    if (i != 0)
      b[i] += b[i - 1];
  }
  int sm = b[m - 1];
  dp[0][0] = 1;
  rep(i, m) {
    int pa = 0;
    if (i != 0)
      pa = b[i - 1];
    for (int j = pa; j <= sm; j++) {                       // imamade sum : j
      for (int k = j - pa; k <= j; k++) {                  // diff sum : k
        for (int x = max(0, b[i] - j); x <= sm - j; x++) { // x add
          int nxt1 = j + x;
          int diff = max(0, x - a[i]);
          if (k + diff > K || k + diff >= 301)
            break;
          dp2[nxt1][k + diff] += dp[j][k];
        }
      }
    }
    for (int j = b[i]; j <= sm; j++) {
      for (int k = j - b[i]; k <= j; k++) {
        dp[j][k] = dp2[j][k];
        dp2[j][k] = 0;
      }
    }
  }
  mod res = 0;
  for (int k = 0; k <= min(sm, K); k++) {
    res += dp[sm][k];
  }
  cout << res << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(), (x).end()

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <unsigned int mod> class ModInt {
private:
  unsigned int v;
  static unsigned int norm(const unsigned int &x) {
    return x < mod ? x : x - mod;
  }
  static ModInt make(const unsigned int &x) {
    ModInt m;
    return m.v = x, m;
  }
  static ModInt inv(const ModInt &x) { return make(inverse(x.v, mod)); }
  static unsigned int inverse(int a, int m) {
    int u[] = {a, 1, 0}, v[] = {m, 0, 1}, t;
    while (*v) {
      t = *u / *v;
      swap(u[0] -= t * v[0], v[0]), swap(u[1] -= t * v[1], v[1]),
          swap(u[2] -= t * v[2], v[2]);
    }
    return (u[1] % m + m) % m;
  }

public:
  ModInt() : v{0} {}
  ModInt(const long long val) : v{norm(val % mod + mod)} {}
  ModInt(const ModInt<mod> &n) : v{n()} {}
  explicit operator bool() const noexcept { return v != 0; }
  bool operator!() const noexcept { return !static_cast<bool>(*this); }
  ModInt &operator=(const ModInt &n) { return v = n(), (*this); }
  ModInt &operator=(const long long val) {
    return v = norm(val % mod + mod), (*this);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { return v == 0 ? make(0) : make(mod - v); }
  ModInt operator+(const ModInt &val) const { return make(norm(v + val())); }
  ModInt operator-(const ModInt &val) const {
    return make(norm(v + mod - val()));
  }
  ModInt operator*(const ModInt &val) const {
    return make((long long)v * val() % mod);
  }
  ModInt operator/(const ModInt &val) const { return *this * inv(val); }
  ModInt &operator+=(const ModInt &val) { return *this = *this + val; }
  ModInt &operator-=(const ModInt &val) { return *this = *this - val; }
  ModInt &operator*=(const ModInt &val) { return *this = *this * val; }
  ModInt &operator/=(const ModInt &val) { return *this = *this / val; }
  ModInt operator+(const long long val) const { return ModInt{v + val}; }
  ModInt operator-(const long long val) const { return ModInt{v - val}; }
  ModInt operator*(const long long val) const {
    return ModInt{(long long)v * (val % mod)};
  }
  ModInt operator/(const long long val) const {
    return ModInt{(long long)v * inv(val)};
  }
  ModInt &operator+=(const long long val) { return *this = *this + val; }
  ModInt &operator-=(const long long val) { return *this = *this - val; }
  ModInt &operator*=(const long long val) { return *this = *this * val; }
  ModInt &operator/=(const long long val) { return *this = *this / val; }
  bool operator==(const ModInt &val) const { return v == val.v; }
  bool operator!=(const ModInt &val) const { return !(*this == val); }
  bool operator==(const long long val) const {
    return v == norm(val % mod + mod);
  }
  bool operator!=(const long long val) const { return !(*this == val); }
  unsigned int operator()() const { return v; }
  friend ModInt operator+(const long long val, const ModInt &n) {
    return n + val;
  }
  friend ModInt operator-(const long long val, const ModInt &n) {
    return ModInt{val - n()};
  }
  friend ModInt operator*(const long long val, const ModInt &n) {
    return n * val;
  }
  friend ModInt operator/(const long long val, const ModInt &n) {
    return ModInt{val} / n;
  }
  friend bool operator==(const long long val, const ModInt &n) {
    return n == val;
  }
  friend bool operator!=(const long long val, const ModInt &n) {
    return !(val == n);
  }
  friend istream &operator>>(istream &is, ModInt &n) {
    unsigned int v;
    return is >> v, n = v, is;
  }
  friend ostream &operator<<(ostream &os, const ModInt &n) {
    return (os << n());
  }
  friend ModInt mod_pow(ModInt x, long long n) {
    ModInt ans = 1;
    while (n) {
      if (n & 1)
        ans *= x;
      x *= x, n >>= 1;
    }
    return ans;
  }
};

#define MOD 998244353
using mod = ModInt<MOD>;
mod dp[301][301];
mod dp2[301][301];
int main() {
  string s;
  cin >> s;
  int K;
  cin >> K;
  vector<int> a;
  int n = s.size();
  int c = 0;
  rep(i, n) {
    if (s[i] == '0') {
      a.push_back(c);
      c = 0;
    } else {
      c++;
    }
  }
  a.push_back(c);
  vector<int> b;
  int m = a.size();
  b.resize(m);
  for (int i = 0; i < m; i++) {
    b[i] += a[i];
    if (i != 0)
      b[i] += b[i - 1];
  }
  int sm = b[m - 1];
  dp[0][0] = 1;
  rep(i, m) {
    int pa = 0;
    if (i != 0)
      pa = b[i - 1];
    for (int j = pa; j <= sm; j++) {                       // imamade sum : j
      for (int k = j - pa; k <= j; k++) {                  // diff sum : k
        for (int x = max(0, b[i] - j); x <= sm - j; x++) { // x add
          int nxt1 = j + x;
          int diff = max(0, x - a[i]);
          if (k + diff > K || k + diff >= 301)
            break;
          dp2[nxt1][k + diff] += dp[j][k];
        }
      }
    }
    for (int j = b[i]; j <= sm; j++) {
      for (int k = j - b[i]; k <= j; k++) {
        dp[j][k] = dp2[j][k];
        dp2[j][k] = 0;
      }
    }
  }
  mod res = 0;
  for (int k = 0; k <= K; k++) {
    res += dp[sm][k];
  }
  cout << res << "\\n";
  return 0;
}