alexjercan/bugnet huggingface dataset:
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
constexpr ll mod = 998244353;

#define NMAX 3030
ll dp[NMAX][NMAX];

void init() { rep(i, 0, NMAX) rep(j, 0, NMAX) dp[i][j] = 0; }

int main() {

  init();

  // input
  ll a, b, c, d;
  cin >> a >> b >> c >> d;

  // dp
  dp[a][b] = 1;
  rep(h, a, c + 1) {
    rep(w, b, d + 1) {
      if (a < h)
        dp[h][w] += (dp[h - 1][w] * w) % mod;
      if (b < w)
        dp[h][w] += (dp[h][w - 1] * h) % mod;
      if (a < h && b < w)
        dp[h][w] -= (dp[h - 1][w - 1] * (w - 1) * (h - 1)) % mod;
      dp[h][w] = (dp[h][w] + mod) % mod;
    }
  }

  // print
  cout << dp[c][d] << endl;
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

vector<VVL> dp(5, VVL(3010, VL(3010, 0)));
ll tbl[3010][3010];

void init() {
  /*
  rep(k, 0, 5) rep(i, 0, 3010) rep(j, 0, 3010) {
      dp[k][i][j] = 0;
  }
  */
  rep(i, 0, 3010) rep(j, 0, 3010) tbl[i][j] = 0;
}

int main() {

  init();

  // input
  ll R, C, K;
  cin >> R >> C >> K;
  VL r(K), c(K), v(K);
  rep(i, 0, K) {
    cin >> r[i] >> c[i] >> v[i];
    tbl[r[i] - 1][c[i] - 1] = v[i];
  }

  rep(i, 0, R + 1) {
    rep(j, 0, C + 1) {
      // (i, j) -> (i+1, j)
      if (i < R) {
        // i, j で取らない
        rep(k, 0, 4) dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[k][i][j]);
        // i, j で取る
        rep(k, 0, 3) dp[0][i + 1][j] =
            max(dp[0][i + 1][j], dp[k][i][j] + tbl[i][j]);
      }
      // (i, j) -> (i, j+1)
      if (j < C) {
        // i, jで取らない
        rep(k, 0, 4) dp[k][i][j + 1] = max(dp[k][i][j + 1], dp[k][i][j]);
        // i, jで取る
        rep(k, 0, 3) dp[k + 1][i][j + 1] =
            max(dp[k + 1][i][j + 1], dp[k][i][j] + tbl[i][j]);
      }
    }
  }

  ll ans = 0;
  rep(i, 0, 4) ans = max(dp[i][R][C], ans);
  cout << ans << endl;
  return 0;
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int MOD = 998244353;
int dp[4001][4001];
int f(int a, int b) {
  if (~dp[a][b])
    return dp[a][b];
  return dp[a][b] = (f(a - 1, b) * b % MOD + f(a, b - 1) * a % MOD + MOD -
                     f(a - 1, b - 1) * (a - 1) % MOD * (b - 1) % MOD) %
                    MOD;
}

signed main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i <= 4000; i++)
    for (int j = 0; j <= 4000; j++)
      if (i < a || j < b)
        dp[i][j] = 0;
  dp[a][b] = 1;
  // for(int i=0;i<10;i++)for(int j=0;j<10;j++)cout<<i<<" "<<j<<"
  // "<<f(i,j)<<endl;
  cout << f(c, d) << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int MOD = 998244353;
int dp[4000][4000];
int f(int a, int b) {
  if (~dp[a][b])
    return dp[a][b];
  return dp[a][b] = (f(a - 1, b) * b % MOD + f(a, b - 1) * a % MOD + MOD -
                     f(a - 1, b - 1) * (a - 1) % MOD * (b - 1) % MOD) %
                    MOD;
}

signed main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i <= 4000; i++)
    for (int j = 0; j <= 4000; j++)
      if (i < a || j < b)
        dp[i][j] = 0;
  dp[a][b] = 1;
  // for(int i=0;i<10;i++)for(int j=0;j<10;j++)cout<<i<<" "<<j<<"
  // "<<f(i,j)<<endl;
  cout << f(c, d) << endl;
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

long long mod = 998244353;
long long A, B, C, D;
long long dp[3030][3030];

