Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val)                                                      \\
  for (int i = 0; i < n; ++i)                                                  \\
    cout << val << " ";                                                        \\
  cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 10000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
// typedef pair<ll, ll> P;
typedef pair<P, int> PI;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;

int memo[350][1 << 10];
int dfs(int s, int c, int bit) {
  if (c == 0) {
    if (s == 0)
      return 1;
    return 0;
  }
  if (memo[s][bit] != -1)
    return memo[s][bit];
  int ret = 0;
  rep(i, 10) {
    if (i * c > s)
      break;
    if (!(bit & (1 << i))) {
      ret += dfs(s - c * i, c - 1, bit | (1 << i));
    }
  }
  return memo[s][bit] = ret;
}

int main() {
  int n, s;
  while (cin >> n >> s) {
    if (s > 340) {
      cout << "0" << endl;
    } else {
      rep(i, s + 1) rep(j, 1 << 10) memo[i][j] = -1;
      cout << dfs(s, n, 0) << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val)                                                      \\
  for (int i = 0; i < n; ++i)                                                  \\
    cout << val << " ";                                                        \\
  cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 10000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
// typedef pair<ll, ll> P;
typedef pair<P, int> PI;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;

bool used[10];
int dfs(int c, int s) {
  if (c == 0) {
    if (s == 0)
      return 1;
    return 0;
  }
  int ret = 0;
  rep(i, 10) {
    if (i * c > s)
      break;
    if (!used[i]) {
      used[i] = true;
      ret += dfs(c - 1, s - i * c);
      used[i] = false;
    }
  }
  return ret;
}

int main() {
  int n, s;
  while (cin >> n >> s) {
    rep(i, 10) used[i] = false;
    cout << dfs(n, s) << endl;
  }
  return 0;
}