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

#define ENABLE_DEBUG 0
// Kana's kitchen {{{
#include <bits/stdc++.h>
#define ALL(v) std::begin(v), std::end(v)
#define REP(i, k) for (std::decay_t<decltype(k)> i = 0; i < k; i++)
#define LOOP(k) REP(ngtkana_is_a_genius, k)

using i32 = std::int_least32_t;
using i64 = std::int_least64_t;
using u32 = std::uint_least32_t;
using u64 = std::uint_least64_t;
using usize = std::size_t;

template <class T, class U> using pair = std::pair<U, T>;
template <class T> using diag_pair = std::pair<T, T>;
template <class... Args> using tuple = std::tuple<Args...>;
template <class T> using vec = std::vector<T>;
template <class T> using numr = std::numeric_limits<T>;

#ifdef NGTKANA
#include <debug.hpp>
#else
#define DEBUG(...) (void)0
#endif
/*}}}*/
// mint{{{
template <class ModType> struct modint {
  using value_type = typename ModType::value_type;
  using mint = modint<ModType>;
  using mod_type = ModType;

  static value_type mod() { return ModType::value; }

private:
  static value_type inverse(value_type x) {
    value_type y = 1, u = mod(), v = 0;
    while (x) {
      value_type q = u / x;
      u -= q * x;
      std::swap(x, u);
      v -= q * y;
      std::swap(y, v);
    }
    assert(x == 0 && std::abs(y) == mod() && std::abs(u) == 1 &&
           std::abs(v) < mod());
    return v < 0 ? v + mod() : v;
  }

public:
  // the member variable
  value_type value;

  // constructors
  modint() = default;
  modint(modint const &) = default;
  modint(modint &&) = default;
  modint &operator=(modint const &) = default;
  modint &operator=(modint &&) = default;
  ~modint() = default;

  template <class T>
  modint(T t)
      : value([t]() mutable {
          if (t <= -static_cast<T>(mod()) || static_cast<T>(mod()) <= t)
            t %= mod();
          return t < 0 ? t + mod() : t;
        }()) {}

  // operators
  mint &operator+=(mint y) {
    value += y.value;
    if (mod() <= value)
      value -= mod();
    return *this;
  }

  mint &operator-=(mint y) {
    value -= y.value;
    if (value < 0)
      value += mod();
    return *this;
  }

  mint &operator*=(mint y) {
    value = (long long)value * y.value % mod();
    return *this;
  }

  mint &operator/=(mint y) {
    value = (long long)value * inverse(y.value) % mod();
    return *this;
  }

  mint &operator++() { return *this += 1; }
  mint &operator--() { return *this -= 1; }
  mint operator++(int) {
    mint this_ = *this;
    ++*this;
    return this_;
  }
  mint operator--(int) {
    mint this_ = *this;
    --*this;
    return this_;
  }
  mint operator-() const { return 0 - *this; }

  // static member functions
  static mint inv(mint x) { return inverse(x.value); }

  static mint m1pow(long long y) { return y % 2 ? -1 : 1; }

  static mint pow(mint x, unsigned long long y) {
    mint ans = 1;
    for (; y; y >>= 1) {
      if (y & 1ull)
        ans *= x;
      x *= x;
    }
    return ans;
  }

  // non-member functions
  mint &add_assign(mint y) { return *this += y; }
  mint &sub_assign(mint y) { return *this -= y; }
  mint &mul_assign(mint y) { return *this *= y; }
  mint &div_assign(mint y) { return *this /= y; }
  mint &inv_assign() { return *this = inv(*this); }
  mint &pow_assign(unsigned long long y) { return *this = pow(*this, y); }

  mint add(mint y) const {
    mint ans = *this;
    return ans.add_assign(y);
  }
  mint sub(mint y) const {
    mint ans = *this;
    return ans.sub_assign(y);
  }
  mint mul(mint y) const {
    mint ans = *this;
    return ans.mul_assign(y);
  }
  mint div(mint y) const {
    mint ans = *this;
    return ans.div_assign(y);
  }
  mint inv() const {
    mint ans = *this;
    return ans.inv_assign();
  }
  mint pow(unsigned long long y) const { return pow(*this, y); }
  mint square(mint x) const { return *this * *this; }
  mint cube(mint x) const { return *this * *this * *this; }

  template <class F> mint map(F const &f) {
    value = f(value);
    return *this;
  }
};

