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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
const ll mod = 998244353;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head

const int N = 310;
char s[N];
int m, ss[N], p[N];
int dp[N][N][N], ans;

void upd(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
int main() {
  scanf("%s%d", s, &m);
  int n = strlen(s);
  reverse(s, s + n);
  m = min(m, n);
  int p0 = 0;
  rep(i, 0, n) {
    if (s[i] == '0')
      ss[p0 + 1] = ss[p0], p0++;
    else
      p[p0]++, ss[p0]++;
  }

  dp[0][0][0] = 1;
  rep(i, 1, p0 + 2) rep(k, 0, m + 1) {
    int t = 0;
    rep(j, 0, ss[i - 1] + 1) {
      for (int z = 0; z < p[i - 1] && z <= j; z++)
        upd(dp[i][j][k + p[i - 1] - z], dp[i - 1][j - z][k]);
      if (j - p[i - 1] >= 0)
        upd(t, dp[i - 1][j - p[i - 1]][k]);
      upd(dp[i][j][k], t);
    }
  }
  rep(i, 0, m + 1) upd(ans, dp[p0 + 1][ss[p0]][i]);
  printf("%d\\n", ans);
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
const ll mod = 998244353;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head

const int N = 310;
char s[N];
int m, ss[N], p[N];
int dp[N][N][N], ans;

void upd(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
int main() {
  scanf("%s%d", s, &m);
  int n = strlen(s);
  reverse(s, s + n);
  m = min(m, n);
  int p0 = 0;
  rep(i, 0, n) {
    if (s[i] == '0')
      ss[p0 + 1] = ss[p0], p0++;
    else
      p[p0]++, ss[p0]++;
  }

  dp[0][0][0] = 1;
  rep(i, 1, p0 + 2) rep(k, 0, m + 1) {
    int t = 0;
    rep(j, 0, ss[i - 1] + 1) {
      for (int z = 0; z < p[i - 1]; z++)
        upd(dp[i][j][k + p[i - 1] - z], dp[i - 1][j - z][k]);
      if (j - p[i - 1] >= 0)
        upd(t, dp[i - 1][j - p[i - 1]][k]);
      upd(dp[i][j][k], t);
    }
  }
  rep(i, 0, m + 1) upd(ans, dp[p0 + 1][ss[p0]][i]);
  printf("%d\\n", ans);
}
Problem p02635 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("input.txt");
#define cin input_from_file
#else

#endif

const LL mod = 998244353;
const int MAXN = 305;

vector<int> A;

LL fatto[MAXN][MAXN][MAXN];
//~ map<tuple<int,int,int>,LL> fatto;

LL F(int R, int B, int K) {
  //~ if (R == 0) cout << R << " " << B << " " << K << ": " << 1 << endl;
  if (B > K)
    return 0;
  //~ if (R == 0) cout << R << " " << B << " " << K << ": " << 1 << endl;
  if (R == 0)
    return 1;
  if (fatto[R][B][K] != -1)
    return fatto[R][B][K];
  LL res = 0;
  //~ if (R == 1 and B == 0) cout << A[R] << endl;
  for (int s = 0; s <= min(A[R] + B, K); s++) {
    res += F(R - 1, s, K - max(B - s, 0)), res %= mod;
  }
  fatto[R][B][K] = res;
  //~ cout << R << " " << B << " " << K << ": " << res << endl;
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0); // Togliere nei problemi con query online

  for (int i = 0; i < MAXN; i++)
    for (int j = 0; j < MAXN; j++)
      for (int k = 0; k < MAXN; k++)
        fatto[i][j][k] = -1;

  string S;
  cin >> S;
  S += '0';
  int K;
  cin >> K;
  int at = 0;
  int qq = 0;
  for (char c : S) {
    if (c == '0') {
      A.push_back(at);
      at = 0;
    } else
      at++, qq++;
  }

  //~ for (int x : A) cout << x << " ";
  //~ cout << endl;
  int L = A.size();
  cout << F(L - 1, 0, min(K, qq)) << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("input.txt");
#define cin input_from_file
#else

#endif

const LL mod = 998244353;

vector<int> A;

map<tuple<int, int, int>, LL> fatto;

LL F(int R, int B, int K) {
  //~ if (R == 0) cout << R << " " << B << " " << K << ": " << 1 << endl;
  if (B > K)
    return 0;
  //~ if (R == 0) cout << R << " " << B << " " << K << ": " << 1 << endl;
  if (R == 0)
    return 1;
  if (fatto[{R, B, K}])
    return fatto[{R, B, K}];
  LL res = 0;
  //~ if (R == 1 and B == 0) cout << A[R] << endl;
  for (int s = 0; s <= min(A[R] + B, K); s++) {
    res += F(R - 1, s, K - max(B - s, 0)), res %= mod;
  }
  fatto[{R, B, K}] = res;
  //~ cout << R << " " << B << " " << K << ": " << res << endl;
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0); // Togliere nei problemi con query online

  string S;
  cin >> S;
  S += '0';
  int K;
  cin >> K;
  int at = 0;
  int qq = 0;
  for (char c : S) {
    if (c == '0') {
      A.push_back(at);
      at = 0;
    } else
      at++, qq++;
  }
  //~ for (int x : A) cout << x << " ";
  //~ cout << endl;
  int L = A.size();
  cout << F(L - 1, 0, min(K, qq)) << "\\n";
}
Problem p02635 (C++) — buggy submission, verdict: Time Limit Exceeded

#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;

const int mod = 998244353;

int pd[310][310][310];
string s;
int k;

vector<int> cnts;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> s >> k;

  int lim = 2;

  int cnt = 0;
  for (int i = 0; i < (int)s.size(); i++) {
    if (s[i] == '0') {
      cnts.push_back(cnt);
      cnt = 0;
    } else {
      cnt++;
      lim++;
    }
  }

  cnts.push_back(cnt);
  int n = cnts.size();
  k = min(k, lim);

  pd[0][k][0] = 1;

  for (int pos = 0; pos < n; pos++) {
    for (int rem = 0; rem <= k; rem++) {
      for (int curd = 0; curd <= k; curd++) {
        for (int use = -min(cnts[pos], curd); use <= rem; use++) {

          int nrem = rem;
          if (use > 0)
            nrem -= use;
          int nuse = curd + use;

          if (nrem < 0 || nuse < 0 || nuse >= lim)
            continue;

          pd[pos + 1][nrem][nuse] =
              (pd[pos + 1][nrem][nuse] + pd[pos][rem][curd]) % mod;
        }
      }
    }
  }

  int ans = 0;
  for (int rem = 0; rem <= k; rem++) {
    ans = (ans + pd[n][rem][0]) % mod;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;

const int mod = 998244353;

int pd[310][310][310];
string s;
int k;

vector<int> cnts;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> s >> k;

  int cnt = 0;
  for (int i = 0; i < (int)s.size(); i++) {
    if (s[i] == '0') {
      cnts.push_back(cnt);
      cnt = 0;
    } else
      cnt++;
  }

  cnts.push_back(cnt);
  int n = cnts.size();
  k = min(k, 302);

  pd[0][k][0] = 1;

  for (int pos = 0; pos < n; pos++) {
    for (int rem = 0; rem <= k; rem++) {
      for (int curd = 0; curd <= k; curd++) {
        for (int use = -min(cnts[pos], curd); use <= rem; use++) {

          int nrem = rem;
          if (use > 0)
            nrem -= use;
          int nuse = curd + use;

          if (nrem < 0 || nuse < 0 || nuse >= 302)
            continue;

          pd[pos + 1][nrem][nuse] =
              (pd[pos + 1][nrem][nuse] + pd[pos][rem][curd]) % mod;
        }
      }
    }
  }

  int ans = 0;
  for (int rem = 0; rem <= k; rem++) {
    ans = (ans + pd[n][rem][0]) % mod;
  }

  cout << ans << endl;
}
Problem p02635 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, a, b) for (int i = (b); i-- > (a);)
#define all(v) (v).begin(), (v).end()
#define trav(x, v) for (auto &x : v)
#define sz(v) int(v.size())
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const ll md = 998244353;

ll mem[301][301][301] = {};

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  string s;
  int k;
  cin >> s >> k;
  s.push_back('0');
  k = min(k, 300);

  vi ls;
  for (int i = 0, cnt = 0; i < sz(s); ++i) {
    if (s[i] == '0') {
      ls.push_back(cnt);
      cnt = 0;
    } else
      ++cnt;
  }
  while (!ls.empty() && ls.back() == 0)
    ls.pop_back();

  mem[0][0][k] = 1;

  rep(i, 0, sz(ls)) {
    rep(t, 0, 301) rep(c, 0, k + 1) {
      ll cur = mem[i][t][c];
      cur %= md;
      mem[i + 1][t][c] += cur; // neutral
      rep(x, 1, c + 1) if (t + x <= 300) mem[i + 1][t + x][c - x] +=
          cur;                                                  // get stuff
      rep(x, 1, 1 + min(ls[i], t)) mem[i + 1][t - x][c] += cur; // give stuff
    }
  }

  ll ans = 0;
  rep(c, 0, k + 1) ans += mem[sz(ls)][0][c];
  ans %= md;
  if (ans < 0)
    ans += md;
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, a, b) for (int i = (b); i-- > (a);)
#define all(v) (v).begin(), (v).end()
#define trav(x, v) for (auto &x : v)
#define sz(v) int(v.size())
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const ll md = 998244353;

