Problem p04015 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

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

#define CIN(T, x)                                                              \
  T x;                                                                         \
  cin >> x;
#define CINS(T, x, N)                                                          \
  vector<T> x(N);                                                              \
  for (int i = 0; i < (N); i++) {                                              \
    cin >> x[i];                                                               \
  }
#define SHOW(x)                                                                \
  for (int i = 0; i < x.size(); i++) {                                         \
    cout << x[i] << " ";                                                       \
  }                                                                            \
  cout << "\n";
#define SHOW2(x)                                                               \
  for (int j = 0; j < x.size(); j++) {                                         \
    SHOW(x[j]);                                                                \
  }                                                                            \
  cout << "\n";

int N, A;
vector<int> x(51);
vector<vector<vector<ll>>>
    dp(51, vector<vector<ll>>(51, vector<ll>(50 * 50 + 1, -1)));

ll calc(int count, int index, int sum) {
  if (sum < 0)
    return 0;
  if (dp[count][index][sum] > -1)
    return dp[count][index][sum];
  if (sum == 0) {
    if (count == 0)
      return 1;
    else
      return 0;
  }

  if (index == 1) {
    if (count == 1 && sum == x[1])
      return 1;
    else
      return 0;
  }
  // cout << count << " " << index << " " << sum << "\n";
  dp[count][index][sum] =
      calc(count, index - 1, sum) + calc(count - 1, index - 1, sum - x[index]);
  // cout << count << " " << index << " " << sum << ":" << dp[count][index][sum]
  // << "\n";

  return dp[count][index][sum];
}

int main() {

  cin >> N >> A;
  for (int i = 1; i <= N; i++) {
    cin >> x[i];
  }

  ll count = 0;

  for (int i = N; i > 0; i--) {
    count += calc(i, N, i * A);
  }

  cout << count << "\n";

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

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

#define CIN(T, x)                                                              \
  T x;                                                                         \
  cin >> x;
#define CINS(T, x, N)                                                          \
  vector<T> x(N);                                                              \
  for (int i = 0; i < (N); i++) {                                              \
    cin >> x[i];                                                               \
  }
#define SHOW(x)                                                                \
  for (int i = 0; i < x.size(); i++) {                                         \
    cout << x[i] << " ";                                                       \
  }                                                                            \
  cout << "\n";
#define SHOW2(x)                                                               \
  for (int j = 0; j < x.size(); j++) {                                         \
    SHOW(x[j]);                                                                \
  }                                                                            \
  cout << "\n";

int N, A;
vector<int> x(51);
vector<vector<vector<ll>>>
    dp(51, vector<vector<ll>>(51, vector<ll>(50 * 50 + 1, -1)));

ll calc(int count, int index, int sum) {
  if (sum < 0)
    return 0;
  if (count < 0)
    return 0;
  if (dp[count][index][sum] > -1)
    return dp[count][index][sum];
  if (sum == 0) {
    if (count == 0)
      return 1;
    else
      return 0;
  }

  if (index == 1) {
    if (count == 1 && sum == x[1])
      return 1;
    else
      return 0;
  }
  // cout << count << " " << index << " " << sum << "\n";
  dp[count][index][sum] =
      calc(count, index - 1, sum) + calc(count - 1, index - 1, sum - x[index]);
  // cout << count << " " << index << " " << sum << ":" << dp[count][index][sum]
  // << "\n";

  return dp[count][index][sum];
}

int main() {

  cin >> N >> A;
  for (int i = 1; i <= N; i++) {
    cin >> x[i];
  }

  ll count = 0;

  for (int i = N; i > 0; i--) {
    count += calc(i, N, i * A);
  }

  cout << count << "\n";

  return 0;
}