template <class T> std::istream &operator>>(std::istream &is, modint<T> &x) {
  typename modint<T>::value_type y;
  is >> y;
  x = modint<T>{y};
  return is;
}
template <class T> std::ostream &operator<<(std::ostream &os, modint<T> x) {
  return os << x.value;
}

template <class T> modint<T> operator+(modint<T> x, modint<T> y) {
  return x += y;
}
template <class T> modint<T> operator-(modint<T> x, modint<T> y) {
  return x -= y;
}
template <class T> modint<T> operator*(modint<T> x, modint<T> y) {
  return x *= y;
}
template <class T> modint<T> operator/(modint<T> x, modint<T> y) {
  return x /= y;
}
template <class T> bool operator==(modint<T> x, modint<T> y) {
  return x.value == y.value;
}
template <class T> bool operator!=(modint<T> x, modint<T> y) {
  return x.value != y.value;
}

template <class T, class U> modint<T> operator+(modint<T> x, U y) {
  return x + modint<T>(y);
}
template <class T, class U> modint<T> operator-(modint<T> x, U y) {
  return x - modint<T>(y);
}
template <class T, class U> modint<T> operator*(modint<T> x, U y) {
  return x * modint<T>(y);
}
template <class T, class U> modint<T> operator/(modint<T> x, U y) {
  return x / modint<T>(y);
}
template <class T, class U> bool operator==(modint<T> x, U y) {
  return x == modint<T>(y);
}
template <class T, class U> bool operator!=(modint<T> x, U y) {
  return x != modint<T>(y);
}

template <class T, class U> modint<T> operator+(U x, modint<T> y) {
  return modint<T>(x) + y;
}
template <class T, class U> modint<T> operator-(U x, modint<T> y) {
  return modint<T>(x) - y;
}
template <class T, class U> modint<T> operator*(U x, modint<T> y) {
  return modint<T>(x) * y;
}
template <class T, class U> modint<T> operator/(U x, modint<T> y) {
  return modint<T>(x) / y;
}
template <class T, class U> bool operator==(U x, modint<T> y) {
  return modint<T>(x) == y;
}
template <class T, class U> bool operator!=(U x, modint<T> y) {
  return modint<T>(x) != y;
}
/*}}}*/
using mint = modint<std::integral_constant<i64, 998244353>>;
template <usize N, class T, class... Args, std::enable_if_t<N == 0, int> = 0>
auto mkvec(Args... args) {
  return T(args...);
}
template <usize N, class T, class... Args, std::enable_if_t<N != 0, int> = 0>
auto mkvec(usize sz, Args... args) {
  using value_type = std::decay_t<decltype(mkvec<N - 1, T>(args...))>;
  return vec<value_type>(sz, mkvec<N - 1, T>(args...));
}
auto cmn = [](auto &x, auto y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
};
int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  std::cout << std::setprecision(15) << std::fixed;

  std::string s;
  i64 K;
  std::cin >> s >> K;
  cmn(K, s.length());
  vec<i64> a;
  REP(i, s.length()) if (s.at(i) == '0') a.push_back(i);
  a.push_back(s.length());
  DEBUG(a);

  i64 n = a.size() - 1;
  auto dp = mkvec<2, mint>(K + 1, K + 1);
  dp.at(0).at(0) = 1;
  for (i64 i = n - 1; i >= 0; i--) {
    // vert
    auto vert = dp;
    for (i64 j = K - 1; j >= 0; j--)
      REP(k, K + 1) { vert.at(j).at(k) += vert.at(j + 1).at(k); }

    // diag
    auto diag = mkvec<2, mint>(K + 1, K + 1);
    i64 d = a.at(i + 1) - a.at(i);
    DEBUG(d);
    REP(j, K) REP(k, K) {
      diag.at(j + 1).at(k + 1) += dp.at(j).at(k);
      if (j + d <= K && k + d <= K)
        diag.at(j + d).at(k + d) -= dp.at(j).at(k);
    }
    REP(j, K) REP(k, K) { diag.at(j + 1).at(k + 1) += diag.at(j).at(k); }

    DEBUG(matrix_style(vert));
    DEBUG(matrix_style(diag));

    dp = mkvec<2, mint>(K + 1, K + 1);
    REP(j, K + 1) REP(k, K + 1) {
      dp.at(j).at(k) = vert.at(j).at(k) + diag.at(j).at(k);
    }
    DEBUG(matrix_style(dp));
  }

  mint ans = 0;
  for (auto &&v : dp)
    for (mint &x : v)
      ans += x;
  std::cout << ans << '\\n';
}


