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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (a); i > (b); i--)
#define REP(i, n) FOR(i, 0, n)

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// debug
//-------------------------------------------
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
int N = 0;
int memo[10][1 << 10][331];
int dfs(int used, int sum, int n) {
  if (sum < 0)
    return 0;
  if (n == N)
    return sum == 0;

  if (~memo[n][used][sum])
    return memo[n][used][sum];

  int res = 0;

  for (int i = 0; i < 10; i++) {
    if ((used >> i) & 1) {
      res += dfs(used & ~(1 << i), sum - ((1 + n) * i), n + 1);
    }
  }
  memo[n][used][sum] = res;
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, s;
  while (1) {
    cin >> n >> s;
    if (cin.eof())
      break;
    N = n;
    memset(memo, -1, sizeof(memo));
    cout << dfs((1 << 10) - 1, s, 0) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (a); i > (b); i--)
#define REP(i, n) FOR(i, 0, n)

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

void print_arr(int arr[10]) {
  cout << "----------------------------" << endl;
  REP(i, 10) cout << arr[i] << ", ";
  cout << endl;
}

// debug
//-------------------------------------------
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
int N = 0;
int S = 0;
int result = 0;
int memo[10][1 << 10][332];
int dfs(int used, int sum, int n) {
  if (sum < 0)
    return 0;
  if (n == N + 1)
    return sum == 0;

  if (memo[n][used][sum] >= 0) {
    return memo[n][used][sum];
  }
  int res = 0;

  for (int i = 0; i < 10; i++) {
    if ((used >> i) & 1) {
      res += dfs(used & ~(1 << i), sum - n * i, n + 1);
    }
  }
  memo[n][used][sum] = res;
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  memset(memo, -1, sizeof(memo));
  int n, s;
  while (1) {
    cin >> n >> s;
    if (cin.eof())
      break;
    N = n;
    if (s >= 331) {
      cout << 0 << endl;
      continue;
    }
    cout << dfs((1 << 10) - 1, s, 1) << endl;
  }
  return 0;
}