Problem p02635 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("Ofast")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T>
// using o_set = tree <T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>; #define sfind(i) find_by_order(i) #define
// ord(i) order_of_key(i)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
#define mod 998244353
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repc(i, a, b, c)                                                       \
  for (ll i = a; (c > 0 ? i < b : i > b) and c != 0; i += c)
#define vi vector<int>
#define vl vector<ll>
#define _max(vec) (*max_element(all(vec)))
#define _min(vec) (*min_element(all(vec)))
#define fi first
#define si second
#define pb push_back
#define all(name) name.begin(), name.end()
#define ral(name) name.rbegin(), name.rend()
#define fact(n) rep(i, 1, n + 1) ft.pb((ft[i - 1] * i) % mod)
#define mod_in(a) power(a, mod - 2)
#define ncr(n, r)                                                              \
  (n >= r ? ((ft[n] * mod_in((ft[r] * ft[(n) - (r)]) % mod)) % mod) : 0LL)
#define deb(x) cerr << "[" << #x << " " << x << "]" << endl;
#define endl '\n' //....????
// #define int ll
vl ft(1, 1LL);

/*======================================================================*/
ll power(ll x, ll y) {
  ll res = 1;
  x %= mod;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res % mod;
}
ll add(ll a, ll b) {
  a += b;
  return a >= mod ? a - mod : a;
}
ll sub(ll a, ll b) {
  a -= b;
  return (a < 0 ? a + mod : a);
}
ll mult(ll a, ll b) { return (a * b) % mod; }
int dp[305][305][305];
void solve() {
  fact(1000);
  string s;
  int steps, n, len;
  cin >> s >> steps;
  n = s.length();
  vi last;
  rep(i, 0, n) if (s[i] == '0') last.pb(i + 1);
  len = last.size();
  steps = min(steps, n - len);
  dp[0][0][0] = 1;
  int prevj = 0;
  rep(i, 1, len + 1) {
    int l = last[i - 1];
    for (int cur = n - len + i; cur >= l; cur--) {
      for (int j = prevj; j < cur; j++) {
        int lim = max(0, (cur - j) - (l - prevj));
        for (int k = 0; k + lim <= steps; k++) {
          dp[i][cur][k + lim] = add(dp[i][cur][k + lim], dp[i - 1][j][k]);
        }
      }
    }
    prevj = l;
  }
  int ans = 0;
  for (int i = last[len - 1]; i <= n; i++) {
    rep(j, 0, steps + 1) { ans = add(ans, dp[len][i][j]); }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}

Fixed submission:
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("Ofast")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T>
// using o_set = tree <T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>; #define sfind(i) find_by_order(i) #define
// ord(i) order_of_key(i)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
#define mod 998244353
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repc(i, a, b, c)                                                       \
  for (ll i = a; (c > 0 ? i < b : i > b) and c != 0; i += c)
#define vi vector<int>
#define vl vector<ll>
#define _max(vec) (*max_element(all(vec)))
#define _min(vec) (*min_element(all(vec)))
#define fi first
#define si second
#define pb push_back
#define all(name) name.begin(), name.end()
#define ral(name) name.rbegin(), name.rend()
#define fact(n) rep(i, 1, n + 1) ft.pb((ft[i - 1] * i) % mod)
#define mod_in(a) power(a, mod - 2)
#define ncr(n, r)                                                              \
  (n >= r ? ((ft[n] * mod_in((ft[r] * ft[(n) - (r)]) % mod)) % mod) : 0LL)
#define deb(x) cerr << "[" << #x << " " << x << "]" << endl;
#define endl '\n' //....????
// #define int ll
vl ft(1, 1LL);

/*======================================================================*/
ll power(ll x, ll y) {
  ll res = 1;
  x %= mod;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res % mod;
}
ll add(ll a, ll b) {
  a += b;
  return a >= mod ? a - mod : a;
}
ll sub(ll a, ll b) {
  a -= b;
  return (a < 0 ? a + mod : a);
}
ll mult(ll a, ll b) { return (a * b) % mod; }
int dp[305][305][305];
void solve() {
  fact(1000);
  string s;
  int steps, n, len;
  cin >> s >> steps;
  n = s.length();
  vi last;
  rep(i, 0, n) if (s[i] == '0') last.pb(i + 1);
  len = last.size();
  if (len == 0) {
    cout << 1;
    return;
  }
  steps = min(steps, n - len);
  dp[0][0][0] = 1;
  int prevj = 0;
  rep(i, 1, len + 1) {
    int l = last[i - 1];
    for (int cur = n - len + i; cur >= l; cur--) {
      for (int j = prevj; j < cur; j++) {
        int lim = max(0, (cur - j) - (l - prevj));
        for (int k = 0; k + lim <= steps; k++) {
          dp[i][cur][k + lim] = add(dp[i][cur][k + lim], dp[i - 1][j][k]);
        }
      }
    }
    prevj = l;
  }
  int ans = 0;
  for (int i = last[len - 1]; i <= n; i++) {
    rep(j, 0, steps + 1) { ans = add(ans, dp[len][i][j]); }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}