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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define MOD 998244353
#define forn(a, n) for (int a = 0; a < (int)(n); ++a)
#define dforn(a, n) for (int a = (n)-1; a >= 0; --a)
#define forall(a, all)                                                         \\
  for (__typeof(all.begin()) a = all.begin(); a != all.end(); ++a)
#define EPS 0.000000000001
using namespace std;
typedef long long tint;
typedef pair<tint, tint> pii;
typedef vector<tint> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int solvedp(int voy, int k, int tengo, string &s, vvvi &dp) {
  if (voy == -1) {
    return tengo == 0;
  }

  if (dp[voy][k][tengo] != -1) {
    return dp[voy][k][tengo];
  }

  int next = voy - 1;
  while (next >= 0 && s[next] == '1')
    next--;

  tint ans = 0;
  if (s[voy] == '0') {
    forn(i, tengo + 1) {
      if (i) {
        ans += solvedp(next, k, tengo - i, s, dp);
        ans %= MOD;
      } else {
        ans += solvedp(voy - 1, k, tengo, s, dp);
        ans %= MOD;
      }
    }
  } else {
    if (k) {
      ans += solvedp(voy - 1, k - 1, tengo + 1, s, dp);
      ans %= MOD;
    }
    ans += solvedp(next, k, tengo, s, dp);
    ans %= MOD;
  }
  // cout << voy << " " << k << " " << tengo << " " << next << " " << ans <<
  // endl;
  dp[voy][k][tengo] = ans;
  return ans;
}

int main() {
  string s;
  tint k;
  cin >> s >> k;
  tint n = s.size();
  k = min(k, n);

  vvvi dp(n, vvi(k + 1, vi(k + 1, -1)));

  cout << solvedp(n - 1, k, 0, s, dp) << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define MOD 998244353
#define forn(a, n) for (int a = 0; a < (int)(n); ++a)
#define dforn(a, n) for (int a = (n)-1; a >= 0; --a)
#define forall(a, all)                                                         \\
  for (__typeof(all.begin()) a = all.begin(); a != all.end(); ++a)
#define EPS 0.000000000001
using namespace std;
typedef long long tint;
typedef pair<tint, tint> pii;
typedef vector<tint> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int solvedp(int voy, int k, int tengo, string &s, vvvi &dp) {
  if (voy == -1) {
    return tengo == 0;
  }

  if (dp[voy][k][tengo] != -1) {
    return dp[voy][k][tengo];
  }

  int next = voy - 1;
  while (next >= 0 && s[next] == '1')
    next--;

  tint ans = 0;
  if (s[voy] == '0') {
    forn(i, tengo + 1) {
      if (i) {
        ans += solvedp(next, k, tengo - i, s, dp);
        ans %= MOD;
      } else {
        ans += solvedp(voy - 1, k, tengo, s, dp);
        ans %= MOD;
      }
    }
  } else {
    if (k) {
      ans += solvedp(voy - 1, k - 1, tengo + 1, s, dp);
      ans %= MOD;
    }
    ans += solvedp(next, k, tengo, s, dp);
    ans %= MOD;
  }
  // cout << voy << " " << k << " " << tengo << " " << next << " " << ans <<
  // endl;
  dp[voy][k][tengo] = ans;
  return ans;
}

int main() {
  string s;
  tint k;
  cin >> s >> k;
  tint n = s.size();

  vvvi dp(n, vvi(min(n + 1, k + 1), vi(min(n + 1, k + 1), -1)));

  cout << solvedp(n - 1, k, 0, s, dp) << endl;
}