Problem p00070 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI = 6 * asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
static const int INF = 1 << 24;

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

// v.erase(unique(v.begin(),v.end()),v.end());
vint v(10, 0);
ll dp[10 + 5][331 + 10][(1 << 10) + 10] = {0};
ll foo(int n, int s, int a) {
  // cout<<n<<" "<<s<<" "<< static_cast<std::bitset<10> >(a)<<endl;
  if (s < 0)
    return 0;
  if (dp[n][s][a])
    return dp[n][s][a];
  if (n == 1) {
    if (s < 10 && a >> s & 1) {
      return dp[n][s][a] = 1;
    } else {
      return dp[n][s][a] = 0;
    }
  }
  if (n <= 0)
    return dp[n][s][a] = 0;
  ll ret = 0;
  rep(i, 10) {
    // cout<<(a>>i)<<endl;
    if (a >> i & 1) {
      ret += foo(n - 1, s - n * i, a - (1 << i));
    }
  }
  return dp[n][s][a] = ret;
}

int main() {
  int n, s;
  while (cin >> n) {
    cin >> s;
    ll ans = foo(n, s, (1 << 10) - 1);
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI = 6 * asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
static const int INF = 1 << 24;

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

// v.erase(unique(v.begin(),v.end()),v.end());
vint v(10, 0);
ll dp[10 + 5][331 + 10][(1 << 10) + 10] = {0};
ll foo(int n, int s, int a) {
  // cout<<n<<" "<<s<<" "<< static_cast<std::bitset<10> >(a)<<endl;
  if (s > 330)
    return 0;
  if (s < 0)
    return 0;
  if (dp[n][s][a])
    return dp[n][s][a];
  if (n == 1) {
    if (s < 10 && a >> s & 1) {
      return dp[n][s][a] = 1;
    } else {
      return dp[n][s][a] = 0;
    }
  }
  if (n <= 0)
    return dp[n][s][a] = 0;
  ll ret = 0;
  rep(i, 10) {
    // cout<<(a>>i)<<endl;
    if (a >> i & 1) {
      ret += foo(n - 1, s - n * i, a - (1 << i));
    }
  }
  return dp[n][s][a] = ret;
}

int main() {
  int n, s;
  while (cin >> n) {
    cin >> s;
    ll ans = foo(n, s, (1 << 10) - 1);
    cout << ans << endl;
  }
}