ll mem[301][301][301];

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  rep(a, 0, 301) rep(b, 0, 301) rep(c, 0, 301) mem[a][b][c] = -1;

  string s;
  int k;
  cin >> s >> k;
  s.push_back('0');

  vi ls;
  for (int i = 0, cnt = 0; i < sz(s); ++i) {
    if (s[i] == '0') {
      ls.push_back(cnt);
      cnt = 0;
    } else
      ++cnt;
  }
  while (!ls.empty() && ls.back() == 0)
    ls.pop_back();

  function<ll(int, int, int)> calc = [&](int i, int t, int c) {
    if (t > 300)
      return 0ll;
    if (mem[i][t][c] == -1) {
      ll cur = 0;

      if (i == sz(ls))
        cur = t == 0;
      else {
        cur += calc(i + 1, t, c);                                  // neutral
        rep(x, 1, 1 + c) cur += calc(i + 1, t + x, c - x);         // need stuff
        rep(x, 1, 1 + min(ls[i], t)) cur += calc(i + 1, t - x, c); // give stuff

        cur %= md;
        if (cur < 0)
          cur += md;
      }

      mem[i][t][c] = cur;
    }
    return mem[i][t][c];
  };
  cout << calc(0, 0, min(k, 300)) << endl;
}
Problem p02635 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 998244353;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string S;
  ll K;
  cin >> S >> K;
  int N = S.size();
  int cnt = 0;
  vector<int> C;
  rep(i, N) {
    if (S[i] == '1')
      cnt++;
    else {
      C.push_back(cnt);
      cnt = 0;
    }
  }
  C.push_back(cnt);
  reverse(all(C));
  int n = C.size();
  int m = accumulate(all(C), 0);
  vector<vector<vector<ll>>> dp(n + 1,
                                vector<vector<ll>>(m + 1, vector<ll>(m + 1)));
  dp[0][0][0] = 1;
  rep(i, n) {       // i番目の区間
    rep(j, m + 1) { // 取り出した回数
      if (j > K)
        continue;
      rep(k, m + 1) { // 挿入した回数
        int rem = j - k;
        if (rem < 0)
          continue;
        rep(l, rem + 1) { // ここで挿入する回数
          (dp[i + 1][j][k + l] += dp[i][j][k]) %= mod;
        }
        rep(l, C[i] + 1) { // ここで取り出す回数
          if (j + l > m)
            continue;
          if (l == 0)
            continue;
          (dp[i + 1][j + l][k] += dp[i][j][k]) %= mod;
        }
      }
    }
  }
  ll ans = 0;
  rep(i, min(K + 1, (ll)m + 1))(ans += dp[n][i][i]) %= mod;
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 998244353;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string S;
  ll K;
  cin >> S >> K;
  int N = S.size();
  int cnt = 0;
  vector<int> C;
  rep(i, N) {
    if (S[i] == '1')
      cnt++;
    else {
      C.push_back(cnt);
      cnt = 0;
    }
  }
  C.push_back(cnt);
  reverse(all(C));
  int n = C.size();
  int m = accumulate(all(C), 0);
  vector<vector<vector<ll>>> dp(n + 1,
                                vector<vector<ll>>(m + 1, vector<ll>(m + 1)));
  dp[0][0][0] = 1;
  rep(i, n) {       // i番目の区間
    rep(j, m + 1) { // 取り出した回数
      if (j > K)
        continue;
      rep(k, m + 1) { // 挿入した回数
        int rem = j - k;
        if (rem < 0)
          continue;
        rep(l, rem + 1) { // ここで挿入する回数
          (dp[i + 1][j][k + l] += dp[i][j][k]) %= mod;
        }
        rep(l, C[i] + 1) { // ここで取り出す回数
          if (j + l > m)
            continue;
          if (l == 0)
            continue;
          (dp[i + 1][j + l][k] += dp[i][j][k]) %= mod;
        }
      }
    }
  }
  ll ans = 0;
  rep(i, K + 1)(ans += dp[n][i][i]) %= mod;
  cout << ans << endl;
  return 0;
}
Problem p02635 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long dp[405][405][405];
long long MOD = 998244353;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s;
  int K;
  cin >> s >> K;
  vector<int> v;
  int cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '0') {
      v.push_back(cnt);
      cnt = 0;
    } else
      cnt++;
    if (i + 1 == s.size())
      v.push_back(cnt);
  }
  // for(int i:v) cout<<i<<endl;
  int n = v.size(), MAXN = 400;
  memset(dp, 0, sizeof dp);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= MAXN; j++) {
      for (int k = 0; k <= MAXN; k++) {
        if (j < k)
          continue;
        if (i == 0) {
          if (j == k)
            dp[i][j][k] = 1;
          continue;
        }
        for (int a = 0; a <= v[i]; a++) {
          if (j - a < 0 || k + a > MAXN)
            break;
          dp[i][j][k] += dp[i - 1][j][k + a];
          // if(i==1 && j==1 && k==0) cout<<i-1<<" "<<j<<" "<<k+a<<endl;
        }
        for (int a = 1; a <= k; a++) {
          if (j - a < 0 || k + a > MAXN)
            break;
          dp[i][j][k] += dp[i - 1][j - a][k - a];
          // if(i==1 && j==1 && k==0) cout<<i-1<<" "<<j-a<<" "<<k-a<<endl;
        }
        dp[i][j][k] %= MOD;
        // cout<<i<<" "<<j<<" "<<k<<": "<<dp[i][j][k]<<endl;
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i <= min(K, MAXN); i++) {
    ans = (ans + dp[n - 1][i][0]) % MOD;
  }
  cout << ans;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long dp[305][305][305];
long long MOD = 998244353;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s;
  int K;
  cin >> s >> K;
  vector<int> v;
  int cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '0') {
      v.push_back(cnt);
      cnt = 0;
    } else
      cnt++;
    if (i + 1 == s.size())
      v.push_back(cnt);
  }
  // for(int i:v) cout<<i<<endl;
  int n = v.size(), MAXN = 300;
  memset(dp, 0, sizeof dp);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= MAXN; j++) {
      for (int k = 0; k <= MAXN; k++) {
        if (i == 0) {
          if (j == k)
            dp[i][j][k] = 1;
          continue;
        }
        for (int a = 0; a <= v[i]; a++) {
          if (j - a < 0 || k + a > MAXN)
            break;
          dp[i][j][k] += dp[i - 1][j][k + a];
          // if(i==1 && j==1 && k==0) cout<<i-1<<" "<<j<<" "<<k+a<<endl;
        }
        for (int a = 1; a <= k; a++) {
          if (j - a < 0 || k + a > MAXN)
            break;
          dp[i][j][k] += dp[i - 1][j - a][k - a];
          // if(i==1 && j==1 && k==0) cout<<i-1<<" "<<j-a<<" "<<k-a<<endl;
        }
        dp[i][j][k] %= MOD;
        // cout<<i<<" "<<j<<" "<<k<<": "<<dp[i][j][k]<<endl;
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i <= K; i++) {
    ans = (ans + dp[n - 1][i][0]) % MOD;
  }
  cout << ans;
}
Problem p02635 (C++) — buggy submission, verdict: Runtime Error

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

