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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;

const long double pi = 3.141592653589793;

#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, j, n) for (int i = j; i < n; i++)
#define pb push_back
#define mp make_pair
#define mod 998244353
#define F first
#define S second

string s;
int k, n;

/*
  --- 0 111111  ----------
      i'     i     (j)

      i(3) j(3) k(5)

      O(N ** 5) solution: accepted (confused now)
*/

unordered_map<ll, int> dp;
ll state_to_num(int i, int j, int k) {
  return 1LL * i * 1E10 + 1LL * j * 1E6 + k;
}

int combinations(int i, int j, int k) {
  if (i < 0)
    return 1;
  if (dp.count(state_to_num(i, j, k)) == 1) {
    return dp[state_to_num(i, j, k)];
  }

  int id = i;
  while (id >= 0 && s[id] == '1')
    id--;
  if (id == -1)
    return 1;
  int ans = 0;
  for (int ps = 0; ps <= (i - id + j); ps++) {
    if (ps <= j) {
      ans += combinations(id - 1, ps, k);
    } else if (ps - j <= k) {
      ans += combinations(id - 1, ps, k - ps + j);
    }
    ans %= mod;
  }
  return dp[state_to_num(i, j, k)] = ans;
}

int main() {

  // freopen("input.in","r",stdin);
  // freopen("output.out","w",stdout);

  cin >> s >> k;
  n = s.size();
  k = min(k, n * n);

  cout << combinations(n - 1, 0, k) << endl;
  if (dp.size() > 1000000) {
    assert(false);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;

const long double pi = 3.141592653589793;

#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, j, n) for (int i = j; i < n; i++)
#define pb push_back
#define mp make_pair
#define mod 998244353
#define F first
#define S second

string s;
int k, n;

/*
  --- 0 111111  ----------
      i'     i     (j)

      i(3) j(3) k(5)

      O(N ** 5) solution: accepted (confused now)
*/

unordered_map<ll, int> dp;
ll state_to_num(int i, int j, int k) {
  return 1LL * i * 1E10 + 1LL * j * 1E6 + k;
}

int combinations(int i, int j, int k) {
  if (i < 0)
    return 1;
  if (dp.count(state_to_num(i, j, k)) == 1) {
    return dp[state_to_num(i, j, k)];
  }

  int id = i;
  while (id >= 0 && s[id] == '1')
    id--;
  if (id == -1)
    return 1;
  int ans = 0;
  for (int ps = 0; ps <= (i - id + j); ps++) {
    if (ps <= j) {
      ans += combinations(id - 1, ps, k);
    } else if (ps - j <= k) {
      ans += combinations(id - 1, ps, k - ps + j);
    }
    ans %= mod;
  }
  return dp[state_to_num(i, j, k)] = ans;
}

int main() {

  cin >> s >> k;
  n = s.size();
  k = min(k, n * n);

  cout << combinations(n - 1, 0, k) << endl;
  if (dp.size() > 10000) {
    assert(false);
  }

  return 0;
}