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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for (i = 0; i < n; ++i)
#define rep1(i, n) for (i = 1; i < n; ++i)
#define rep2d(i, j, n)                                                         \\
  for (i = 0; i < n; ++i)                                                      \\
    for (j = i + 1; j < n; ++j)
#define per(i, n) for (i = n - 1; i > -1; --i)
#define int(x)                                                                 \\
  int x;                                                                       \\
  scanf("%d", &x)
#define int2(x, y)                                                             \\
  int x, y;                                                                    \\
  scanf("%d%d", &x, &y)
#define int3(x, y, z)                                                          \\
  int x, y, z;                                                                 \\
  scanf("%d%d%d", &x, &y, &z)
#define sc(x) cin >> x
#define sc2(x, y) cin >> x >> y
#define sc3(x, y, z) cin >> x >> y >> z
#define scn(n, a) rep(i, n) cin >> a[i]
#define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i]
#define pri(x) cout << x << "\\n"
#define pri2(x, y) cout << x << " " << y << "\\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\\n"
#define debugn(i, n, a) rep(i, n) cout << " --- " << a[i] << "\\n";
#define debugin(i, n, a) rep(i, n) printf(" --- %10d\\n", a[i])
#define debugi2n(i, n, a, b) rep(i, n) printf(" --- %10d %10d\\n", a[i], b[i])
#define debugiin(i, n, a)                                                      \\
  rep(i, n) printf(" --- %10d %10d\\n", a[i].first, a[i].second)
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\\n", x)

int use[10];
int s, tmp;

int dfs(int n, int now) {
  if (n < 0)
    return 0;
  if (n == 0 && s == now)
    return 1;
  if (n == 0 && s != now)
    return 0;
  int i, j, smin = 0, smax = 0;

  j = tmp;
  rep(i, 10) {
    if (!use[i])
      smin += i * j--;
    if (smin + now > s)
      return 0;
    if (j == tmp - n)
      break;
  }
  if (smin + now == s)
    return 1;
  j = tmp;
  per(i, 10) {
    if (!use[i])
      smax += i * j--;
    if (j == tmp - n)
      break;
  }
  if (smax + now < s)
    return 0;
  if (smax + now == s)
    return 1;

  int res = 0;
  rep(i, 10) if (!use[i]) {
    if ((tmp - n + 1) * i + now > s)
      return res;
    use[i] = tmp - n + 1;
    res += dfs(n - 1, now + i * (tmp - n + 1));
    use[i] = 0;
  }
  return res;
}

signed main(void) {
  int i, j;
  int n;
  for (int testcase = 0; scanf("%d%d", &n, &s) != EOF; testcase++) {
    rep(i, 10) use[i] = 0;
    tmp = n;
    if (n > 10)
      pri(0);
    else
      pri(dfs(n, 0));

    //*/

    //*/ break;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for (i = 0; i < n; ++i)
#define rep1(i, n) for (i = 1; i < n; ++i)
#define rep2d(i, j, n)                                                         \\
  for (i = 0; i < n; ++i)                                                      \\
    for (j = i + 1; j < n; ++j)
#define per(i, n) for (i = n - 1; i > -1; --i)
#define int(x)                                                                 \\
  int x;                                                                       \\
  scanf("%d", &x)
#define int2(x, y)                                                             \\
  int x, y;                                                                    \\
  scanf("%d%d", &x, &y)
#define int3(x, y, z)                                                          \\
  int x, y, z;                                                                 \\
  scanf("%d%d%d", &x, &y, &z)
#define sc(x) cin >> x
#define sc2(x, y) cin >> x >> y
#define sc3(x, y, z) cin >> x >> y >> z
#define scn(n, a) rep(i, n) cin >> a[i]
#define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i]
#define pri(x) cout << x << "\\n"
#define pri2(x, y) cout << x << " " << y << "\\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\\n"
#define debugn(i, n, a) rep(i, n) cout << " --- " << a[i] << "\\n";
#define debugin(i, n, a) rep(i, n) printf(" --- %10d\\n", a[i])
#define debugi2n(i, n, a, b) rep(i, n) printf(" --- %10d %10d\\n", a[i], b[i])
#define debugiin(i, n, a)                                                      \\
  rep(i, n) printf(" --- %10d %10d\\n", a[i].first, a[i].second)
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\\n", x)

int use[10];
int s, tmp;

int dfs(int n, int now) {
  int i;

  if (n < 0)
    return 0;
  if (tmp * n * 9 + now < s)
    return 0;
  if (n == 0 && s == now) {
    //    rep(i, 10)printf("%2d", use[i]);
    //    kara;
    return 1;
  }
  if (n == 0 && s != now)
    return 0;
  if (s < now)
    return 0;

  int res = 0;
  rep(i, 10) if (!use[i]) {
    if ((tmp - n + 1) * i + now > s)
      return res;
    use[i] = tmp - n + 1;
    res += dfs(n - 1, now + i * (tmp - n + 1));
    use[i] = 0;
  }
  return res;
}

signed main(void) {
  int i, j;
  int n;
  //  pri(2 + 6 + 12 + 20 + 30 + 42 + 56 + 72 + 90);
  //  pri(9 + 9 + 16 + 16 + 21 + 21 + 24 + 24 + 25);
  for (int testcase = 0; scanf("%d%d", &n, &s) != EOF; testcase++) {
    rep(i, 10) use[i] = 0;
    tmp = n;
    if (n <= 10 && s <= 330 - 45 * (10 - n))
      pri(dfs(n, 0));
    else
      pri(0);

    //*/

    //*/ break;
  }
  return 0;
}