Problem p02635 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ull unchaed long long
#define ll long long
#define il inline
#define db double
#define ls rt << 1
#define rs rt << 1 | 1
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define X first
#define Y second
#define pcc pair<char, char>
#define vi vector<int>
#define vl vector<ll>
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define rrep(i, x, y) for (int i = x; i >= y; i--)
#define rep0(i, n) for (int i = 0; i < (n); i++)
#define per0(i, n) for (int i = (n)-1; i >= 0; i--)
#define ept 1e-9
#define INF 0x3f3f3f3f
#define sz(x) (x).size()
#define Axx(x) (x).begin(), (x).cntd()
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline ll read1() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
using namespace std;
const int mod = 998244353;
const int N = 310;
inline void add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

char s[N];
int k, n, seq[N], tot, pre[N];

int dp[N][N], S;

void do_dp(int minsum, int cur) {
  static int pre1[N][N], pre2[N][N];
  for (int i = 0; i <= S; i++)
    for (int j = 0; j <= S; j++)
      pre1[i][j] = pre2[i][j] = 0;

  // puts("dp");
  for (int i = 0; i <= S; i++)
    for (int j = 0; j <= S; j++)
      if (dp[i][j]) {
        // 0 <= a[i] < cur
        add(pre1[i][j], dp[i][j]);
        add(pre1[i + cur][j], mod - dp[i][j]);

        // a[i] >= cur
        add(pre2[i + cur][j], dp[i][j]);
      }

  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= S; i++)
    for (int j = 0; j <= S; j++) {
      if (i)
        add(pre1[i][j], pre1[i - 1][j]);
      if (i && j)
        add(pre2[i][j], pre2[i - 1][j - 1]);
      if (i >= minsum) {
        dp[i][j] = pre1[i][j] + pre2[i][j];
        if (dp[i][j] >= mod)
          dp[i][j] -= mod;
      }
    }
}

int main() {
  scanf("%s%d", s + 1, &k);
  n = strlen(s + 1);

  int last = 0;
  for (int i = 1; i <= n; i++)
    if (s[i] == '0') {
      seq[++tot] = i - last - 1;
      last = i;
    }
  seq[++tot] = n - last;
  for (int i = 1; i <= tot; i++)
    pre[i] = pre[i - 1] + seq[i];
  S = pre[tot];

  dp[0][0] = 1;
  for (int i = 1; i <= tot; i++)
    do_dp(pre[i], seq[i]);

  int ans = 0;
  for (int i = 0; i <= S && i <= k; i++)
    add(ans, dp[S][i]);
  cout << ans;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ull unchaed long long
#define ll long long
#define il inline
#define db double
#define ls rt << 1
#define rs rt << 1 | 1
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define X first
#define Y second
#define pcc pair<char, char>
#define vi vector<int>
#define vl vector<ll>
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define rrep(i, x, y) for (int i = x; i >= y; i--)
#define rep0(i, n) for (int i = 0; i < (n); i++)
#define per0(i, n) for (int i = (n)-1; i >= 0; i--)
#define ept 1e-9
#define INF 0x3f3f3f3f
#define sz(x) (x).size()
#define Axx(x) (x).begin(), (x).cntd()
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline ll read1() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
using namespace std;
const int mod = 998244353;
const int N = 1010;
inline void add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

char s[N];
int k, n, seq[N], tot, pre[N];

int dp[N][N], S;

void do_dp(int minsum, int cur) {
  static int pre1[N][N], pre2[N][N];
  for (int i = 0; i <= S; i++)
    for (int j = 0; j <= S; j++)
      pre1[i][j] = pre2[i][j] = 0;

  // puts("dp");
  for (int i = 0; i <= S; i++)
    for (int j = 0; j <= S; j++)
      if (dp[i][j]) {
        // 0 <= a[i] < cur
        add(pre1[i][j], dp[i][j]);
        add(pre1[i + cur][j], mod - dp[i][j]);

        // a[i] >= cur
        add(pre2[i + cur][j], dp[i][j]);
      }

  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= S; i++)
    for (int j = 0; j <= S; j++) {
      if (i)
        add(pre1[i][j], pre1[i - 1][j]);
      if (i && j)
        add(pre2[i][j], pre2[i - 1][j - 1]);
      if (i >= minsum) {
        dp[i][j] = pre1[i][j] + pre2[i][j];
        if (dp[i][j] >= mod)
          dp[i][j] -= mod;
      }
    }
}

int main() {
  scanf("%s%d", s + 1, &k);
  n = strlen(s + 1);

  int last = 0;
  for (int i = 1; i <= n; i++)
    if (s[i] == '0') {
      seq[++tot] = i - last - 1;
      last = i;
    }
  seq[++tot] = n - last;
  for (int i = 1; i <= tot; i++)
    pre[i] = pre[i - 1] + seq[i];
  S = pre[tot];

  dp[0][0] = 1;
  for (int i = 1; i <= tot; i++)
    do_dp(pre[i], seq[i]);

  int ans = 0;
  for (int i = 0; i <= S && i <= k; i++)
    add(ans, dp[S][i]);
  cout << ans;

  return 0;
}