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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define FOR(v, c) for (auto &v : c)
#define EACH(it, c) for (auto it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()
#define DRANGE(c, p) (c).begin(), (c).begin() + p, (c).end()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

int bitcount(const int b) {
  int res = 0;
  REP(i, 0, 10) { res += b & 1 << i ? 1 : 0; }
  return res;
}

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

  for (string line; getline(cin, line);) {
    int n, s;
    {
      ISS iss(line);
      iss >> n >> s;
    }

    VVI dp(1 << 10, VI(s + 1, 0));
    dp[0][0] = 1;

    int res = 0;
    REP(bits, 0, 1 << 10) {
      const int num_bits = bitcount(bits);
      if (n < num_bits) {
        continue;
      }

      REP(i, 0, s + 1) {
        if (num_bits == n && i == s) {
          res += dp[bits][i];
        }

        REP(j, 0, 10) {
          if (bits & 1 << j || s < i + j * (num_bits + 1)) {
            continue;
          }
          dp[bits | 1 << j][i + j * (num_bits + 1)] += dp[bits][i];
        }
      }
    }

    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define FOR(v, c) for (auto &v : c)
#define EACH(it, c) for (auto it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()
#define DRANGE(c, p) (c).begin(), (c).begin() + p, (c).end()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

int bitcount(const int b) {
  int res = 0;
  REP(i, 0, 10) { res += b & 1 << i ? 1 : 0; }
  return res;
}

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

  for (string line; getline(cin, line);) {
    int n, s;
    {
      ISS iss(line);
      iss >> n >> s;
    }

    VVI dp(1 << 10, VI(s + 1, 0));
    dp[0][0] = 1;

    int res = 0;
    REP(bits, 0, 1 << 10) {
      REP(i, 0, s + 1) {
        const int num_bits = bitcount(bits);
        if (num_bits == n && i == s) {
          res += dp[bits][i];
        }
        if (n <= num_bits) {
          continue;
        }

        REP(j, 0, 10) {
          if (bits & 1 << j || s < i + j * (num_bits + 1)) {
            continue;
          }
          dp[bits | 1 << j][i + j * (num_bits + 1)] += dp[bits][i];
        }
      }
    }

    cout << res << endl;
  }

  return 0;
}