alexjercan/bugnet huggingface dataset:
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int dp[10 + 1][330 + 1];
  for (int i = 0; i < 10 + 1; ++i) {
    for (int j = 0; j < 330 + 1; ++j)
      dp[i][j] = 0;
  }

  int fac[10 + 1];
  fac[0] = fac[1] = 1;
  for (int i = 2; i < 10 + 1; ++i)
    fac[i] = i * fac[i - 1];

  vector<int> num;
  for (int i = 0; i < 10; ++i)
    num.push_back(i);
  do {
    int sum = 0;
    for (int i = 0; i < 10; ++i) {
      sum += ((i + 1) * num[i]);
      ++dp[i + 1][sum];
    }
  } while (next_permutation(num.begin(), num.end()));

  int n, s;
  while (cin >> n >> s)
    if (n < 0 || 10 < n || s < 0 || 330 < s)
      cout << "0" << endl;
    else
      cout << dp[n][s] / fac[10 - n] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int dp[10 + 1][330 + 1];
  for (int i = 0; i < 10 + 1; ++i) {
    for (int j = 0; j < 330 + 1; ++j)
      dp[i][j] = 0;
  }

  int fac[10 + 1];
  fac[0] = fac[1] = 1;
  for (int i = 2; i < 10 + 1; ++i)
    fac[i] = i * fac[i - 1];

  vector<int> num;
  for (int i = 0; i < 10; ++i)
    num.push_back(i);
  do {
    int sum = 0;
    for (int i = 0; i < 10; ++i) {
      sum += ((i + 1) * num[i]);
      ++dp[i + 1][sum];
    }
  } while (next_permutation(num.begin(), num.end()));

  int n, s;
  while (cin >> n >> s)
    cout << dp[n][s] / fac[10 - n] << endl;
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string.h>
using namespace std;

int m[21][11024];
int n, s;