const int MOD = 998244353;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;

ll dp[400][400][400];
int a[400];
int d[400];

int main() {
  string s;
  cin >> s;
  int K;
  cin >> K;
  int n = 0;
  int cnt = 0;
  for (char c : s) {
    if (c == '0')
      cnt++;
    else
      a[n++] = cnt;
  }
  rep(i, n) { d[a[i]]++; }
  K = min(K, n);
  dp[cnt + 1][0][0] = 1;
  for (int i = cnt + 1; i >= 1; i--)
    rep(j, K + 1) rep(k, n + 1) {
      if (dp[i][j][k] == 0)
        continue;
      for (int t = 0; t <= d[i - 1] + k; t++) {
        int nj = j + max(0, d[i - 1] - t);
        //~ if(nj>K)break;
        (dp[i - 1][nj][d[i - 1] + k - t] += dp[i][j][k]) %= MOD;
      }
    }
  ll ans = 0;
  rep(i, K + 1) { (ans += dp[0][i][0]) %= MOD; }
  cout << ans << endl;
}

Error: -11

Fixed submission:

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

const int MOD = 998244353;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;

ll dp[400][400][400];
int a[400];
int d[400];

int main() {
  string s;
  cin >> s;
  int K;
  cin >> K;
  int n = 0;
  int cnt = 0;
  for (char c : s) {
    if (c == '0')
      cnt++;
    else
      a[n++] = cnt;
  }
  rep(i, n) { d[a[i]]++; }
  dp[cnt + 1][0][0] = 1;
  for (int i = cnt + 1; i >= 1; i--)
    rep(j, K + 1) rep(k, n + 1) {
      if (dp[i][j][k] == 0)
        continue;
      for (int t = 0; t <= d[i - 1] + k; t++) {
        int nj = j + max(0, d[i - 1] - t);
        //~ if(nj>K)break;
        (dp[i - 1][nj][d[i - 1] + k - t] += dp[i][j][k]) %= MOD;
      }
    }
  ll ans = 0;
  rep(i, K + 1) { (ans += dp[0][i][0]) %= MOD; }
  cout << ans << endl;
}
Problem p02635 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
#define RI register int
#define CI const int &
using namespace std;
const int N = 305, mod = 998244353;
char s[N];
int n, m, t, lim, a[N], f[N << 1][N][N << 1],
    ans; // f[x][y][z]: x turns; position y; delta z