int main() {
  cin >> A >> B >> C >> D;
  dp[A][B] = 1;
  for (long long i = A; i <= C; i++) {
    for (long long j = B; j <= D; j++) {
      if ((i > A) || (j > B)) {
        dp[i][j] = (i * dp[i][j - 1] % mod + j * dp[i - 1][j] % mod -
                    (i - 1) * (j - 1) * dp[i - 1][j - 1] % mod + mod) %
                   mod;
      }
    }
  }
  cout << dp[C][D];
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

long long mod = 998244353;
long long A, B, C, D;
long long dp[3000][3000];

int main() {
  cin >> A >> B >> C >> D;
  dp[A][B] = 1;
  for (long long i = A; i <= C; i++) {
    for (long long j = B; j <= D; j++) {
      if ((i > A) || (j > B)) {
        dp[i][j] = (i * dp[i][j - 1] % mod + j * dp[i - 1][j] % mod -
                    (i - 1) * (j - 1) * dp[i - 1][j - 1] % mod) %
                   mod;
      }
    }
  }
  cout << dp[C][D];
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

int const nmax = 3000;
int const modulo = 998244353;

int dp[5 + nmax][5 + nmax];

int main() {
  int n, m, q, p;
  std::cin >> n >> m >> p >> q;
  dp[p][q] = 1;
  for (int i = p; n <= i; i--)
    for (int j = q; m <= j; j--)
      dp[i][j] += (modulo + 1LL * dp[i][j + 1] * i + 1LL * dp[i + 1][j] * j -
                   1LL * dp[i + 1][j + 1] * i % modulo * j % modulo) %
                  modulo;
  std::cout << dp[n][m];
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

int const nmax = 3000;
int const modulo = 998244353;

int dp[1 + nmax][1 + nmax];

int main() {
  int n, m, q, p;
  std::cin >> n >> m >> p >> q;
  dp[p][q] = 1;
  for (int i = p; n <= i; i--)
    for (int j = q; m <= j; j--)
      dp[i][j] += (modulo + 1LL * dp[i][j + 1] * i + 1LL * dp[i + 1][j] * j -
                   1LL * dp[i + 1][j + 1] * i % modulo * j) %
                  modulo;
  std::cout << dp[n][m];
}
Problem p02634 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

const int P = 998244353;

int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;

  vector<vector<long long>> dpv(C - A + 1, vector<long long>(D - B + 1, 0));
  dpv[0][0] = 1; // Boundary condition.
  for (int i = 0; i <= C - A; ++i) {
    for (int j = 0; j <= D - B; ++j) {
      if (i == 0 && j == 0)
        continue;

      dpv[i][j] = 0;
      if (i > 0) { // Last step was adding a row.
        dpv[i][j] += (B + j) * dpv[i - 1][j];
      }
      if (j > 0) { // Last step was adding a col
        dpv[i][j] += (A + i) * dpv[i][j - 1];
      }
      if (i > 0 && j > 0) {
        dpv[i][j] -= ((A + i - 1) * (B + j - 1) * dpv[i - 1][j - 1]) % P;
      }
      while (dpv[i][j] < 0) {
        dpv[i][j] += P;
      }
      while (dpv[i][j] >= P) {
        dpv[i][j] %= P;
      }
    }
  }
  cout << dpv.back().back() << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int P = 998244353;

int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;

  vector<vector<long long>> dpv(C - A + 1, vector<long long>(D - B + 1, 0));
  dpv[0][0] = 1; // Boundary condition.
  for (int i = 0; i <= C - A; ++i) {
    for (int j = 0; j <= D - B; ++j) {
      if (i == 0 && j == 0)
        continue;

      dpv[i][j] = 0;
      if (i > 0) { // Last step was adding a row.
        dpv[i][j] += (B + j) * dpv[i - 1][j];
      }
      if (j > 0) { // Last step was adding a col
        dpv[i][j] += (A + i) * dpv[i][j - 1];
      }
      if (i > 0 && j > 0) {
        dpv[i][j] -= (A + i - 1) * (B + j - 1) * dpv[i - 1][j - 1];
      }
      while (dpv[i][j] < 0)
        dpv[i][j] += P;
      while (dpv[i][j] >= P)
        dpv[i][j] -= P;
    }
  }
  cout << dpv.back().back() << endl;
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int mod = 998244353;
typedef long long ll;
ll dp[2][6005] = {0};
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  dp[a % 2][b] = 1;
  int j;
  for (int i = a; i <= c; i++) {
    int row = i % 2;
    j = b;
    if (i == a)
      j++;
    for (; j <= d; j++)
      if (i != a && j != b) {
        dp[row][j] =
            (dp[(row + 1) % 2][j] * j % mod + dp[row][j - 1] * i % mod -
             (i - 1) * (j - 1) * dp[(row + 1) % 2][j - 1] % mod + mod) %
            mod;
      } else
        dp[row][j] =
            (dp[(row + 1) % 2][j] * j % mod + dp[row][j - 1] * i % mod) % mod;
    //        for(int j=b;j<=d;j++){
    //            cout << dp[row][j] << " \\n"[j==d];
    //        }
  }
  cout << dp[c % 2][d] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int mod = 998244353;
typedef long long ll;
ll dp[2][6005] = {0};
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  dp[a % 2][b] = 1;
  int j;
  for (int i = a; i <= c; i++) {
    int row = i % 2;
    j = b;
    if (i == a)
      j++;
    for (; j <= d; j++)
      if (i != a && j != b) {
        dp[row][j] =
            (dp[(row + 1) % 2][j] * j % mod + dp[row][j - 1] * i % mod - 1) %
            mod;
      } else
        dp[row][j] =
            (dp[(row + 1) % 2][j] * j % mod + dp[row][j - 1] * i % mod -
             (i - 1) * (j - 1) * dp[i - 1][j - 1] % mod) %
            mod;
    //        for(int j=b;j<=d;j++){
    //            cout << dp[row][j] << " \\n"[j==d];
    //        }
  }
  cout << dp[c % 2][d] << endl;
  return 0;
}
Problem p02634 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef long long Int;

const Int MOD = 998244353;

int main() {
  Int A, B, C, D;
  cin >> A >> B >> C >> D;

  Int h, w;
  h = C - A + 1;
  w = D - B + 1;
  vector<vector<Int>> dp(3010, vector<Int>(3010, 0));
  dp[0][0] = 1;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (i == 0 && j == 0)
        continue;
      if (i == 0)
        dp[i][j] = (dp[i][j - 1] * A) % MOD;
      else if (j == 0)
        dp[i][j] = (dp[i - 1][j] * B) % MOD;
      else
        dp[i][j] =
            (((((dp[i][j - 1] * (Int)(A + i)) % MOD) +
               ((dp[i - 1][j] * (Int)(B + j)) % MOD)) %
              MOD) -
             (((dp[i - 1][j - 1] * (Int)(A + i - 1)) % MOD * (Int)(B + j - 1)) %
              MOD) +
             MOD) %
            MOD;
    }
  }
  cout << dp[h - 1][w - 1] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef long long Int;

const Int MOD = 998244353;

int main() {
  Int A, B, C, D;
  cin >> A >> B >> C >> D;

  Int h, w;
  h = C - A + 1;
  w = D - B + 1;
  vector<vector<Int>> dp(h, vector<Int>(w, 0));
  dp[0][0] = 1;
  dp[1][0] = B;
  dp[0][1] = A;
  for (int i = 2; i < h; i++) {
    dp[i][0] = (dp[i - 1][0] * B) % MOD;
  }
  for (int i = 2; i < w; i++) {
    dp[0][i] = (dp[0][i - 1] * A) % MOD;
  }

  if (h == 1) {
    cout << dp[h - 1][w - 1] << endl;
    return 0;
  }

  if (w == 1) {
    cout << dp[h - 1][w - 1] << endl;
    return 0;
  }

  for (int i = 1; i < h; i++) {
    for (int j = 1; j < w; j++) {
      dp[i][j] =
          (((((dp[i][j - 1] * (Int)(A + i)) % MOD) +
             ((dp[i - 1][j] * (Int)(B + j)) % MOD)) %
            MOD) -
           (((dp[i - 1][j - 1] * (Int)(A + i - 1)) % MOD * (Int)(B + j - 1)) %
            MOD) +
           MOD) %
          MOD;
    }
  }

  cout << dp[h - 1][w - 1] << endl;

  return 0;
}
Problem p02634 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
const long long mod = 998244353;
int main(void) {
  long long A, B, C, D;
  cin >> A >> B >> C >> D;
  long long DP[3001][3001];
  DP[A][B] = 1;
  long long ans = 0;
  long long N;

  long long i, j, t, k;
  for (i = A + 1; i < C + 1; i++) {
    DP[i][B] = DP[i - 1][B] * B % mod;
  }
  for (i = B + 1; i < D + 1; i++) {
    DP[A][i] = DP[A][i - 1] * A % mod;
  }
  N = (C - A) + (D - B) + 1;
  for (i = 2; i < N; i++) {
    long long s = max((long long)0, i - (D - B));
    long long g = min(i, C - A + 1);
    for (j = s; j < g; j++) {
      int x = A + j;
      int y = B + i - j;
      DP[x][y] = (DP[x - 1][y] * y + DP[x][y - 1] * x) % mod;
      DP[x][y] -= DP[x - 1][y - 1] * (x - 1) * (y - 1) % mod;
      while (DP[x][y] < 0) {
        DP[x][y] += mod;
      }
    }
  }
  cout << DP[C][D] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
const long long mod = 998244353;
int main(void) {
  long long A, B, C, D;
  cin >> A >> B >> C >> D;
  long long DP[3001][3001];
  DP[A][B] = 1;
  long long ans = 0;
  long long N;

  long long i, j, t, k;
  for (i = A + 1; i < C + 1; i++) {
    DP[i][B] = DP[i - 1][B] * B % mod;
  }
  for (i = B + 1; i < D + 1; i++) {
    DP[A][i] = DP[A][i - 1] * A % mod;
  }
  N = (C - A) + (D - B) + 1;
  for (i = 2; i < N; i++) {
    long long s = max((long long)0, i - (D - B));
    long long g = min(i, C - A + 1);
    for (j = s; j < g; j++) {
      int x = A + j;
      int y = B + i - j;
      DP[x][y] = (DP[x - 1][y] * y + DP[x][y - 1] * x) % mod;
      DP[x][y] -= DP[x - 1][y - 1] * (x - 1) * (y - 1);
      while (DP[x][y] < 0) {
        DP[x][y] += mod;
      }
    }
  }
  cout << DP[C][D] << endl;
}
Problem p02634 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
#define INF (1e9)
ll MOD = 998244353;

int main() {
  ll A, B, C, D;
  cin >> A >> B >> C >> D;

  vvi DP(C + 1, vi(D + 1));
  DP[A][B] = 1;
  for (int i = A + 1; i <= C; i++) {
    DP[i][B] = (DP[i - 1][B] * B) % MOD;
  }
  for (int j = B + 1; j <= D; j++) {
    DP[A][j] = (DP[A][j - 1] * A) % MOD;
  }

  for (int i = A + 1; i <= C; i++) {
    for (int j = B + 1; j <= D; j++) {
      DP[i][j] = DP[i - 1][j] * j + DP[i][j - 1] * i +
                 (MOD - DP[i - 1][j - 1]) * (i - 1) * (j - 1);
      DP[i][j] %= MOD;
    }
  }

  ll ans = DP[C][D];
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
#define INF (1e9)
ll MOD = 998244353;

int main() {
  ll A, B, C, D;
  cin >> A >> B >> C >> D;

  vvi DP(C + 1, vi(D + 1));
  DP[A][B] = 1;
  for (int i = A + 1; i <= C; i++) {
    DP[i][B] = (DP[i - 1][B] * B) % MOD;
  }
  for (int j = B + 1; j <= D; j++) {
    DP[A][j] = (DP[A][j - 1] * A) % MOD;
  }

  for (int i = A + 1; i <= C; i++) {
    for (int j = B + 1; j <= D; j++) {
      DP[i][j] = DP[i - 1][j] * j + DP[i][j - 1] * i -
                 DP[i - 1][j - 1] * (i - 1) * (j - 1);
      while (DP[i][j] < 0)
        DP[i][j] += MOD;
      DP[i][j] %= MOD;
    }
  }

  ll ans = DP[C][D];
  cout << ans << endl;
}
Problem p02634 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
typedef long long LL;
#define REP(j, k, n) for (LL j = k; j < (LL)n; j++)
#define rep(i, N) REP(i, 0, N)
const int MOD = 998244353;
inline LL Pow(LL x, LL y) {
  LL z = 1;
  x %= MOD;
  while (y) {
    if (y & 1) {
      z = (z * x) % MOD;
    }
    x = (x * x) % MOD;
    y >>= 1;
  }
  return z % MOD;
}
int main(void) {
  int A, B, C, D, ans = 0, dp[3001] = {0};
  LL sum;
  if (!scanf("%d %d %d %d", &A, &B, &C, &D))
    return 0;
  dp[A] = 1;
  rep(i, D - B) {
    sum = 0;
    REP(j, 1, C + 1) {
      sum = (sum * (B + i) % MOD + dp[j]) % MOD;
      dp[j] = (dp[j] * (j - 1) + sum) % MOD;
    }
  }
  rep(i, C + 1) ans = (ans + dp[i] * Pow(D, C - i) % MOD) % MOD;
  return printf("%lld\\n", ans < 0 ? ans + MOD : ans), 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
typedef long long LL;
#define rep(i, N) for (int i = 0; i < (int)N; i++)
const int MOD = 998244353;
const double EPS = 1e-8;
inline int M(int x, int y) {
  LL z = x * y - (LL)((double)x / MOD * y + EPS) * MOD;
  return z < 0 ? z + MOD : (int)z;
}
inline int Pow(int x, int y) {
  int z = 1;
  while (y) {
    if (y & 1) {
      z = M(z, x);
    }
    x = M(x, x);
    y >>= 1;
  }
  return z;
}
int main(void) {
  int A, B, C, D, x, y, dp[3001][2] = {0};
  LL temp, Ans = 0;
  if (!scanf("%d %d %d %d", &A, &B, &C, &D))
    return 0;
  dp[A - 1][0] = Pow(B, C - A);
  rep(i, D - B) {
    x = B + i, temp = 0;
    rep(j, C - A + 1) {
      y = A + j - 1;
      dp[y][1] = (int)((LL)M((LL)M((M(dp[y][0], A + j) + temp) % MOD,
                                   Pow(Pow(x, C - A - j), MOD - 2)),
                             Pow(x + 1, C - y - 1)));
      temp = (int)((LL)(temp + dp[y][0]) % MOD);
      dp[A + j - 1][0] = dp[A + j - 1][1];
    }
  }
  rep(i, C - A + 1) Ans = (int)((LL)(Ans + dp[A + i - 1][0]) % MOD);
  return printf("%lld\\n", Ans), 0;
}
Problem p02635 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int p = 998244353;
int n, m, mx, i, j, k, l, val, ans, f[302][302][302], a[302], b[302], sum, sz;
char s[10010];
int main() {
  scanf("%s", s + 1);
  scanf("%d", &mx);
  n = strlen(s + 1);
  for (i = 1; i <= n; i++) {
    if (s[i] == '1')
      sz++, sum++;
    else {
      a[++m] = sz;
      sz = 0;
    }
  }
  f[0][0][0] = 1;
  for (i = 1; i <= m; i++) {
    b[i] = a[i];
    a[i] += a[i - 1];
    for (j = a[i - 1]; j <= sum; j++)
      for (k = 0; k <= min(j, mx); k++)
        if (f[i - 1][j][k])
          for (l = max(0, a[i] - j); l <= sum - j; l++) {
            val = max(0, l - b[i]);
            if (k + val <= mx)
              (f[i][j + l][k + val] += f[i - 1][j][k]) %= p;
          }
  }
  for (j = a[m]; j <= sum; j++)
    for (k = 0; k <= min(mx, sum); k++)
      ans = (ans + f[m][j][k]) % p;
  printf("%d", ans);
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int p = 998244353;
int n, m, mx, i, j, k, l, val, ans, f[302][302][302], a[302], b[302], sum, sz;
char s[10010];
int main() {
  scanf("%s", s + 1);
  scanf("%d", &mx);
  n = strlen(s + 1);
  for (i = 1; i <= n; i++) {
    if (s[i] == '1')
      sz++, sum++;
    else {
      a[++m] = sz;
      sz = 0;
    }
  }
  f[0][0][0] = 1;
  for (i = 1; i <= m; i++) {
    b[i] = a[i];
    a[i] += a[i - 1];
    for (j = a[i - 1]; j <= sum; j++)
      for (k = 0; k <= min(j, mx); k++)
        if (f[i - 1][j][k])
          for (l = max(0, a[i] - j); l <= sum - j; l++) {
            val = max(0, l - b[i]);
            if (k + val <= mx)
              (f[i][j + l][k + val] += f[i - 1][j][k]) %= p;
          }
  }
  for (j = a[m]; j <= sum; j++)
    for (k = 0; k <= mx; k++)
      ans = (ans + f[m][j][k]) % p;
  printf("%d", ans);
}
Problem p02635 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long lol;
const int mod = 998244353;
inline int pls(int a, int b) {
  a += b - mod;
  return a + (a >> 31 & mod);
}
inline int mns(int a, int b) {
  a -= b;
  return a + (a >> 31 & mod);
}
inline void inc(int &a, int b) {
  a += b - mod;
  a += a >> 31 & mod;
}
inline void dec(int &a, int b) {
  a -= b;
  a += a >> 31 & mod;
}
inline int fpow(int base, int k) {
  int res = 1;
  while (k) {
    if (k & 1)
      res = (lol)res * base % mod;
    base = (lol)base * base % mod;
    k >>= 1;
  }
  return res;
}
int _w;

const int N = 3e2 + 5;

int f[N][N][N], n, k, sum[N], m, ss[N];
char str[N];

int main(void) {
  _w = scanf("%s%d", str + 1, &k);
  n = strlen(str + 1);
  str[n + 1] = '0';
  for (int i = 0, r; i <= n; i = r) {
    r = i + 1;
    while (r <= n && str[r] != '0')
      ++r;
    sum[++m] = r - i - 1;
  }
  for (int i = m; i; --i)
    ss[i] = ss[i + 1] + sum[i];
  k = min(k, ss[1]);
  f[0][0][0] = 1;
  for (int i = 1, s = 0; i <= m; s += sum[i], ++i)
    for (int j = s; j <= ss[1]; ++j)
      for (int l = 0; l <= k; ++l)
        if (f[i - 1][j][l])
          for (int p = 0; p + j <= ss[1] && l + p - sum[i] <= k; ++p) {
            inc(f[i][j + p][l + max(p - sum[i], 0)], f[i - 1][j][l]);
          }
  int ans = 0;
  for (int i = 0; i <= k; ++i)
    inc(ans, f[m][ss[1]][i]);
  printf("%d", ans);
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long lol;
const int mod = 998244353;
inline int pls(int a, int b) {
  a += b - mod;
  return a + (a >> 31 & mod);
}
inline int mns(int a, int b) {
  a -= b;
  return a + (a >> 31 & mod);
}
inline void inc(int &a, int b) {
  a += b - mod;
  a += a >> 31 & mod;
}
inline void dec(int &a, int b) {
  a -= b;
  a += a >> 31 & mod;
}
inline int fpow(int base, int k) {
  int res = 1;
  while (k) {
    if (k & 1)
      res = (lol)res * base % mod;
    base = (lol)base * base % mod;
    k >>= 1;
  }
  return res;
}
int _w;

const int N = 3e2 + 5;

int f[N][N][N], n, k, sum[N], m, ss[N];
char str[N];

int main(void) {
  _w = scanf("%s%d", str + 1, &k);
  n = strlen(str + 1);
  str[n + 1] = '0';
  for (int i = 0, r; i <= n; i = r) {
    r = i + 1;
    while (r <= n && str[r] != '0')
      ++r;
    sum[++m] = r - i - 1;
  }
  for (int i = m; i; --i)
    ss[i] = ss[i + 1] + sum[i];
  f[0][0][0] = 1;
  for (int i = 1, s = 0; i <= m; s += sum[i], ++i)
    for (int j = s; j <= ss[1]; ++j)
      for (int l = 0; l <= k; ++l)
        if (f[i - 1][j][l])
          for (int p = 0; p + j <= ss[1] && l + p - sum[i] <= k; ++p) {
            inc(f[i][j + p][l + max(p - sum[i], 0)], f[i - 1][j][l]);
          }
  int ans = 0;
  for (int i = 0; i <= k; ++i)
    inc(ans, f[m][ss[1]][i]);
  printf("%d", ans);
  return 0;
}