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

#include <bits/stdc++.h>

using namespace std;

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);
}

int main() {
  long long int N, S;
  while (cin >> N >> S) {
    vector<long long int> D{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    long long int ans = 0;
    do {
      vector<long long int> D2;
      copy(D.begin(), D.begin() + N, back_inserter(D2));
      do {
        long long int sum = 0;
        for (size_t i = 0; i < N; i++) {
          sum += (i + 1) * D2[i];
        }
        if (sum == S) {
          ans++;
        }
      } while (next_permutation(D2.begin(), D2.end()));
    } while (next_combination(D.begin(), D.begin() + N, D.end()));
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

int t[10] = {};

int saiki(int n, int s, bool y[10]) {
  if (n == 1)
    return s < 10 ? !y[s] : 0;
  if (s > t[n])
    return 0;
  int count2 = 0;
  for (size_t i = 0, x = s / n; i <= x; i++) {
    if (!y[i]) {
      y[i] = 1;
      count2 += saiki(n - 1, s - i * n, y);
      y[i] = 0;
    }
  }
  return count2;
}

int main() {
  int n, s;
  bool y[10];
  t[0] = 0;
  for (size_t i = 1; i < 10; i++) {
    t[i] = t[i - 1] + (10 - i) * (10 - i);
  }
  while (cin >> n >> s) {
    for (size_t i = 0; i < 10; i++) {
      y[i] = 0;
    }
    cout << saiki(n, s, y) << endl;
  }
  return 0;
}