Problem p04015 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;

const ll MOD = 1000000007ll;

ll modSum(ll a, ll b) { return ((a % MOD) + (b % MOD)) % MOD; }

ll modProd(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; }

ll getPow(ll x, ll e) {
  if (e == 0)
    return 1;
  if (e % 2 == 0) {
    ll tmp = getPow(x, e / 2);
    return modProd(tmp, tmp);
  } else {
    ll tmp = getPow(x, e - 1);
    return modProd(tmp, x);
  }
}

bool isEven(int x) {
  int tmp = ((x % 2) + 2) % 2;
  return tmp == 0;
}

bool isMultipleOfFour(int x) { return isEven(x) && isEven(x / 2); }

const int MAXN = 51;
const int MAXSUM = 2501;

int N, A;
int ar[MAXN];
ll memo[MAXN][MAXSUM][MAXN];

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

void preprocess() {}

void solve() {
  memo[0][0][0] = 1;
  for (int i = 1; i <= N; i++) {
    memo[i][0][0] = 1;
    for (int s = 1; s < MAXSUM; s++) {
      for (int c = 1; c <= N; c++) {
        memo[i][s][c] = memo[i - 1][s][c];
        if (s >= ar[i]) {
          memo[i][s][c] += memo[i - 1][s - ar[i]][c - 1];
        }
      }
    }
  }
  ll ans = 0;
  for (int mul = 1; mul * A < MAXSUM; mul++) {
    ans += memo[N][mul * A][mul];
  }
  cout << ans;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cerr.tie(0);

  int T;
  //  cin >> T;
  T = 1;

  for (int i = 0; i < T; i++) {
    input();
    preprocess();
    solve();
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;

const ll MOD = 1000000007ll;

ll modSum(ll a, ll b) { return ((a % MOD) + (b % MOD)) % MOD; }

ll modProd(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; }

ll getPow(ll x, ll e) {
  if (e == 0)
    return 1;
  if (e % 2 == 0) {
    ll tmp = getPow(x, e / 2);
    return modProd(tmp, tmp);
  } else {
    ll tmp = getPow(x, e - 1);
    return modProd(tmp, x);
  }
}

bool isEven(int x) {
  int tmp = ((x % 2) + 2) % 2;
  return tmp == 0;
}

bool isMultipleOfFour(int x) { return isEven(x) && isEven(x / 2); }

const int MAXN = 51;
const int MAXSUM = 2501;

int N, A;
int ar[MAXN];
ll memo[MAXN][MAXSUM][MAXN];

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

void preprocess() {}

void solve() {
  memo[0][0][0] = 1;
  for (int i = 1; i <= N; i++) {
    memo[i][0][0] = 1;
    for (int s = 1; s < MAXSUM; s++) {
      for (int c = 1; c <= N; c++) {
        memo[i][s][c] = memo[i - 1][s][c];
        if (s >= ar[i]) {
          memo[i][s][c] += memo[i - 1][s - ar[i]][c - 1];
        }
      }
    }
  }
  ll ans = 0;
  for (int mul = 1; mul * A < MAXSUM && mul <= N; mul++) {
    ans += memo[N][mul * A][mul];
  }
  cout << ans;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cerr.tie(0);

  int T;
  //  cin >> T;
  T = 1;

  for (int i = 0; i < T; i++) {
    input();
    preprocess();
    solve();
  }

  return 0;
}