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

#include <algorithm> //sort,reverse
#include <deque>     //double_ended queue
#include <iostream>
#include <map>   //map
#include <queue> //queue,priority_queue
#include <string>
#include <utility> //pair
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P; // Pでpair<-,->を表す。
const LL MAX = 2000010;
const LL MOD = 998244353;
typedef map<LL, LL>::iterator Itr;
/*ここから*/
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

/*ここまで*/

int main() {

  // 前処理
  COMinit();
  string s;
  cin >> s;
  LL K;
  cin >> K;
  LL n = s.size();
  LL n0 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      n0++;
    }
  }
  if (n0 == 0) {
    cout << 1 << endl;
    return 0;
  }
  LL n1[n0];
  LL n1_l = 0;
  LL dp2[310][310];
  LL dp[310][310];
  for (int k = 0; k < 310; k++) {
    for (int i = 0; i < 310; i++) {
      dp[k][i] = 0;
    }
  }
  LL cnt1 = 0;
  LL cnt2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      cnt1++;
    } else {
      n1[cnt2] = cnt1;
      cnt1 = 0;
      cnt2++;
    }
  }
  n1_l = cnt1;
  for (int i = 0; i < n0; i++) {
    for (int k = 0; k < 310; k++) {
      LL sum = 0;
      for (int j = 309; j >= 0; j--) {
        dp2[j][k] = dp[j][k];
        sum = (sum + dp[j][k]) % MOD;
        dp[j][k] = sum;
      }
    }
    if (i == 0) {
      for (int j = 0; j <= n1_l; j++) {
        dp[j][j] = 1;
      }
    } else {
      for (int j = 0; j < 310; j++) {
        for (int k = 0; k < 310; k++) {
          for (int l = 0; l <= n1[n0 - i]; l++) {
            if (l == 0) {
            } else {
              if (j + l < 310 && k + l < 310) {
                dp[j + l][k + l] = (dp[j + l][k + l] + dp2[j][k]) % MOD;
              }
            }
          }
        }
      }
    }
    /*for(int j=0;j<6;j++){
        for(int k=0;k<6;k++){
            cout<<dp[j][k]<<' ';
        }
        cout<<endl;
    }
    cout<<endl;*/
  }
  LL ans = 0;
  if (K > 305) {
    K = 305;
  }
  for (int i = 0; i <= K; i++) {
    for (int j = 0; j < 310; j++)
      ans = (ans + dp[j][i]) % MOD;
  }
  cout << ans << endl;
  return 0;
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <algorithm> //sort,reverse
#include <deque>     //double_ended queue
#include <iostream>
#include <map>   //map
#include <queue> //queue,priority_queue
#include <string>
#include <utility> //pair
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P; // Pでpair<-,->を表す。
const LL MAX = 2000010;
const LL MOD = 998244353;
typedef map<LL, LL>::iterator Itr;
/*ここから*/
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

/*ここまで*/

int main() {

  // 前処理
  COMinit();
  string s;
  cin >> s;
  LL K;
  cin >> K;
  LL n = s.size();
  LL n0 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      n0++;
    }
  }
  if (n0 == 0) {
    cout << 1 << endl;
    return 0;
  }
  LL n1[n0];
  LL n1_l = 0;
  LL dp2[310][310];
  LL dp[310][310];
  for (int k = 0; k < 310; k++) {
    for (int i = 0; i < 310; i++) {
      dp[k][i] = 0;
    }
  }
  LL cnt1 = 0;
  LL cnt2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      cnt1++;
    } else {
      n1[cnt2] = cnt1;
      cnt1 = 0;
      cnt2++;
    }
  }
  n1_l = cnt1;
  for (int i = 0; i < n0; i++) {
    LL sum = 0;
    for (int j = 310; j >= 0; j--) {
      for (int k = 0; k < 310; k++) {
        dp2[j][k] = dp[j][k];
        sum = (sum + dp[j][k]) % MOD;
        dp[j][k] = sum;
      }
    }
    if (i == 0) {
      for (int j = 0; j <= n1_l; j++) {
        dp[j][j] = 1;
      }
    } else {
      for (int j = 0; j < 310; j++) {
        for (int k = 0; k < 310; k++) {
          for (int l = 0; l < n1[n0 - i]; l++) {
            if (l == 0) {
            } else {
              if (j + l < 310 && k + l < 310) {
                dp[j + l][k + l] = (dp[j + l][k + l] + dp2[j][k]) % MOD;
              }
            }
          }
        }
      }
    }
  }
  LL ans = 0;
  if (K > 305) {
    K = 305;
  }
  for (int i = 0; i <= K; i++) {
    for (int j = 0; j < 310; j++)
      ans = (ans + dp[j][i]) % MOD;
  }
  cout << ans << endl;
  return 0;
}