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

#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 = (a); i >= (b); --i)
#define PB push_back
#define ar2 array<int, 2>
#define vs vector<string>
typedef long long LL;
const int P = 998244353;
const int N = 305;
mt19937 rng(time(0));

int T, n, K, n0 = 0, n1;
char s[N];
int a[N], sum[N], dp[N][N][N], sdp[N][N][N];
// i-th, j-same, k-max

void madd(int &x, int y) {
  x += y;
  x = (x >= P ? x - P : x);
}

int main() {
  cin >> (s + 1) >> K;
  n = strlen(s + 1);
  rep(i, 1, n) if (s[i] == '0') n0++;
  else a[n0]++;
  sum[0] = a[0];
  rep(i, 1, n0) sum[i] = sum[i - 1] + a[i];
  n1 = n - n0;

  rep(i, 0, n1) rep(j, 0, i) {
    dp[i][j][0] = sdp[i][j][0] = (min(i, a[0]) == j ? 1 : 0);
    // cout<<i<<" "<<j<<" 0 "<<dp[i][j][0]<<endl;
    rep(k, 1, n0) {
      if (sum[k] > i) {
        sdp[i][j][k] = sdp[i][j][k - 1];
        continue;
      }
      rep(t, sum[k - 1], i - 1) {
        if (j - min(i - t, a[k]) < 0)
          continue;
        madd(dp[i][j][k], sdp[t][j - min(i - t, a[k])][k - 1]);
      }
      sdp[i][j][k] = sdp[i][j][k - 1];
      madd(sdp[i][j][k], dp[i][j][k]);
      // cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl;
    }
  }

  int ans = 0;
  rep(j, max(0, n1 - K), n1) madd(ans, sdp[n1][j][n0]);
  cout << ans << endl;
  return 0;
}

Error: -11

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 = (a); i >= (b); --i)
#define PB push_back
#define ar2 array<int, 2>
#define vs vector<string>
typedef long long LL;
const int P = 998244353;
const int N = 305;
mt19937 rng(time(0));

int T, n, K, n0 = 0, n1;
char s[N];
int a[N], sum[N], dp[N][N][N], sdp[N][N][N];
// i-th, j-same, k-max

void madd(int &x, int y) {
  x += y;
  x = (x >= P ? x - P : x);
}