Error: 0

Fixed submission:

#define ENABLE_DEBUG 0
// Kana's kitchen {{{
#include <bits/stdc++.h>
#define ALL(v) std::begin(v), std::end(v)
#define REP(i, k) for (std::decay_t<decltype(k)> i = 0; i < k; i++)
#define LOOP(k) REP(ngtkana_is_a_genius, k)

using i32 = std::int_least32_t;
using i64 = std::int_least64_t;
using u32 = std::uint_least32_t;
using u64 = std::uint_least64_t;
using usize = std::size_t;

template <class T, class U> using pair = std::pair<U, T>;
template <class T> using diag_pair = std::pair<T, T>;
template <class... Args> using tuple = std::tuple<Args...>;
template <class T> using vec = std::vector<T>;
template <class T> using numr = std::numeric_limits<T>;

#ifdef NGTKANA
#include <debug.hpp>
#else
#define DEBUG(...) (void)0
#endif
/*}}}*/
// mint{{{
template <class ModType> struct modint {
  using value_type = typename ModType::value_type;
  using mint = modint<ModType>;
  using mod_type = ModType;

  static value_type mod() { return ModType::value; }

private:
  static value_type inverse(value_type x) {
    value_type y = 1, u = mod(), v = 0;
    while (x) {
      value_type q = u / x;
      u -= q * x;
      std::swap(x, u);
      v -= q * y;
      std::swap(y, v);
    }
    assert(x == 0 && std::abs(y) == mod() && std::abs(u) == 1 &&
           std::abs(v) < mod());
    return v < 0 ? v + mod() : v;
  }

public:
  // the member variable
  value_type value;

  // constructors
  modint() = default;
  modint(modint const &) = default;
  modint(modint &&) = default;
  modint &operator=(modint const &) = default;
  modint &operator=(modint &&) = default;
  ~modint() = default;

  template <class T>
  modint(T t)
      : value([t]() mutable {
          if (t <= -static_cast<T>(mod()) || static_cast<T>(mod()) <= t)
            t %= mod();
          return t < 0 ? t + mod() : t;
        }()) {}

  // operators
  mint &operator+=(mint y) {
    value += y.value;
    if (mod() <= value)
      value -= mod();
    return *this;
  }

  mint &operator-=(mint y) {
    value -= y.value;
    if (value < 0)
      value += mod();
    return *this;
  }

  mint &operator*=(mint y) {
    value = (long long)value * y.value % mod();
    return *this;
  }

  mint &operator/=(mint y) {
    value = (long long)value * inverse(y.value) % mod();
    return *this;
  }

  mint &operator++() { return *this += 1; }
  mint &operator--() { return *this -= 1; }
  mint operator++(int) {
    mint this_ = *this;
    ++*this;
    return this_;
  }
  mint operator--(int) {
    mint this_ = *this;
    --*this;
    return this_;
  }
  mint operator-() const { return 0 - *this; }

  // static member functions
  static mint inv(mint x) { return inverse(x.value); }

  static mint m1pow(long long y) { return y % 2 ? -1 : 1; }

  static mint pow(mint x, unsigned long long y) {
    mint ans = 1;
    for (; y; y >>= 1) {
      if (y & 1ull)
        ans *= x;
      x *= x;
    }
    return ans;
  }

  // non-member functions
  mint &add_assign(mint y) { return *this += y; }
  mint &sub_assign(mint y) { return *this -= y; }
  mint &mul_assign(mint y) { return *this *= y; }
  mint &div_assign(mint y) { return *this /= y; }
  mint &inv_assign() { return *this = inv(*this); }
  mint &pow_assign(unsigned long long y) { return *this = pow(*this, y); }

  mint add(mint y) const {
    mint ans = *this;
    return ans.add_assign(y);
  }
  mint sub(mint y) const {
    mint ans = *this;
    return ans.sub_assign(y);
  }
  mint mul(mint y) const {
    mint ans = *this;
    return ans.mul_assign(y);
  }
  mint div(mint y) const {
    mint ans = *this;
    return ans.div_assign(y);
  }
  mint inv() const {
    mint ans = *this;
    return ans.inv_assign();
  }
  mint pow(unsigned long long y) const { return pow(*this, y); }
  mint square(mint x) const { return *this * *this; }
  mint cube(mint x) const { return *this * *this * *this; }

  template <class F> mint map(F const &f) {
    value = f(value);
    return *this;
  }
};

