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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define pp pair<ll, ll>
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll MOD = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
ll mod = INF;

void Add(int &a, int b) {
  a += b;
  while (a >= MOD)
    a -= MOD;
  while (a < 0)
    a += MOD;
}

void Mul(int &a, int b) { a = 1LL * a * b % MOD; }

int Pow(int b, int e) {
  if (e < 0)
    e += MOD - 1;
  int ret = 1;
  while (e) {
    if (e & 1)
      ret = 1LL * ret * b % MOD;
    b = 1LL * b * b % MOD;
    e >>= 1;
  }
  return ret;
}

namespace Math {
const int NMAX = (int)1e5;
int pow2[1 + NMAX];
int fact[1 + NMAX], tcaf[1 + NMAX];
void Precalc() {
  fact[0] = pow2[0] = 1;
  for (int i = 1; i <= NMAX; ++i) {
    pow2[i] = pow2[i - 1];
    Add(pow2[i], pow2[i]);

    fact[i] = fact[i - 1];
    Mul(fact[i], i);
  }
  tcaf[NMAX] = Pow(fact[NMAX], -1);
  for (int i = NMAX - 1; i >= 0; --i) {
    tcaf[i] = tcaf[i + 1];
    Mul(tcaf[i], i + 1);
    assert(1LL * fact[i] * tcaf[i] % MOD == 1);
  }
}
int Choose(int n, int k) {
  if (k > n || n < 0)
    return 0;
  return 1LL * fact[n] * tcaf[n - k] % MOD * tcaf[k] % MOD;
}
}; // namespace Math

int main() {
  string s;
  int k;
  cin >> s >> k;
  vector<ll> a(0);
  ll c = 0, su = 0;
  rep(i, s.size()) {
    if (s[i] == '0') {
      a.push_back(c);
      c = 0;
    } else {
      c++;
      su++;
    }
  }
  if (k > su)
    k = su;
  int t = (k + c + 1) * 2 + 4;
  vector<vector<ll>> w(k + c + 1, vector<ll>(k + 1));
  rep(j, k + c + 1) { rep(ww, k + 1) w[j][ww] = 0; }
  vector<vector<ll>> we(k + c + 1, vector<ll>(k + 1));
  we = w;
  w[0][0] = 1;
  rep(i, a.size()) {
    vector<vector<ll>> wt(k + c + 1, vector<ll>(k + 1));
    wt = we;
    rep(j, t) {
      if (j <= k + c + 1) {
        int r = k + c + 1 - j;
        if (a[i] - r < 0)
          continue;
        rep(ii, k + c + 1) {
          rep(jj, k + 1) {
            if (ii < jj + r)
              continue;
            if (jj + r >= k + 1)
              break;
            (wt[ii][jj + r] += w[ii][jj]) %= MOD;
          }
        }
      } else {
        int r = j - k - c - 1;
        rep(ii, k + c + 1) {
          rep(jj, k + 1) {
            if (ii + r < jj)
              continue;
            if (ii + r >= k + c + 1)
              break;
            (wt[ii + r][jj] += w[ii][jj]) %= MOD;
          }
        }
      }
    }
    w = wt;
  }
  ll ans = 0;
  rep(j, c + 1) {
    rep(i, k - j + 1) { ans = (ans + w[i + j][i]) % MOD; }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define pp pair<ll, ll>
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll MOD = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
ll mod = INF;

void Add(int &a, int b) {
  a += b;
  while (a >= MOD)
    a -= MOD;
  while (a < 0)
    a += MOD;
}

void Mul(int &a, int b) { a = 1LL * a * b % MOD; }

int Pow(int b, int e) {
  if (e < 0)
    e += MOD - 1;
  int ret = 1;
  while (e) {
    if (e & 1)
      ret = 1LL * ret * b % MOD;
    b = 1LL * b * b % MOD;
    e >>= 1;
  }
  return ret;
}

namespace Math {
const int NMAX = (int)1e5;
int pow2[1 + NMAX];
int fact[1 + NMAX], tcaf[1 + NMAX];
void Precalc() {
  fact[0] = pow2[0] = 1;
  for (int i = 1; i <= NMAX; ++i) {
    pow2[i] = pow2[i - 1];
    Add(pow2[i], pow2[i]);

    fact[i] = fact[i - 1];
    Mul(fact[i], i);
  }
  tcaf[NMAX] = Pow(fact[NMAX], -1);
  for (int i = NMAX - 1; i >= 0; --i) {
    tcaf[i] = tcaf[i + 1];
    Mul(tcaf[i], i + 1);
    assert(1LL * fact[i] * tcaf[i] % MOD == 1);
  }
}
int Choose(int n, int k) {
  if (k > n || n < 0)
    return 0;
  return 1LL * fact[n] * tcaf[n - k] % MOD * tcaf[k] % MOD;
}
}; // namespace Math

int main() {
  Math::Precalc();
  string s;
  int k;
  cin >> s >> k;
  vector<ll> a(0);
  ll c = 0, su = 0;
  rep(i, s.size()) {
    if (s[i] == '0') {
      a.push_back(c);
      c = 0;
    } else {
      c++;
      su++;
    }
  }
  if (k > su)
    k = su;
  int t = (k + c + 1) * 2 + 4;
  ll d[t][k + c + 1][k + 1];
  vector<vector<ll>> w(k + c + 1, vector<ll>(k + 1));
  rep(i, t) {
    rep(j, k + c + 1) { rep(ww, k + 1) d[i][j][ww] = 0; }
  }
  rep(j, k + c + 1) { rep(ww, k + 1) w[j][ww] = 0; }
  vector<vector<ll>> we(k + c + 1, vector<ll>(k + 1));
  we = w;
  w[0][0] = 1;
  d[k + c + 1][0][0] = 1;
  rep(i, a.size()) {
    vector<vector<ll>> wt(k + c + 1, vector<ll>(k + 1));
    wt = we;
    rep(j, t) {
      if (j <= k + c + 1) {
        int r = k + c + 1 - j;
        if (a[i] - r < 0)
          continue;
        rep(ii, k + c + 1) {
          rep(jj, k + 1) {
            if (ii < jj + r)
              continue;
            if (jj + r >= k + 1)
              break;
            d[j][ii][jj + r] = w[ii][jj];
            (wt[ii][jj + r] += w[ii][jj]) %= MOD;
          }
        }
      } else {
        int r = j - k - c - 1;
        rep(ii, k + c + 1) {
          rep(jj, k + 1) {
            if (ii + r < jj)
              continue;
            if (ii + r >= k + c + 1)
              break;
            d[j][ii + r][jj] = w[ii][jj];
            (wt[ii + r][jj] += w[ii][jj]) %= MOD;
          }
        }
      }
    }
    w = wt;
  }
  ll ans = 0;
  rep(j, c + 1) {
    rep(i, k - j + 1) { ans = (ans + w[i + j][i]) % MOD; }
  }
  cout << ans << endl;
}