int main() {
  cin >> (s + 1) >> K;
  n = strlen(s + 1);
  rep(i, 1, n) if (s[i] == '0') n0++;
  else a[n0]++;
  sum[0] = a[0];
  rep(i, 1, n0) sum[i] = sum[i - 1] + a[i];
  n1 = n - n0;

  rep(i, 0, n1) rep(j, 0, i) {
    dp[i][j][0] = sdp[i][j][0] = (min(i, a[0]) == j ? 1 : 0);
    // cout<<i<<" "<<j<<" 0 "<<dp[i][j][0]<<endl;
    rep(k, 1, n0) {
      if (sum[k] > i) {
        sdp[i][j][k] = sdp[i][j][k - 1];
        continue;
      }
      rep(t, sum[k - 1], i - 1) {
        madd(dp[i][j][k], sdp[t][j - min(i - t, a[k])][k - 1]);
      }
      sdp[i][j][k] = sdp[i][j][k - 1];
      madd(sdp[i][j][k], dp[i][j][k]);
      // cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl;
    }
  }

  int ans = 0;
  rep(j, max(0, n1 - K), n1) madd(ans, sdp[n1][j][n0]);
  cout << ans << endl;
  return 0;
}
Problem p02635 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int maxn = 300 + 10;
typedef long long ll;
const ll mod = 998244353;
int tot, K;
char s[maxn];
int a[maxn];
ll f[maxn][maxn][maxn];
ll ans;
inline ll norm(const ll &x) { return x >= mod ? x - mod : x; }
int main() {
  scanf("%s%d", s + 1, &K);
  K = min(K, 300);
  int len = strlen(s + 1);
  reverse(s + 1, s + len + 1);
  int cur = 0;
  for (int i = 1; i <= len; ++i)
    if (s[i] == '0') {
      a[++tot] = cur;
      cur = 0;
    } else
      ++cur;
  a[++tot] = cur;
  f[0][0][0] = 1;
  for (int i = 1; i <= tot; ++i)
    for (int k = 0; k <= K; ++k) {
      for (int j = k; ~j; --j)
        f[i][j][k] = norm(f[i - 1][j][k] + f[i][j + 1][k]);
      for (int j = 0; j <= k; ++j) {
        /*
        for(int jd = j; jd <= k; ++jd)
                f[i][j][k] = (f[i][j][k] + f[i - 1][jd][k]) % mod;
        */
        int lim = min(j, a[i]);
        for (int jd = 1; jd <= lim; ++jd)
          f[i][j][k] = norm(f[i][j][k] + f[i - 1][j - jd][k - jd]);
      }
    }
  for (int k = 0; k <= K; ++k)
    ans = norm(ans + f[tot][0][k]);
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 300 + 10;
typedef long long ll;
const ll mod = 998244353;
int tot, K;
char s[maxn];
int a[maxn];
ll f[maxn][maxn][maxn];
ll ans;
inline ll norm(const ll &x) { return x >= mod ? x - mod : x; }
int main() {
  scanf("%s%d", s + 1, &K);
  int len = strlen(s + 1);
  reverse(s + 1, s + len + 1);
  int cur = 0;
  for (int i = 1; i <= len; ++i)
    if (s[i] == '0') {
      a[++tot] = cur;
      cur = 0;
    } else
      ++cur;
  a[++tot] = cur;
  f[0][0][0] = 1;
  for (int i = 1; i <= tot; ++i)
    for (int k = 0; k <= K; ++k) {
      for (int j = k; ~j; --j)
        f[i][j][k] = norm(f[i - 1][j][k] + f[i][j + 1][k]);
      for (int j = 0; j <= k; ++j) {
        /*
        for(int jd = j; jd <= k; ++jd)
                f[i][j][k] = (f[i][j][k] + f[i - 1][jd][k]) % mod;
        */
        int lim = min(j, a[i]);
        for (int jd = 1; jd <= lim; ++jd)
          f[i][j][k] = norm(f[i][j][k] + f[i - 1][j - jd][k - jd]);
      }
    }
  for (int k = 0; k <= K; ++k)
    ans = norm(ans + f[tot][0][k]);
  cout << ans << endl;
  return 0;
}
Problem p02635 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 300 + 10, mod = 998244353;
void add(ll &x, ll y) { (x += y) %= mod; }
char s[N];
int n, k;
ll f[N][N], s1[N][N], s2[N][N];
void calc() {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      s1[i][j] = s2[i][j] = f[i][j];
      if (i - 1 >= 0 && j - 1 >= 0)
        add(s1[i][j], s1[i - 1][j - 1]);
      if (j - 1 >= 0)
        add(s2[i][j], s2[i][j - 1]);
    }
  }
}
int main() {
  cin >> s >> k;
  n = strlen(s);
  f[0][0] = 1;
  calc();
  for (int i = 0, j = 0; i < n; i = j = j + 1) {
    while (s[j] == '1')
      j++;
    int c = j - i;
    for (int x = 0; x <= n; x++) {
      for (int y = 0; y <= n; y++) {
        f[x][y] = 0;
        add(f[x][y], s2[x][min(y + c, n)] - s2[x][y] + mod);
        add(f[x][y], s1[x][y]);
      }
    }
    calc();
  }
  ll ans = 0;
  for (int i = 0; i <= min(n, k); i++)
    add(ans, f[i][0]);
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 300 + 10, mod = 998244353;
void add(ll &x, ll y) { (x += y) %= mod; }
char s[N];
int n, k;
ll f[N][N], s1[N][N], s2[N][N];
void calc() {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      s1[i][j] = s2[i][j] = f[i][j];
      if (i - 1 >= 0 && j - 1 >= 0)
        add(s1[i][j], s1[i - 1][j - 1]);
      if (j - 1 >= 0)
        add(s2[i][j], s2[i][j - 1]);
    }
  }
}
int main() {
  cin >> s >> k;
  n = strlen(s);
  f[0][0] = 1;
  calc();
  for (int i = 0, j = 0; i < n; i = j = j + 1) {
    while (s[j] == '1')
      j++;
    int c = j - i;
    for (int x = 0; x <= n; x++) {
      for (int y = 0; y <= n; y++) {
        f[x][y] = 0;
        add(f[x][y], s2[x][min(y + c, n)] - s2[x][y]);
        add(f[x][y], s1[x][y]);
      }
    }
    calc();
  }
  ll ans = 0;
  for (int i = 0; i <= k; i++)
    add(ans, f[i][0]);
  cout << ans << endl;
}
Problem p02635 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int maxn = 305;
const int mod = 998244353;
vector<int> v;
int mem[2][maxn][maxn][maxn];
int N, K;