inline void inc(int &x, CI y) {
  if ((x += y) >= mod)
    x -= mod;
}
int main() {
  RI i, j, k, p;
  for (scanf("%s%d", s + 1, &t), n = strlen(s + 1), m = i = 1; i <= n; ++i)
    if (s[i] == '0')
      ++m;
    else
      ++a[m];
  if (!a[m])
    --m;
  f[0][0][0] = 1;
  lim = (min(t, n) << 1);
  for (j = 0; j < m; ++j)
    for (i = 0; i <= lim; ++i)
      for (k = 0; k <= n; ++k)
        if (f[i][j][k]) {
          inc(f[i][j + 1][k], f[i][j][k]); // do nothing
          for (p = 1; i + p <= lim; ++p)
            inc(f[i + p][j + 1][k + p], f[i][j][k]); // addition
          for (p = 1; p <= min(k, a[j + 1]) && i + p <= lim; ++p)
            inc(f[i + p][j + 1][k - p], f[i][j][k]); // subtraction
        }
  for (i = 0; i <= (min(t, n) << 1); ++i)
    inc(ans, f[i][m][0]);
  return printf("%d", ans), 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#define RI register int
#define CI const int &
using namespace std;
const int N = 305, mod = 998244353;
char s[N];
int n, m, t, a[N], f[N << 1][N][N << 1],
    ans; // f[x][y][z]: x turns; position y; delta z
inline void inc(int &x, CI y) {
  if ((x += y) >= mod)
    x -= mod;
}
int main() {
  RI i, j, k, p;
  for (scanf("%s%d", s + 1, &t), n = strlen(s + 1), m = i = 1; i <= n; ++i)
    if (s[i] == '0')
      ++m;
    else
      ++a[m];
  if (!a[m])
    --m;
  for (i = 0; i <= m; ++i)
    f[0][i][0] = 1;
  for (j = 1; j <= m; ++j)
    for (i = 1; i <= (min(t, n) << 1); ++i)
      for (k = 0; k <= n; ++k) {
        inc(f[i][j][k], f[i][j - 1][k]); // do nothing
        for (p = 1; p <= min(i, k); ++p)
          inc(f[i][j][k], f[i - p][j - 1][k - p]); // addition
        for (p = 1; p <= min(i, a[j]) && k + p <= n; ++p)
          inc(f[i][j][k], f[i - p][j - 1][k + p]); // subtraction
      }
  for (i = 0; i <= (min(t, n) << 1); ++i)
    inc(ans, f[i][m][0]);
  return printf("%d", ans), 0;
}
Problem p02635 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#define For(i, a, b) for (register int i = a; i <= b; ++i)
using namespace std;
const int maxn = 3e2 + 5;
const int mod = 998244353;
int f[maxn][maxn][maxn], K, N, n, num[maxn], ans;
// x     k     owe
char s[maxn];
void Add(int &x, const int &a) { x = (x + a) >= mod ? x + a - mod : x + a; }

int main() {
  scanf("%s%d", s, &K);
  N = strlen(s), n = 1;
  f[0][0][0] = 1;
  For(i, 0, N - 1) {
    if (s[i] == '0')
      ++n;
    if (s[i] == '1')
      ++num[n];
  }
  K = min(K, N);
  For(i, 0, n - 1) For(j, 0, K) For(k, 0, j) {
    Add(f[i + 1][j][k], f[i][j][k]);
    // in
    For(l, 1, K - j) Add(f[i + 1][j + l][k + l], f[i][j][k]);
    // out
    For(l, 1, min(k, num[i + 1])) Add(f[i + 1][j][k - l], f[i][j][k]);
  }
  For(i, 0, K) Add(ans, f[n][i][0]);
  printf("%d", ans);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#define For(i, a, b) for (register int i = a; i <= b; ++i)
using namespace std;
const int maxn = 3e2 + 5;
const int mod = 998244353;
int f[maxn][maxn][maxn], K, N, n, num[maxn], ans;
// x     k     owe
char s[maxn];
void Add(int &x, const int &a) { x = (x + a) >= mod ? x + a - mod : x + a; }

int main() {
  scanf("%s%d", s, &K);
  N = strlen(s), n = 1;
  f[0][0][0] = 1;
  For(i, 0, N - 1) {
    if (s[i] == '0')
      ++n;
    if (s[i] == '1')
      ++num[n];
  }
  For(i, 0, n - 1) For(j, 0, K) For(k, 0, j) {
    Add(f[i + 1][j][k], f[i][j][k]);
    // in
    For(l, 1, K - j) Add(f[i + 1][j + l][k + l], f[i][j][k]);
    // out
    For(l, 1, min(k, num[i + 1])) Add(f[i + 1][j][k - l], f[i][j][k]);
  }
  For(i, 0, K) Add(ans, f[n][i][0]);
  printf("%d", ans);
  return 0;
}