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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define it vector<int>::iterator
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
using namespace std;

bool next_combination(it first1, it last1, it first2, it last2) {
  if ((first1 == last1) || (first2 == last2)) {
    return false;
  }
  it m1 = last1;
  it 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;
}
bool next_combination(it first, it middle, it last) {
  return next_combination(first, middle, middle, last);
}
int main() {
  int n, s;
  vvi in(20, vi(500, -1));
  while (cin >> n >> s) {
    if (in[n][s] >= 0) {
      cout << in[n][s] << endl;
      continue;
    }
    int sum = 0;
    rep(i, n) sum += (i + 1) * (n - i - 1);
    if (sum > s) {
      cout << 0 << endl;
      in[n][s] = 0;
      continue;
    }
    vi d(10);
    int co = 0;
    rep(i, 10) d[i] = i;
    do {
      do {
        int sum = 0;
        rep(i, n) sum += (i + 1) * d[i];
        if (sum == s) {
          co++;
        }
      } while (next_permutation(d.begin(), d.begin() + n));
    } while (next_combination(d.begin(), d.begin() + n, d.end()));
    cout << co << endl;
    in[n][s] = co;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define it vector<int>::iterator
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
using namespace std;

bool next_combination(it first1, it last1, it first2, it last2) {
  if ((first1 == last1) || (first2 == last2)) {
    return false;
  }
  it m1 = last1;
  it 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;
}
bool next_combination(it first, it middle, it last) {
  return next_combination(first, middle, middle, last);
}
int main() {
  int n, s;
  vvi in(20, vi(500, -1));
  while (cin >> n >> s) {
    if (in[n][s] >= 0) {
      cout << in[n][s] << endl;
      continue;
    }
    vi d(10);
    int co = 0;
    rep(i, 10) d[i] = i;
    do {
      do {
        int sum = 0;
        rep(i, n) sum += (i + 1) * d[i];
        if (sum == s) {
          co++;
        }
      } while (next_permutation(d.begin(), d.begin() + n));
    } while (next_combination(d.begin(), d.begin() + n, d.end()));
    cout << co << endl;
    in[n][s] = co;
  }
}