int dp(int state, int cur, int moves, int need) {
  if (moves > K || moves > N)
    return 0;
  if (cur == v.size()) {
    return need == 0;
  }
  int &ans = mem[state][cur][moves][need];
  if (ans != -1)
    return ans;
  ans = dp(0, cur + 1, moves, need);
  if (state == 0) {
    ans += dp(1, cur, moves + 1, need + 1);
    ans %= mod;
    for (int i = 1; i <= v[cur] && i <= need; i++) {
      ans += dp(0, cur + 1, moves, need - i);
      ans %= mod;
    }
  } else {
    ans += dp(1, cur, moves + 1, need + 1);
    ans %= mod;
  }
  return ans;
}
int main() {
  string s;
  cin >> s >> K;
  N = s.size();
  int cnt = 0;
  for (auto i : s) {
    if (i == '0') {
      v.push_back(cnt);
      cnt = 0;
    } else
      cnt = cnt + 1;
  }
  v.push_back(cnt);
  memset(mem, -1, sizeof mem);
  cout << dp(0, 0, 0, 0) << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 305;
const int mod = 998244353;
vector<int> v;
int mem[2][maxn][maxn][maxn];
int K;

int dp(int state, int cur, int moves, int need) {
  if (moves > K)
    return 0;
  if (cur == v.size()) {
    return need == 0;
  }
  int &ans = mem[state][cur][moves][need];
  if (ans != -1)
    return ans;
  ans = dp(0, cur + 1, moves, need);
  if (state == 0) {
    ans += dp(1, cur, moves + 1, need + 1);
    ans %= mod;
    for (int i = 1; i <= v[cur] && i <= need; i++) {
      ans += dp(0, cur + 1, moves, need - i);
      ans %= mod;
    }
  } else {
    ans += dp(1, cur, moves + 1, need + 1);
    ans %= mod;
  }
  return ans;
}
int main() {
  string s;
  cin >> s >> K;
  int cnt = 0;
  for (auto i : s) {
    if (i == '0') {
      v.push_back(cnt);
      cnt = 0;
    } else
      cnt = cnt + 1;
  }
  v.push_back(cnt);
  memset(mem, -1, sizeof mem);
  cout << dp(0, 0, 0, 0) << endl;
  return 0;
}
Problem p02635 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
constexpr ll mod = 998244353;
ll dp[301][301][301] = {}, dp2[301][301] = {}, dp3[301][301] = {};
vector<int> v;
int main() {
  int i, j, k, l, r;
  string s;
  cin >> s >> k;
  s.push_back('0');
  int n = s.size();
  for (i = 0; i < n; i++) {
    if (s[i] == '0') {
      int cnt = 0;
      if (i == 0)
        v.push_back(0);
      for (j = i - 1; j >= 0; j--) {
        if (j != 0 && s[j] == '1') {
          cnt++;
        } else {
          v.push_back(cnt);
          break;
        }
      }
    }
  }
  int m = v.size(), sum = 300;
  dp[m][0][0] = 1;
  for (i = m - 1; i >= 0; i--) {
    for (l = 0; l <= sum; l++) {
      dp2[sum][l] = dp[i + 1][sum][l];
      for (j = sum - 1; j >= 0; j--) {
        dp2[j][l] = dp2[j + 1][l];
        (dp2[j][l] += dp[i + 1][j][l]);
      }
    }
    for (j = 0; j <= sum; j++) {
      for (l = 0; l <= sum; l++) {
        (dp[i][j][l] += dp2[j][l]) %= mod;
        for (r = 1; r <= min({j, l, v[i]}); r++) {
          (dp[i][j][l] += dp[i + 1][j - r][l - r]) %= mod;
        }
      }
    }
  }
  ll ans = 0;
  for (i = 0; i <= min(300, k); i++) {
    (ans += dp[0][0][i]) %= mod;
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
constexpr ll mod = 998244353;
ll dp[305][305][305] = {}, dp2[305][305] = {}, dp3[305][305] = {};
vector<int> v;
int main() {
  int i, j, k, l, r;
  string s;
  cin >> s >> k;
  s.push_back('0');
  int n = s.size();
  for (i = 0; i < n; i++) {
    if (s[i] == '0') {
      int cnt = 0;
      if (i == 0)
        v.push_back(0);
      for (j = i - 1; j >= 0; j--) {
        if (j != 0 && s[j] == '1') {
          cnt++;
        } else {
          v.push_back(cnt);
          break;
        }
      }
    }
  }
  int m = v.size(), sum = 300;
  dp[m][0][0] = 1;
  for (i = m - 1; i >= 0; i--) {
    for (l = 0; l <= sum; l++) {
      dp2[sum][l] = dp[i + 1][sum][l];
      for (j = sum - 1; j >= 0; j--) {
        dp2[j][l] = dp2[j + 1][l];
        (dp2[j][l] += dp[i + 1][j][l]);
      }
    }
    for (j = 0; j <= sum; j++) {
      for (l = 0; l <= sum; l++) {
        (dp[i][j][l] += dp2[j][l]) %= mod;
        for (r = 1; r <= min({j, l, v[i]}); r++) {
          (dp[i][j][l] += dp[i + 1][j - r][l - r]) %= mod;
        }
      }
    }
  }
  ll ans = 0;
  for (i = 0; i <= k; i++) {
    (ans += dp[0][0][i]) %= mod;
  }
  cout << ans << endl;
}
Problem p02635 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

string S;
ll N;
ll K;
vector<ll> A;
ll M;
vector<vector<vector<ll>>> Dp;
const ll MOD = 998244353;
ll Res;

int main() {
  cin >> S >> K;
  N = S.size();
  K = min(N, K);
  reverse(S.begin(), S.end());
  ll now = 0;
  for (char c : S) {
    if (c == '0') {
      A.push_back(now);
      now = 0;
    } else {
      ++now;
    }
  }
  A.push_back(now);
  M = A.size();
  Dp.resize(M + 1);
  for (auto &Dpi : Dp) {
    Dpi.resize(N + 1);
    for (auto &Dpij : Dpi)
      Dpij.resize(K + 1);
  }
  Dp[0][0][0] = 1;
  for (ll i = 0; i < M; ++i) {
    for (ll j = N; j >= 0; --j) {
      for (ll k = 0; k <= K; ++k) {
        (Dp[i + 1][j][k] += Dp[i][j][k]) %= MOD;
        if (j != N) {
          (Dp[i + 1][j][k] += Dp[i + 1][j + 1][k]) %= MOD;
        }
      }
    }
    for (ll j = 0; j <= N; ++j) {
      for (ll k = 0; k <= K; ++k) {
        for (ll a = 1; a <= A[i] && j + a <= N && k + a <= K; ++a) {
          (Dp[i + 1][j + a][k + a] += Dp[i][j][k]) %= MOD;
        }
      }
    }
  }
  for (ll k = 0; k <= K; ++k) {
    (Res += Dp[M][0][k]) %= MOD;
  }
  cout << Res << endl;
  return 0;
}


Error: 0

Fixed submission:

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

typedef long long ll;

string S;
ll N;
ll K;
vector<ll> A;
ll M;
vector<vector<vector<ll>>> Dp;
const ll MOD = 998244353;
ll Res;

int main() {
  cin >> S >> K;
  N = S.size();
  reverse(S.begin(), S.end());
  ll now = 0;
  for (char c : S) {
    if (c == '0') {
      A.push_back(now);
      now = 0;
    } else {
      ++now;
    }
  }
  A.push_back(now);
  M = A.size();
  Dp.resize(M + 1);
  for (auto &Dpi : Dp) {
    Dpi.resize(N + 1);
    for (auto &Dpij : Dpi)
      Dpij.resize(K + 1);
  }
  Dp[0][0][0] = 1;
  for (ll i = 0; i < M; ++i) {
    for (ll j = N; j >= 0; --j) {
      for (ll k = 0; k <= K; ++k) {
        (Dp[i + 1][j][k] += Dp[i][j][k]) %= MOD;
        if (j != N) {
          (Dp[i + 1][j][k] += Dp[i + 1][j + 1][k]) %= MOD;
        }
      }
    }
    for (ll j = 0; j <= N; ++j) {
      for (ll k = 0; k <= K; ++k) {
        for (ll a = 1; a <= A[i] && j + a <= N && k + a <= K; ++a) {
          (Dp[i + 1][j + a][k + a] += Dp[i][j][k]) %= MOD;
        }
      }
    }
  }
  for (ll k = 0; k <= K; ++k) {
    (Res += Dp[M][0][k]) %= MOD;
  }
  cout << Res << endl;
  return 0;
}
Problem p02635 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const long long mod = 998244353;
string S;
int K, no = 0;
vector<int> cnt;
vector<int> pfx;
long long dptable[301][301][301][2];
long long dp(int a, int b, int c, bool d) {
  if (b > no || c < 0)
    return 0;
  if (a == cnt.size())
    return 1;
  if (dptable[a][b][c][d] != -1)
    return dptable[a][b][c][d];
  if (d == true) {
    if (a + 1 == cnt.size())
      return dptable[a][b][c][d] = dp(a + 1, b, c, d);
    dptable[a][b][c][d] = 0;
    for (int i = max(b, pfx[a + 1]); i < b + cnt[a + 1]; ++i)
      dptable[a][b][c][d] += dp(a + 1, i, c, 1), dptable[a][b][c][d] %= mod;
    dptable[a][b][c][d] =
        (dptable[a][b][c][d] + dp(a + 1, b + cnt[a + 1], c, 0)) % mod;
    return dptable[a][b][c][d];
  } else
    return dptable[a][b][c][d] =
               (dp(a, b, c, 1) + dp(a, b + 1, c - 1, 0)) % mod;
}
int main() {
  memset(dptable, -1, sizeof dptable);
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> S >> K;
  int x = 0;
  for (char c : S) {
    if (c == '1')
      ++x, ++no;
    else
      cnt.push_back(x), x = 0;
  }
  K = min(K, no);
  if (cnt.size() == 0) {
    cout << 1;
    return 0;
  }
  pfx.push_back(cnt[0]);
  for (int i = 1; i < cnt.size(); ++i)
    pfx.push_back(cnt[i] + pfx[i - 1]);
  cout << dp(0, cnt[0], K, false);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const long long mod = 998244353;
string S;
int K, no = 0;
vector<int> cnt;
vector<int> pfx;
long long dptable[301][301][301][2];
long long dp(int a, int b, int c, bool d) {
  if (b > no || c < 0)
    return 0;
  if (a == cnt.size())
    return 1;
  if (dptable[a][b][c][d] != -1)
    return dptable[a][b][c][d];
  if (d == true) {
    if (a + 1 == cnt.size())
      return dptable[a][b][c][d] = dp(a + 1, b, c, d);
    dptable[a][b][c][d] = 0;
    for (int i = max(b, pfx[a + 1]); i < b + cnt[a + 1]; ++i)
      dptable[a][b][c][d] += dp(a + 1, i, c, 1), dptable[a][b][c][d] %= mod;
    dptable[a][b][c][d] =
        (dptable[a][b][c][d] + dp(a + 1, b + cnt[a + 1], c, 0)) % mod;
    return dptable[a][b][c][d];
  } else
    return dptable[a][b][c][d] =
               (dp(a, b, c, 1) + dp(a, b + 1, c - 1, 0)) % mod;
}
int main() {
  memset(dptable, -1, sizeof dptable);
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> S >> K;
  int x = 0;
  for (char c : S) {
    if (c == '1')
      ++x, ++no;
    else
      cnt.push_back(x), x = 0;
  }
  K = min(K, no);
  pfx.push_back(cnt[0]);
  for (int i = 1; i < cnt.size(); ++i)
    pfx.push_back(cnt[i] + pfx[i - 1]);
  printf("%lld", dp(0, cnt[0], K, false));
}
Problem p02635 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 510000;
const int MOD = 998244353;

signed main() {
  string s;
  int K;
  cin >> s >> K;
  vector<int> c;
  c.push_back(0);
  int t = 0;
  for (auto &&i : s) {
    if (i == '0')
      c.push_back(0);
    else {
      c[c.size() - 1]++;
      t++;
    }
  }
  int dp[c.size() + 1][t + 1][301];
  for (int i = 0; i <= t; i++)
    fill(dp[0][i], dp[0][i] + 301, 0);
  dp[0][0][0] = 1;
  int o = 0;
  for (int i = 1; i <= c.size(); i++) {
    o += c[i - 1];
    for (int j = 0; j <= t; j++)
      fill(dp[i][j], dp[i][j] + 301, 0);
    for (int j = 0; j <= t; j++)
      for (int k = 0; k < 301; k++)
        for (int j2 = 0; j2 <= j; j2++)
          if (k - max(j - j2 - c[i - 1], 0ll) >= 0 && j >= o)
            dp[i][j][k] =
                (dp[i][j][k] + dp[i - 1][j2][k - max(j - j2 - c[i - 1], 0ll)]) %
                MOD;
  }
  int ans = 0;
  for (int i = 0; i <= min(K, 300ll); i++) {
    ans = (ans + dp[c.size()][t][i]) % MOD;
  } // cerr<<dp[c.size()][t][i]<<endl;}

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 510000;
const int MOD = 998244353;

signed main() {
  string s;
  int K;
  cin >> s >> K;
  vector<int> c;
  c.push_back(0);
  int t = 0;
  for (auto &&i : s) {
    if (i == '0')
      c.push_back(0);
    else {
      c[c.size() - 1]++;
      t++;
    }
  }
  int dp[c.size() + 1][t + 1][301];
  for (int i = 0; i <= t; i++)
    fill(dp[0][i], dp[0][i] + 301, 0);
  dp[0][0][0] = 1;
  int o = 0;
  for (int i = 1; i <= c.size(); i++) {
    o += c[i - 1];
    for (int j = 0; j <= t; j++)
      fill(dp[i][j], dp[i][j] + 301, 0);
    for (int j = 0; j <= t; j++)
      for (int k = 0; k < 301; k++)
        for (int j2 = 0; j2 <= j; j2++)
          if (k - max(j - j2 - c[i - 1], 0ll) >= 0 && j >= o)
            dp[i][j][k] =
                (dp[i][j][k] + dp[i - 1][j2][k - max(j - j2 - c[i - 1], 0ll)]) %
                MOD;
  }
  int ans = 0;
  for (int i = 0; i <= min(K, 301ll); i++) {
    ans = (ans + dp[c.size()][t][i]) % MOD;
  } // cerr<<dp[c.size()][t][i]<<endl;}

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

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

const int N = 305;
typedef long long LL;

LL f[N][N][N], mod = 998244353, ans;
int n, m, last, cnt, a[N], sum[N];
char s[N];

int main() {
  cin >> (s + 1) >> m;
  n = strlen(s + 1);
  m = min(m, n);
  for (int i = 1; i <= n; i++)
    if (s[i] == '0')
      a[++cnt] = i - last - 1, last = i;
  if (last != n)
    a[++cnt] = n - last;
  for (int i = cnt; i; i--)
    sum[i] = sum[i + 1] + a[i];
  f[cnt + 1][0][0] = 1;
  for (int i = cnt; i; i--)
    for (int j = 0; j <= m && j <= sum[i]; j++)
      for (int k = 0; k <= j; k++) {
        f[i][j][k] = f[i + 1][j][k];
        for (int p = 1; p <= a[i] && p <= j; p++)
          f[i][j][k] = (f[i][j][k] + f[i + 1][j - p][k - p]) % mod;
        for (int p = 1; p <= j - k; p++)
          f[i][j][k] = (f[i][j][k] + f[i + 1][j][k + p]) % mod;
      }
  for (int i = 0; i <= m; i++)
    ans = (ans + f[1][i][0]) % mod;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

const int N = 305;
typedef long long LL;

LL f[N][N][N], mod = 998244353, ans;
int n, m, last, cnt, a[N];
char s[N];

int main() {
  cin >> (s + 1) >> m;
  n = strlen(s + 1);
  m = min(m, n);
  for (int i = 1; i <= n; i++)
    if (s[i] == '0')
      a[++cnt] = i - last - 1, last = i;
  if (last != n)
    a[++cnt] = n - last;
  f[cnt + 1][0][0] = 1;
  for (int i = cnt; i; i--)
    for (int j = 0; j <= m; j++)
      for (int k = 0; k <= j; k++) {
        f[i][j][k] = f[i + 1][j][k];
        for (int p = 1; p <= a[i] && p <= j; p++)
          f[i][j][k] = (f[i][j][k] + f[i + 1][j - p][k - p]) % mod;
        for (int p = 1; p <= j - k; p++)
          f[i][j][k] = (f[i][j][k] + f[i + 1][j][k + p]) % mod;
      }
  for (int i = 0; i <= m; i++)
    ans = (ans + f[1][i][0]) % mod;
  cout << ans << endl;
  return 0;
}
Problem p02638 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
const int N = 205;
int n, k, p;
long long ksm(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1)
      res = res * a % p;
    a = a * a % p, b >>= 1;
  }
  return res;
}
long long fac[N], inv[N];
void init(int n = 200) {
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % p;
  inv[n] = ksm(fac[n], p - 2);
  for (int i = n; i >= 1; i--)
    inv[i - 1] = inv[i] * i % p;
  return;
}
long long P(int n, int m) {
  if (m > n)
    return 0;
  return fac[n] * inv[n - m] % p;
}
long long f[N][N];
long long sum[N][N];
int calc(int n, int m, int k) {
  f[0][m] = 1;
  for (int i = 1; i <= m - 1; i++)
    f[0][i] = 0;
  f[0][0] = -1;
  for (int j = 0; j <= m; j++) {
    sum[0][j] = j > 0 ? sum[0][j - 1] : 0;
    sum[0][j] = (sum[0][j] + f[0][j] + p) % p;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= m; j++) {
      f[i][j] = 0;
      if (i - 1 + j <= k && n - i + m - j <= k) {
        f[i][j] = (f[i][j] +
                   (sum[i - 1][m] - (j > 0 ? sum[i - 1][j - 1] : 0) + p) % p) %
                  p;
      }
      sum[i][j] = j > 0 ? sum[i][j - 1] : 0;
      sum[i][j] = (sum[i][j] + f[i][j]) % p;
    }
  return f[n][0];
}
long long solve(int n, int k) {
  if (n == 1)
    return 1;
  long long res = 0;
  for (int deg = 2; deg < n; deg++)
    res = (res + calc(deg, n - deg, k)) % p;
  return res * fac[n - 1] % p;
}
int main() {
  scanf("%d%d%d", &n, &k, &p);
  init();
  long long ans = 0;
  for (int i = 0; i <= k; i++)
    ans = (ans + P(n, i) * solve(n - i, k - i) % p) % p;
  printf("%lld", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
const int N = 205;
int n, k, p;
long long ksm(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1)
      res = res * a % p;
    a = a * a % p, b >>= 1;
  }
  return res;
}
long long fac[N], inv[N];
void init(int n = 200) {
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % p;
  inv[n] = ksm(fac[n], p - 2);
  for (int i = n; i >= 1; i--)
    inv[i - 1] = inv[i] * i % p;
  return;
}
long long P(int n, int m) {
  if (m > n)
    return 0;
  return fac[n] * inv[n - m] % p;
}
long long f[N][N];
int calc(int n, int m, int k) {
  for (int i = 1; i <= n; i++)
    for (int j = m; j >= 0; j--) {
      f[i][j] = 0;
      if (i - 1 + j <= k && n - i + m - j <= k) {
        if (i == 1 && j > 0)
          f[i][j] = 1;
        else {
          for (int q = m; q >= j; q--)
            f[i][j] = (f[i][j] + f[i - 1][q]) % p;
        }
      }
    }
  return f[n][0];
}
long long solve(int n, int k) {
  if (n == 1)
    return 1;
  long long res = 0;
  for (int deg = 2; deg < n; deg++)
    res = (res + calc(deg, n - deg, k)) % p;
  return res * fac[n - 1] % p;
}
int main() {
  scanf("%d%d%d", &n, &k, &p);
  init();
  long long ans = 0;
  for (int i = 0; i <= k; i++)
    ans = (ans + P(n, i) * solve(n - i, k - i) % p) % p;
  printf("%lld", ans);
  return 0;
}