template <class T> std::istream &operator>>(std::istream &is, modint<T> &x) {
  typename modint<T>::value_type y;
  is >> y;
  x = modint<T>{y};
  return is;
}
template <class T> std::ostream &operator<<(std::ostream &os, modint<T> x) {
  return os << x.value;
}

template <class T> modint<T> operator+(modint<T> x, modint<T> y) {
  return x += y;
}
template <class T> modint<T> operator-(modint<T> x, modint<T> y) {
  return x -= y;
}
template <class T> modint<T> operator*(modint<T> x, modint<T> y) {
  return x *= y;
}
template <class T> modint<T> operator/(modint<T> x, modint<T> y) {
  return x /= y;
}
template <class T> bool operator==(modint<T> x, modint<T> y) {
  return x.value == y.value;
}
template <class T> bool operator!=(modint<T> x, modint<T> y) {
  return x.value != y.value;
}

template <class T, class U> modint<T> operator+(modint<T> x, U y) {
  return x + modint<T>(y);
}
template <class T, class U> modint<T> operator-(modint<T> x, U y) {
  return x - modint<T>(y);
}
template <class T, class U> modint<T> operator*(modint<T> x, U y) {
  return x * modint<T>(y);
}
template <class T, class U> modint<T> operator/(modint<T> x, U y) {
  return x / modint<T>(y);
}
template <class T, class U> bool operator==(modint<T> x, U y) {
  return x == modint<T>(y);
}
template <class T, class U> bool operator!=(modint<T> x, U y) {
  return x != modint<T>(y);
}

template <class T, class U> modint<T> operator+(U x, modint<T> y) {
  return modint<T>(x) + y;
}
template <class T, class U> modint<T> operator-(U x, modint<T> y) {
  return modint<T>(x) - y;
}
template <class T, class U> modint<T> operator*(U x, modint<T> y) {
  return modint<T>(x) * y;
}
template <class T, class U> modint<T> operator/(U x, modint<T> y) {
  return modint<T>(x) / y;
}
template <class T, class U> bool operator==(U x, modint<T> y) {
  return modint<T>(x) == y;
}
template <class T, class U> bool operator!=(U x, modint<T> y) {
  return modint<T>(x) != y;
}
/*}}}*/
using mint = modint<std::integral_constant<i64, 998244353>>;
template <usize N, class T, class... Args, std::enable_if_t<N == 0, int> = 0>
auto mkvec(Args... args) {
  return T(args...);
}
template <usize N, class T, class... Args, std::enable_if_t<N != 0, int> = 0>
auto mkvec(usize sz, Args... args) {
  using value_type = std::decay_t<decltype(mkvec<N - 1, T>(args...))>;
  return vec<value_type>(sz, mkvec<N - 1, T>(args...));
}
int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  std::cout << std::setprecision(15) << std::fixed;

  std::string s;
  i64 K;
  std::cin >> s >> K;
  vec<i64> a;
  REP(i, s.length()) if (s.at(i) == '0') a.push_back(i);
  a.push_back(s.length());
  DEBUG(a);

  i64 n = a.size() - 1;
  auto dp = mkvec<2, mint>(K + 1, K + 1);
  dp.at(0).at(0) = 1;
  for (i64 i = n - 1; i >= 0; i--) {
    // vert
    auto vert = dp;
    for (i64 j = K - 1; j >= 0; j--)
      REP(k, K + 1) { vert.at(j).at(k) += vert.at(j + 1).at(k); }

    // diag
    auto diag = mkvec<2, mint>(K + 1, K + 1);
    i64 d = a.at(i + 1) - a.at(i);
    DEBUG(d);
    REP(j, K) REP(k, K) {
      diag.at(j + 1).at(k + 1) += dp.at(j).at(k);
      if (j + d <= K && k + d <= K)
        diag.at(j + d).at(k + d) -= dp.at(j).at(k);
    }
    REP(j, K) REP(k, K) { diag.at(j + 1).at(k + 1) += diag.at(j).at(k); }

    DEBUG(matrix_style(vert));
    DEBUG(matrix_style(diag));

    dp = mkvec<2, mint>(K + 1, K + 1);
    REP(j, K + 1) REP(k, K + 1) {
      dp.at(j).at(k) = vert.at(j).at(k) + diag.at(j).at(k);
    }
    DEBUG(matrix_style(dp));
  }

  mint ans = 0;
  for (auto &&v : dp)
    for (mint &x : v)
      ans += x;
  std::cout << ans << '\\n';
}