void dfs(int b, int d, int s) {
  m[d][s]++;
  for (int i = 0; i < 10; i++)
    if ((b >> i) & 1)
      dfs(b - (1 << i), d + 1, s + (1 + d) * i);
}
int main() {
  memset(m, 0, sizeof(m));
  dfs(1023, 0, 0);
  while (cin >> n >> s)
    cout << m[n][s] << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string.h>
using namespace std;

int m[11][1024];
int n, s;

void dfs(int b, int d, int s) {
  m[d][s]++;
  for (int i = 0; i < 10; i++)
    if ((b >> i) & 1)
      dfs(b - (1 << i), d + 1, s + (1 + d) * i);
}
int main() {
  memset(m, 0, sizeof(m));
  dfs(1023, 0, 0);
  while (cin >> n >> s)
    cout << m[n][s] << endl;
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <unordered_set>
#include <vector>
using namespace std;

typedef unsigned long long ll;
int n, s;
map<pair<int, int>, int> ma; // sum,number, count
void dfs(int cur, int used, ll status) {
  if (cur > 10000)
    return;
  if (used > 10) {
    return;
  }
  ma[make_pair(cur, used)]++;
  used++;
  for (int j = 0; j <= 9; j++) {
    if (((status >> j) & 1ll) == 0ll)
      dfs(cur + used * j, used, (status | (1ll << j)));
  }
}
int main() {
  dfs(0, 0, 0);
  while (cin >> n >> s) {
    int ans = 0;
    ans = ma[make_pair(s, n)];
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <unordered_set>
#include <vector>
using namespace std;

typedef unsigned long long ll;
int n, s;
map<pair<pair<int, int>, int>, int> ma;
int dfs(int need, int used, ll status) {
  if (need < 0)
    return 0;
  if (used == n) {
    if (need == 0)
      return 1;
    else
      return 0;
  }
  if (ma[make_pair(make_pair(need, used), status)]) {
    return ma[make_pair(make_pair(need, used), status)];
  }
  int ret = 0;
  used++;
  for (int j = 0; j <= 9; j++) {
    if (((status >> j) & 1ll) == 0ll)
      ret += dfs(need - used * j, used, (status | (1ll << j)));
  }
  ma[make_pair(make_pair(need, used - 1), status)] = ret;
  return ret;
}
int main() {
  while (cin >> n >> s) {
    int ans = 0;
    ll st = 0;
    ans = dfs(s, 0, st);
    cout << ans << endl;
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int N, S;
int dp[(1 << 10)][331];
int solve(int st, int s, int no) {
  if (s > S)
    return 0;
  if (no == N + 1) {
    if (s == S)
      return 1;
    return 0;
  }
  if (dp[st][s] != -1)
    return dp[st][s];
  int ret = 0;
  for (int i = 0; i < 10; i++) {
    if (!(st & (1 << i))) {
      ret += solve(st | (1 << i), s + no * i, no + 1);
    }
  }
  return dp[st][s] = ret;
}
int main() {
  while (~scanf("%d %d", &N, &S)) {
    memset(dp, -1, sizeof(dp));
    printf("%d\\n", solve(0, 0, 1));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int h[11];
int N, S;
int solve(int no, int s) {
  if (s > S)
    return 0;
  if (no == N + 1) {
    if (s == S)
      return 1;
    return 0;
  }
  int ret = 0;
  for (int i = 0; i < 10; i++) {
    if (!h[i]) {
      h[i] = 1;
      ret += solve(no + 1, s + no * i);
      h[i] = 0;
    }
  }
  return ret;
}
int main() {
  while (~scanf("%d %d", &N, &S)) {
    printf("%d\\n", solve(1, 0));
  }
}
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; i++)
#define FOR(i, j, k) for (int i = j; i < k; i++)
const int INF = INT_MAX / 2;
const int NUM = 10;
const int N = 11;
const int S = 331;
const int P = (1 << NUM);

int n, s;
int dp[N][S][P];

void make_dp() {
  REP(i, N) REP(j, S) REP(k, P) dp[i][j][k] = 0;
  int num = 0;
  for (int mask = 1; mask < (1 << NUM); mask = (mask << 1)) {
    dp[1][num][mask] = 1;
    ++num;
  }

  FOR(i, 2, N) {
    num = 0;
    for (int mask = 1; mask < (1 << NUM); mask = (mask << 1)) {
      FOR(j, (num * i), S) {
        for (int k = 1; k < (1 << NUM); ++k) {
          if ((k & mask) != 0)
            continue;
          int p = (k | mask);
          if (j - (num * i) >= 0)
            dp[i][j][p] += dp[i - 1][j - (num * i)][k];
        }
      }
      ++num;
    }
  }
}

int main() {
  make_dp();
  while (cin >> n >> s) {
    if (n >= N || s >= S) {
      cout << 0 << endl;
      continue;
    }
    int ans = 0;
    REP(i, P) ans += dp[n][s][i];
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; i++)
#define FOR(i, j, k) for (int i = j; i < k; i++)
const int INF = INT_MAX / 2;
const int NUM = 10;
const int N = 11;
const int S = 331;
const int P = (1 << NUM);

int n, s;
int dp[N][S][P];

void make_dp() {
  REP(i, N) REP(j, S) REP(k, P) dp[i][j][k] = 0;
  int num = 0;
  for (int mask = 1; mask < (1 << NUM); mask = (mask << 1)) {
    dp[1][num][mask] = 1;
    ++num;
  }

  FOR(i, 2, N) {
    num = 0;
    for (int mask = 1; mask < (1 << NUM); mask = (mask << 1)) {
      FOR(j, (num * i), S) {
        for (int k = 1; k < (1 << NUM); ++k) {
          if ((k & mask) != 0)
            continue;
          int p = (k | mask);
          if (j - (num * i) >= 0)
            dp[i][j][p] += dp[i - 1][j - (num * i)][k];
        }
      }
      ++num;
    }
  }
}

int main() {
  make_dp();
  while (cin >> n >> s) {
    if (n > 10) {
      cout << 0 << endl;
      continue;
    }
    int ans = 0;
    REP(i, P) ans += dp[n][s][i];
    cout << ans << endl;
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

int number[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
long long int dp[10][10001];

long long int factorial(int x) {
  long long int ret = 1;
  for (int i = 1; i <= x; i++)
    ret *= i;
  return ret;
}

int main() {
  do {
    int sum = 0;
    for (int i = 0; i < 10; i++) {
      sum += number[i] * (i + 1);
      dp[i][sum]++;
    }
  } while (next_permutation(number, number + 10));

  int n, s;
  while (cin >> n >> s) {
    cout << dp[n - 1][s] / factorial(10 - n) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int table[10][10001];
int perm[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

void write() {
  int sum = 0;
  for (int i = 1; i <= 10; i++) {
    sum += i * perm[i - 1];
    table[i - 1][sum]++;
  }
}

void solve() {
  do {
    write();
  } while (next_permutation(perm, perm + 10));
}

int factorial(int n) {
  if (n == 1)
    return 1;
  return n * factorial(n - 1);
}

int main() {
  solve();

  int n, s;
  while (cin >> n >> s) {
    cout << table[n - 1][s] / factorial(10 - n) << endl;
  }

  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int n, s;
int dp[1024][1000][10];

int solve(int stat, int sum, int cnt) {

  if (dp[stat][sum][cnt] != -1)
    return dp[stat][sum][cnt];

  if (cnt == n) {
    if (sum == s)
      return 1;
    else
      return 0;
  }

  int ret = 0;

  for (int i = 0; i <= 9; ++i) {
    if (!(stat & 1 << i)) {
      ret += solve(stat | 1 << i, sum + i * (cnt + 1), cnt + 1);
    }
  }

  return dp[stat][sum][cnt] = ret;
}

int main() {

  while (cin >> n >> s) {

    memset(dp, -1, sizeof(dp));
    cout << solve(0, 0, 0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n, s;

int solve(int stat, int sum, int cnt) {

  if (cnt == n) {
    if (sum == s)
      return 1;
    else
      return 0;
  }

  int ret = 0;

  for (int i = 0; i <= 9; ++i) {
    if (!(stat & 1 << i)) {
      ret += solve(stat | 1 << i, sum + i * (cnt + 1), cnt + 1);
    }
  }

  return ret;
}

int main() {

  while (cin >> n >> s) {

    cout << solve(0, 0, 0) << endl;
  }
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
int x[11][600][1024], y[11][600], n, s;
int main() {
  x[0][0][0] = 1;
  for (int i = 1; i <= 10; i++) {
    for (int j = 0; j < 500; j++) {
      for (int k = 0; k < 1024; k++) {
        for (int l = 0; l < 10; l++) {
          if ((k / (1 << l)) % 2 == 0) {
            x[i][j + i * l][k + (1 << l)] += x[i - 1][j][k];
          }
        }
      }
    }
  }
  for (int i = 0; i <= 10; i++) {
    for (int j = 0; j < 500; j++) {
      for (int k = 0; k < 1024; k++) {
        y[i][j] += x[i][j][k];
      }
    }
  }
  while (cin >> n >> s) {
    cout << y[n][s] << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
int x[10][600][1024], y[10][600], n, s;
int main() {
  x[0][0][0] = 1;
  for (int i = 1; i < 10; i++) {
    for (int j = 0; j < 500; j++) {
      for (int k = 0; k < 1024; k++) {
        for (int l = 0; l < 10; l++) {
          if ((k / (int)pow(2, l)) % 2 == 0) {
            x[i][j + i * l][k + (int)pow(2, l)] += x[i - 1][j][k];
          }
        }
      }
    }
  }
  for (int i = 1; i < 10; i++) {
    for (int j = 0; j < 500; j++) {
      for (int k = 0; k < 1024; k++) {
        y[i][j] += x[i][j][k];
      }
    }
  }
  while (cin >> n >> s) {
    cout << y[n][s] << endl;
  }
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

#define M 10

using namespace std;

int NUM[M] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int ans;

int Combination(int n, int i, int j, int kumi[M]) {
  int br = 0;
  /*
  for(int c=0; c<n; c++){
      cout<<kumi[c]<<" ";
  }cout<<endl;
   */
  /*for(int k=0; k<i; k++){
      cout<<kumi[k]<<" ";
      br+=(i-k)*kumi[k];

  }
  cout<<" br :"<<br<<endl;
  if(br>n)return 0;
  */
  if (i == n) {
    int ret = 0;
    int br = 0, br2 = 0;
    for (int c = 0; c < n; c++) {
      // cout<<kumi[c]<<" ";
      br += (n - c) * kumi[c];
      br2 += (c + 1) * kumi[c];

    } // cout<<br<<endl;
    if (br > ans)
      return 0;
    if (br2 < ans)
      return 0;
    do {
      int s = 0;
      int k = 1;
      for (int c = 0; c < n; c++) {
        s += k * kumi[c];
        k++;
      }
      if (ans == s)
        ret++;

    } while (next_permutation(kumi, kumi + n));
    return ret;
  }
  if (j == M) {
    return 0;
  }
  int ret1, ret2;
  ret1 = Combination(n, i, j + 1, kumi);
  kumi[i] = NUM[j];
  ret2 = Combination(n, i + 1, j + 1, kumi);
  return ret1 + ret2;
}

int main() {
  int kumi[M] = {0};
  int n;
  while (cin >> n >> ans) {
    cout << Combination(n, 0, 0, kumi) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

#define M 10

using namespace std;

int NUM[M] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int ans;

int Combination(int n, int i, int j, int kumi[M]) {
  int br = 0;
  /*
  for(int c=0; c<n; c++){
      cout<<kumi[c]<<" ";
  }cout<<endl;
   */
  /*for(int k=0; k<i; k++){
      cout<<kumi[k]<<" ";
      br+=(i-k)*kumi[k];

  }
  cout<<" br :"<<br<<endl;
  if(br>n)return 0;
  */
  if (i == n) {
    int ret = 0;
    int br = 0;
    for (int c = 0; c < n; c++) {
      // cout<<kumi[c]<<" ";
      br += (n - c) * kumi[c];
    } // cout<<br<<endl;
    if (br > ans)
      return 0;
    do {
      int s = 0;
      int k = 1;
      for (int c = 0; c < n; c++) {
        s += k * kumi[c];
        k++;
      }
      if (ans == s)
        ret++;

    } while (next_permutation(kumi, kumi + n));
    return ret;
  }
  if (j == M) {
    return 0;
  }
  int ret1, ret2;
  ret1 = Combination(n, i, j + 1, kumi);
  kumi[i] = NUM[j];
  ret2 = Combination(n, i + 1, j + 1, kumi);
  return ret1 + ret2;
}

int main() {
  int kumi[M] = {0};
  int n;
  while (cin >> n >> ans) {
    cout << Combination(n, 0, 0, kumi) << endl;
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

// 23
#include <iostream>
#include <map>

using namespace std;

int m[11][1000];

void calc(int p, int d, int u) {
  if (d <= 10) {
    for (int i = 0; i <= 9; i++) {
      if (!(u & 1 << i)) {
        int cs = p + i * d;
        m[d][cs]++;
        calc(cs, d + 1, u | 1 << i);
      }
    }
  }
}

int main() {
  calc(0, 1, 0);
  for (int n, s; cin >> n >> s;) {
    cout << ((s < 1000) ? m[n][s] : 0) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

// 23
#include <iostream>
#include <map>

using namespace std;

int m[11][1000];

void calc(int p, int d, int u) {
  if (d <= 10) {
    for (int i = 0; i <= 9; i++) {
      if (!(u & 1 << i)) {
        int cs = p + i * d;
        m[d][cs]++;
        calc(cs, d + 1, u | 1 << i);
      }
    }
  }
}

int main() {
  calc(0, 1, 0);
  for (int n, s; cin >> n >> s;) {
    cout << m[n][s] << endl;
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

int n, s;
int a[1 << 11][340];

// S: つかったもの
// v: さっきつかったもの
// rn: 入れた数
// sum: 残る和
int rec(int S, int rn, int sum) {
  if (sum >= 0 && a[S][sum] != -1) {
    return a[S][sum];
  }

  if (sum == 0 && rn == n) {
    return 1;
  }

  if (rn == n) { // うまく入らなかった
    return 0;
  }

  if (sum < 0) {
    return 0;
  }

  int res = 0;
  for (int i = 0; i < 10; i++) {
    if (!(S >> i & 1) && sum - (rn + 1) * i >= 0) { // 使っていない
      res += rec(S | (1 << i), rn + 1, sum - (rn + 1) * i);
    }
  }
  return a[S][sum] = res;
}

int main() {
  int res;
  while (std::cin >> n >> s, !std::cin.eof()) {
    res = 0;
    for (int i = 0; i < (1 << 11); i++) {
      for (int j = 0; j < 340; j++) {
        a[i][j] = -1;
      }
    }
    for (int i = 0; i < 10; i++) {
      res += rec(1 << i, 1, s - i);
    }

    std::cout << res << std::endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

int n, s;
int a[1 << 10][340];

// S: つかったもの
// v: さっきつかったもの
// rn: 入れた数
// sum: 残る和
int rec(int S, int rn, int sum) {
  if (sum >= 0 && a[S][sum] != -1) {
    return a[S][sum];
  }

  if (sum == 0 && rn == n) {
    return 1;
  }

  if (rn == n) { // うまく入らなかった
    return 0;
  }

  if (sum < 0) {
    return 0;
  }

  int res = 0;
  for (int i = 0; i < 10; i++) {
    if (!(S >> i & 1) && sum - (rn + 1) * i >= 0) { // 使っていない
      res += rec(S | (1 << i), rn + 1, sum - (rn + 1) * i);
    }
  }
  return a[S][sum] = res;
}

int main() {
  int res;
  while (std::cin >> n >> s, !std::cin.eof()) {
    res = 0;
    for (int i = 0; i < (1 << 10); i++) {
      for (int j = 0; j < 340; j++) {
        a[i][j] = -1;
      }
    }
    for (int i = 0; i < 10; i++) {
      res += rec(1 << i, 1, s - i);
    }

    std::cout << res << std::endl;
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;

int num[21];
int fact[21];
int ans[11][1101];

int main() {
  for (int i = 0; i < 10; i++) {
    num[i] = i;
  }

  fact[0] = 1;
  for (int i = 1; i <= 10; i++) {
    fact[i] = fact[i - 1] * i;
  }

  do {
    int sum = 0;
    for (int i = 0; i < 10; i++) {
      sum += num[i] * (i + 1);
      ans[i + 1][sum]++;
    }
  } while (next_permutation(num, num + 10));

  while (1) {
    int n, s;
    if (scanf("%d%d", &n, &s) != 2)
      return 0;
    if (s >= 1000)
      puts("0");
    else
      printf("%d\\n", ans[n][s] / fact[10 - n]);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;

int num[11];
int fact[11];
int ans[11][1001];

int main() {
  for (int i = 0; i < 10; i++) {
    num[i] = i;
  }

  fact[0] = 1;
  for (int i = 1; i <= 10; i++) {
    fact[i] = fact[i - 1] * i;
  }

  do {
    int sum = 0;
    for (int i = 0; i < 10; i++) {
      sum += num[i] * (i + 1);
      ans[i + 1][sum]++;
    }
  } while (next_permutation(num, num + 10));

  while (1) {
    int n, s;
    if (scanf("%d%d", &n, &s) != 2)
      return 0;
    printf("%d\\n", ans[n][s] / fact[10 - n]);
  }
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int ul[10];
bool used[10];

int dfs(int x, int n, int s) {
  int r = 0;
  if (!used[x]) {
    if (s < n * x)
      return 0;
    else if (s == n * x && n == 1)
      return 1;
    else if (n > 1 && ul[n - 1] >= s - n * x) {
      used[x] = true;
      r = dfs(0, n - 1, s - n * x);
      used[x] = false;
    }
  }
  if (x < 9) {
    r += dfs(x + 1, n, s);
  }
  return r;
}

int main() {
  int n, s;

  for (int i = 1; i <= 10; i++) {
    ul[i - 1] = i * (i + 1) * (28 - i) / 6;
  }

  while (cin >> n >> s) {
    for (int i = 0; i < n; i++)
      used[i] = false;
    cout << dfs(0, n, s) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool used[10];

int dfs(int x, int n, int s) {
  int r = 0;
  if (!used[x]) {
    if (s < n * x)
      return 0;
    else if (s == n * x && n == 1)
      return 1;
    else if (n > 1) {
      used[x] = true;
      r = dfs(0, n - 1, s - n * x);
      used[x] = false;
    }
  }
  if (x < 9) {
    r += dfs(x + 1, n, s);
  }
  return r;
}

int main() {
  int n, s;

  while (cin >> n >> s) {
    for (int i = 0; i < n; i++)
      used[i] = false;
    cout << dfs(0, n, s) << endl;
  }

  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
int dp[10 + 5][331 + 5][(1 << 10) + 5];

int calc(int n, int s, int a) {
  if (s > 330 || s < 0)
    return 0;
  if (dp[n][s][a] >= 0)
    return dp[n][s][a];
  if (n == 1) {
    if (s < 10 && a >> s & 1)
      return dp[n][s][a] = 1;
    else
      return dp[n][s][a] = 0;
  }
  int ret = 0;
  rep(i, 10) {
    if (a >> i & 1)
      ret += calc(n - 1, s - n * i, a - (1 << i));
  }
  return dp[n][s][a] = ret;
}

int main() {
  rep(i, 10 + 5) {
    rep(j, 331 + 5) {
      rep(k, (1 << 10) + 5) {
        if (i == 0)
          dp[i][j][k] = 0;
        else
          dp[i][j][k] = -1;
      }
    }
  }
  int n, s;
  while (cin >> n) {
    cin >> s;
    int ans;
    ans = calc(n, s, (1 << 10) - 1);
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
int dp[10 + 5][331 + 5][1 << 10 + 5];

int calc(int n, int s, int a) {
  if (s > 330 || s < 0)
    return 0;
  if (dp[n][s][a] >= 0)
    return dp[n][s][a];
  if (n == 1) {
    if (s < 10 && a >> s & 1)
      return dp[n][s][a] = 1;
    else
      return dp[n][s][a] = 0;
  }
  int ret = 0;
  rep(i, 10) {
    if (a >> i & 1)
      ret += calc(n - 1, s - n * i, a - (1 << i));
  }
  return dp[n][s][a] = ret;
}

int main() {
  rep(i, 10) {
    rep(j, 331) {
      rep(k, 1 << 10) {
        if (i == 0)
          dp[i][j][k] = 0;
        else
          dp[i][j][k] = -1;
      }
    }
  }
  int n, s;
  while (cin >> n) {
    cin >> s;
    int ans;
    ans = calc(n, s, (1 << 10) - 1);
    cout << ans << endl;
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

const int MAX_N = 10 + 2;
int count1;
int perm[MAX_N];
bool used[MAX_N];
int n, s, sum;

void permutation1(int pos, int n, int sum) {
  if (sum > s)
    return;
  int b = 9;
  int sum2 = 0;
  for (int i = n; i > pos; i--) {
    while (used[b])
      b--;
    sum2 += (i * b);
    // cout<<sum2<<endl;
  }
  if ((s - sum) > sum2)
    return;
  // if(s>sum2) return;

  if (pos == n) {
    if (sum == s) {
      count1++;
      // for(int i=0;i<n;i++) cout<<perm[i]<<" ";
      // cout<<endl;
    }
    return;
  }
  for (int i = 0; i <= 9; i++) {
    if (!used[i]) {
      perm[pos] = i;
      used[i] = true;
      permutation1(pos + 1, n, sum + i * (pos + 1));
      used[i] = false;
    }
  }
  return;
}

int main() {
  while (cin >> n >> s) {
    // for(int i=0;i<=n;i++) perm[i]=i;
    // used[MAX_N]={false};
    int b = 9;
    int sum_max = 0;
    int sum_min = 0;
    for (int i = n; i >= 1; i--) {
      sum_max += (b * i);
      sum_min += (9 - b) * i;
      b--;
    }
    if (s > sum_max || s < sum_min)
      cout << "0" << endl;
    else {
      count1 = 0;
      sum = 0;
      permutation1(0, n, 0);
      cout << count1 << endl;
    }
  }
  // while(1);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

const int MAX_N = 10 + 2;
int count1;
int perm[MAX_N];
bool used[MAX_N];
int n, s, sum;

void permutation1(int pos, int n, int sum) {
  if (sum > s)
    return;
  if (pos == n) {
    if (sum == s) {
      count1++;
      // for(int i=0;i<n;i++) cout<<perm[i]<<" ";
      // cout<<endl;
    }
    return;
  }
  for (int i = 0; i <= 9; i++) {
    if (!used[i]) {
      perm[pos] = i;
      used[i] = true;
      permutation1(pos + 1, n, sum + i * (pos + 1));
      used[i] = false;
    }
  }
  return;
}

int main() {
  while (cin >> n >> s) {
    // for(int i=0;i<=n;i++) perm[i]=i;
    // used[MAX_N]={false};
    count1 = 0;
    sum = 0;
    int sum2 = 0;
    int sum3 = 0;
    int kk = n;
    int b = 10;
    while (kk != 0) {
      sum2 += kk * b;
      sum3 += kk * (10 - b);
      b--;
      kk--;
    }
    if (s < sum3 || s > sum2)
      cout << "0" << endl;
    else {
      permutation1(0, n, 0);
      cout << count1 << endl;
    }
  }
  // while(1);
  return 0;
}