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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

#ifdef __BORLANDC__
typedef __int64 ll;
#else
typedef long long ll;
#endif

const int N = 10;
int n;

typedef pair<int, int> pii;

map<pii, int> memo;

int saiki(int depth, int used, int lest) {
  pii key = pii(used, lest);
  map<pii, int>::iterator it = memo.find(key);
  if (it != memo.end())
    return it->second;

  if (lest < 0)
    return 0;
  if (depth == n) {
    if (lest == 0)
      return 1;
    return 0;
  }

  int maxim = 0;
  int cpd = n - 1;
  for (int i = N - 1; i >= 0; i--) {
    if (cpd == depth - 1)
      break;
    if ((used & (1 << i)) == 0) {
      maxim += i * (cpd + 1);
      cpd--;
    }
  }
  if (maxim < lest)
    return 0;

  int ret = 0;
  rep(i, N) {
    if ((used & (1 << i)) == 0) {
      ret += saiki(depth + 1, used | (1 << i), lest - (depth + 1) * i);
    }
  }
  return memo[key] = ret;
}

class Q {
public:
  int i, n, s;
  Q(int i, int n, int s) : i(i), n(n), s(s) {}
  bool operator<(const Q &a) const {
    if (n != a.n)
      return n < a.n;
    if (i != a.i)
      return i < a.i;
    return s < a.s;
  }
};
int main() {
  vector<Q> in;
  int s, cnt = 0;
  while (cin >> n >> s) {
    if (n == 0)
      break;
    in.push_back(Q(cnt, n, s));
    cnt++;
  }
  sort(in.begin(), in.end());

  vector<int> ans(in.size());
  rep(i, in.size()) {
    if (i != 0 && in[i].n != in[i - 1].n) {
      memo.clear();
    }

    n = in[i].n;
    ans[in[i].i] = saiki(0, 0, in[i].s);
  }

  rep(i, ans.size()) { printf("%d\\n", ans[i]); }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

#ifdef __BORLANDC__
typedef __int64 ll;
#else
typedef long long ll;
#endif

const int N = 10;
int n;

typedef pair<int, int> pii;

map<pii, int> memo;

int saiki(int depth, int used, int lest) {
  pii key = pii(used, lest);
  map<pii, int>::iterator it = memo.find(key);
  if (it != memo.end())
    return it->second;

  if (lest < 0)
    return it->second = 0;
  if (depth == n) {
    if (lest == 0)
      return it->second = 1;
    return it->second = 0;
  }

  int ret = 0;
  rep(i, N) {
    if ((used & (1 << i)) == 0) {
      ret += saiki(depth + 1, used | (1 << i), lest - (depth + 1) * i);
    }
  }
  return it->second = ret;
}

class Q {
public:
  int i, n, s;
  Q(int i, int n, int s) : i(i), n(n), s(s) {}
  bool operator<(const Q &a) const {
    if (n != a.n)
      return n < a.n;
    if (i != a.i)
      return i < a.i;
    return s < a.s;
  }
};
int main() {
  vector<Q> in;
  int s, cnt = 0;
  while (cin >> n >> s) {
    if (n == 0)
      break;
    in.push_back(Q(cnt, n, s));
    cnt++;
  }
  sort(in.begin(), in.end());

  vector<int> ans(in.size());
  rep(i, in.size()) {
    if (i != 0 && in[i].n != in[i - 1].n) {
      memo.clear();
    }

    n = in[i].n;
    ans[in[i].i] = saiki(0, 0, in[i].s);
  }

  rep(i, ans.size()) { printf("%d\\n", ans[i]); }
}