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

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
#define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--)
#define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define vrep(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define mp make_pair
#define pb push_back
template <class T, class S> inline bool minup(T &m, S x) {
  return m > (T)x ? (m = (T)x, true) : false;
}
template <class T, class S> inline bool maxup(T &m, S x) {
  return m < (T)x ? (m = (T)x, true) : false;
}

const int INF = 1000000000;
const ll MOD = 1000000007LL;
const double EPS = 1E-12;

inline int nextCombination(int comb) {
  int x = comb & -comb, y = comb + x;
  return (((comb & ~y) / x) >> 1) | y;
}

const int MAX_N = 10;
const int MAX_S = 100000;
const int N = MAX_N;
int n, s;
int dp[MAX_N + 1][MAX_S + 1];

template <typename T> ostream &operator<<(ostream &r, const vector<T> t) {
  vrep(v, t) r << *v << (v + 1 == t.end() ? "" : " ");
  return r;
}
template <typename T>
ostream &operator<<(ostream &r, const pair<vector<T>, int> p) {
  int n = p.second;
  minup(n, p.first.size());
  rep(i, n - 1) r << p.first[i] << ' ';
  r << p.first[n - 1];
  return r;
}
template <typename T> ostream &operator<<(ostream &r, const pair<T *, int> p) {
  int n = p.second - 1;
  rep(i, n) r << p.first[i] << ' ';
  return r << p.first[n];
}
template <typename T, size_t S>
ostream &operator<<(ostream &r, const T (&a)[S]) {
  rep(i, S - 1) r << a[i] << ' ';
  return r << a[S - 1];
}

int main() {
  erep(m, 1, N) {
    for (int comb = (1 << (m)) - 1; comb < (1 << N);
         comb = nextCombination(comb)) {
      int d = 1;
      int sum = 0;
      vi c;
      rep(i, N) if (comb & (1 << i)) c.pb(i);
      do {
        int sum = 0;
        rep(i, m) sum += (i + 1) * c[i];
        dp[m][sum] += 1;
      } while (next_permutation(ALL(c)));
    }
  }

  while (cin >> n >> s)
    cout << dp[n][s] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
#define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--)
#define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define vrep(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define mp make_pair
#define pb push_back
template <class T, class S> inline bool minup(T &m, S x) {
  return m > (T)x ? (m = (T)x, true) : false;
}
template <class T, class S> inline bool maxup(T &m, S x) {
  return m < (T)x ? (m = (T)x, true) : false;
}

const int INF = 1000000000;
const ll MOD = 1000000007LL;
const double EPS = 1E-12;

template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first1, BidirectionalIterator last1,
                      BidirectionalIterator first2,
                      BidirectionalIterator last2) {
  if ((first1 == last1) || (first2 == last2)) {
    return false;
  }
  BidirectionalIterator m1 = last1;
  BidirectionalIterator m2 = last2;
  --m2;
  while (--m1 != first1 && !(*m1 < *m2)) {
  }
  bool result = (m1 == first1) && !(*first1 < *m2);
  if (!result) {
    // ?
    while (first2 != m2 && !(*m1 < *first2)) {
      ++first2;
    }
    first1 = m1;
    std ::iter_swap(first1, first2);
    ++first1;
    ++first2;
  }
  if ((first1 != last1) && (first2 != last2)) {
    // ?
    m1 = last1;
    m2 = first2;
    while ((m1 != first1) && (m2 != last2)) {
      std ::iter_swap(--m1, m2);
      ++m2;
    }
    // ?
    std ::reverse(first1, m1);
    std ::reverse(first1, last1);
    std ::reverse(m2, last2);
    std ::reverse(first2, last2);
  }
  return !result;
}

template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first, BidirectionalIterator middle,
                      BidirectionalIterator last) {
  return next_combination(first, middle, middle, last);
}

template <typename T> ostream &operator<<(ostream &r, const vector<T> t) {
  vrep(v, t) r << *v << (v + 1 == t.end() ? "" : " ");
  return r;
}
template <typename T>
ostream &operator<<(ostream &r, const pair<vector<T>, int> p) {
  int n = p.second;
  minup(n, p.first.size());
  rep(i, n - 1) r << p.first[i] << ' ';
  r << p.first[n - 1];
  return r;
}
template <typename T> ostream &operator<<(ostream &r, const pair<T *, int> p) {
  int n = p.second - 1;
  rep(i, n) r << p.first[i] << ' ';
  return r << p.first[n];
}
template <typename T, size_t S>
ostream &operator<<(ostream &r, const T (&a)[S]) {
  rep(i, S - 1) r << a[i] << ' ';
  return r << a[S - 1];
}

int n, s;

int main() {
  while (cin >> n >> s) {
    vi vk;
    int res = 0;
    erep(i, 0, 9) vk.pb(i);
    do {
      do {
        vi comb;
        rep(i, n) comb.pb(vk[i]);
        int check = 0;
        erep(i, 1, n) check += i * vk[i - 1];
        if (check == s)
          res += 1;
      } while (next_permutation(vk.begin(), vk.begin() + n));

      /*
      int check = 0;
      erep(i, 1, n) check += i * vk[i - 1];
      if(check == s) res += 1;
      //      if(check == s) cerr << vk << endl << check << endl;
      cerr << mp(vk, n) << endl;
      */
    } while (next_combination(vk.begin(), vk.begin() + n, vk.end()));
    cout << res << endl;
  }

  return 0;
}