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

#include <bitset>
#include <iostream>

int main() {
  const int N = 10;
  const int B = 1 << N;
  const int SMAX =
      330; // 10*9 + 9*8 + ... + 2*1 + 1*0 = 90+72+56+42+30+20+12+6+2+0 = 330
  int table[N + 1][SMAX + 1] = {0};

  int(*bitTable)[SMAX + 1] = new int[B][SMAX + 1];

  int emax[N + 1] = {0};
  for (int i = 1; i <= N; ++i) {
    emax[i] = emax[i - 1] + 9 * i - i * (i - 1) / 2;
  }

  bitTable[0][0] = 1;

  for (int i = 0; i < B; ++i) {
    std::bitset<N> bs(i);
    int nbc = bs.count() + 1;

    for (int j = 0; j < N; ++j) {
      if (bs.test(j) == false) {
        std::bitset<N> nextBit = bs;
        nextBit.set(j);

        int d = j * nbc;

        for (int k = 0; k <= emax[nbc]; ++k) {
          bitTable[nextBit.to_ulong()][k + d] += bitTable[i][k];
        }
      }
    }
  }

  for (int i = 0; i < B; ++i) {
    std::bitset<N> bs(i);
    int n = bs.count();
    for (int s = 0; s <= emax[n]; ++s) {
      table[n][s] += bitTable[bs.to_ulong()][s];
    }
  }

  delete[] bitTable;

  int n, s;
  while (std::cin >> n >> s) {
    if (n <= 0 || n > N || s > SMAX || s < 0) {
      std::cout << 0 << std::endl;
      continue;
    }
    std::cout << table[n][s] << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bitset>
#include <iostream>

int main() {
  const int N = 10;
  const int B = 1 << N;
  const int SMAX =
      330; // 10*9 + 9*8 + ... + 2*1 + 1*0 = 90+72+56+42+30+20+12+6+2+0 = 330
  int table[N + 1][SMAX + 1] = {0};

  int(*bitTable)[SMAX + 1] = new int[B][SMAX + 1];

  int emax[N + 1] = {0};
  for (int i = 1; i <= N; ++i) {
    emax[i] = emax[i - 1] + 9 * i - i * (i - 1) / 2;
  }

  clock_t start = clock();

  bitTable[0][0] = 1;

  for (int i = 0; i < B; ++i) {
    std::bitset<N> bs(i);
    int nbc = bs.count() + 1;

    for (int j = 0; j < N; ++j) {
      if (bs.test(j) == false) {
        std::bitset<N> nextBit = bs;
        nextBit.set(j);

        int d = j * nbc;

        for (int k = 0; k < emax[nbc]; ++k) {
          bitTable[nextBit.to_ulong()][k + d] += bitTable[i][k];
        }
      }
    }
  }

  for (int i = 0; i < B; ++i) {
    std::bitset<N> bs(i);
    int n = bs.count();
    for (int s = 0; s < emax[n]; ++s) {
      table[n][s] += bitTable[bs.to_ulong()][s];
    }
  }

  delete[] bitTable;

  int n, s;
  while (std::cin >> n >> s, n) {
    std::cout << table[n][s] << std::endl;
  }

  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

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

int dp[20][500][2048];

int main() {
  dp[0][0][0] = 1;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j <= 330; j++) {
      for (int k = 0; k < 2048; k++) {
        if (dp[i][j][k]) {
          for (int l = 0; l <= 9; l++) {
            if ((~k) & (1 << l)) {
              dp[i + 1][j + (i + 1) * l][k | (1 << l)] += dp[i][j][k];
            }
          }
        }
      }
    }
  }

  int n, s;
  while (scanf("%d %d", &n, &s) != EOF) {
    if (s > 330) {
      puts("0");
      continue;
    }

    int sum = 0;
    for (int i = 0; i < 2048; i++) {
      sum += dp[n][s][i];
    }
    printf("%d\\n", sum);
  }

  return 0;
}

Error: -11

Fixed submission:

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

int dp[10][331][2048];

int main() {
  dp[0][0][0] = 1;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j <= 330; j++) {
      for (int k = 0; k < 2048; k++) {
        if (dp[i][j][k]) {
          for (int l = 0; l <= 9; l++) {
            if ((~k) & (1 << l)) {
              dp[i + 1][j + (i + 1) * l][k | (1 << l)] += dp[i][j][k];
            }
          }
        }
      }
    }
  }

  int n, s;
  while (scanf("%d %d", &n, &s) != EOF) {
    if (s > 330) {
      puts("0");
      continue;
    }

    int sum = 0;
    for (int i = 0; i < 2048; i++) {
      sum += dp[n][s][i];
    }
    printf("%d\\n", sum);
  }

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

#include <cstdio>

bool used[10];
int num[10][10000];
int sum, ans, n, s, Min, Max;

inline void A(int t, int x) {
  if (x == 11)
    return;
  int tmp = -x;
  for (int i = 9; i >= 0; i--) {
    tmp += x;
    if (used[i])
      continue;
    used[i] = true;
    sum += tmp;
    num[t][sum]++;
    A(t + 1, x + 1);
    sum -= tmp;
    used[i] = false;
  }
}

int main() {
  A(0, 1);
  while (scanf("%hd%hd", &n, &s) != EOF) {
    if (s >= 10000 || s < 0) {
      printf("0\\n");
    } else {
      printf("%d\\n", num[n - 1][s]);
    }
  }
}

Error: TLE

Fixed submission:

#include <cstdio>

bool used[10];
int num[10][10000];
int sum, ans, n, s, Min, Max;

inline void A(int t, int x) {
  if (x == 0) {
    num[t][sum]++;
  }
  int tmp = -x;
  for (int i = 9; i >= 0; i--) {
    tmp += x;
    if (used[i])
      continue;
    used[i] = true;
    sum += tmp;
    A(t, x - 1);
    sum -= tmp;
    used[i] = false;
  }
}

int main() {
  for (int i = 0; i < 10; i++)
    A(i, i + 1);
  while (scanf("%hd%hd", &n, &s) != EOF) {
    if (s >= 10000 || s < 0) {
      printf("0\\n");
    } else {
      printf("%d\\n", num[n - 1][s]);
    }
  }
}
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

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

int dp[20][331][2000];

int n, s;
int main() {
  dp[0][0][0] = 1;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 331; j++) {
      for (int k = 0; k < (1 << 10); k++) {
        if (dp[i][j][k] == 0)
          continue;
        for (int l = 0; l < 10; l++) {
          if (!((k >> l) & 1) && j + l * (i + 1) <= 330) {
            dp[i + 1][j + l * (i + 1)][k | (1 << l)] += dp[i][j][k];
          }
        }
      }
    }
  }
  while (cin >> n >> s) {
    int ans = 0;
    if (s > 330) {
      printf("0\\n");
      continue;
    }
    for (int i = 0; i < (1 << 10); i++) {
      ans += dp[n][s][i];
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

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

int dp[20][331][2000];

int main() {
  dp[0][0][0] = 1;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 331; j++) {
      for (int k = 0; k < (1 << 10); k++) {
        if (dp[i][j][k] == 0)
          continue;
        for (int l = 0; l < 10; l++) {
          if (!((k >> l) & 1)) {
            dp[i + 1][j + l * (i + 1)][k | (1 << l)] += dp[i][j][k];
          }
        }
      }
    }
  }
  int n, s;
  while (cin >> n >> s) {
    int ans = 0;
    for (int i = 0; i < (1 << 10); i++) {
      ans += dp[n][s][i];
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, s, dp[12][331][1030];
int solve(int now, int sum, int a) {
  int ans = 0;
  if (dp[now][sum][a] != -1)
    return dp[now][sum][a];
  if (sum > s)
    return dp[now][sum][a] = 0;
  if (now == n + 1) {
    if (sum == s)
      return dp[now][sum][a] = 1;
  }
  now++;
  for (int i = 0; i < 10; i++) {
    if (!((a >> i) & 1))
      ans += solve(now, sum + i * (now - 1), a | (1 << i));
  }
  return dp[now - 1][sum][a] = ans;
}
int main() {
  while (cin >> n >> s) {
    int a[10];
    for (int i = 0; i < 12; i++)
      for (int j = 0; j < 331; j++)
        for (int k = 0; k < 1030; k++)
          dp[i][j][k] = -1;
    // now sum
    cout << solve(1, 0, 0) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, s, dp[12][331][1030];
int solve(int now, int sum, int a) {
  int ans = 0;
  if (dp[now][sum][a] != -1)
    return dp[now][sum][a];
  if (sum > s)
    return dp[now][sum][a] = 0;
  if (now == n + 1) {
    if (sum == s)
      return dp[now][sum][a] = 1;
  }
  now++;
  for (int i = 0; i < 10; i++) {
    if (!((a >> i) & 1))
      ans += solve(now, sum + i * (now - 1), a | (1 << i));
  }
  return dp[now - 1][sum][a] = ans;
}
int main() {
  while (cin >> n >> s) {
    int a[10];
    for (int i = 0; i <= n + 1; i++)
      for (int j = 0; j <= n * 9 * 9; j++)
        for (int k = 0; k < 1 << 10; k++)
          dp[i][j][k] = -1;
    // now sum
    cout << solve(1, 0, 0) << endl;
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>

using namespace std;

int retnum[11][10000];
bool isused[10];

void filling(int size, int depth, int sum) {
  int tmp;

  // cout << "begin = " << begin << " depth = " << depth << " sum = " << sum <<
  // endl;
  if (size == depth - 1) {
    retnum[size][sum]++;
    return;
  }
  // cout << "retnum[" << depth << "][" << sum+begin*depth << "] = " <<
  // retnum[depth][sum+begin*depth] << endl;
  for (int i = 0; i <= 9; i++) {
    if (!isused[i]) {
      isused[i] = true;
      filling(size, depth + 1, sum + i * depth);
      isused[i] = false;
    }
  }
}

int main(void) {

  memset(retnum, 0, sizeof(retnum));
  memset(isused, false, sizeof(isused));

  for (int i = 1; i <= 10; i++) {
    filling(i, 1, 0);
    int tmp;
    // cin >> tmp;
  }

  int n, m;
  while (cin >> n >> m) {
    cout << retnum[n][m] << endl;
  }
}

Error: TLE

Fixed submission:

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

int main(void) {
  int n, s;
  int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  int tmp;
  int ret;
  int fact;

  while (cin >> n) {
    cin >> s;

    ret = 0;
    for (int i = 0; i < 10; i++)
      array[i] = i; // initialize array;

    do {
      tmp = 0;
      for (int i = 1; i <= n; i++) {
        tmp += i * array[10 - i];
        if (tmp > s)
          break;
        // cout << i << "*" << array[10-i] << "  + ";
      }
      // cout << endl;
      if (tmp == s)
        ret++;
    } while (next_permutation(array, array + 10));
    fact = 1;
    for (int i = 2; i <= 10 - n; i++) {
      fact *= i;
    }
    cout << ret / fact << endl;
  }
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int m[10][1024][350];
int search(int n, int s, int p) {
  if (n > 10 || n < 1 || s < 0 || s >= 350)
    return 0;
  if (n == 1) {
    if (s > 9)
      return 0;
    if ((p & (1 << s)) == 0)
      return 1;
    else
      return 0;
  }
  if (m[n - 1][p][s] >= 0)
    return m[n - 1][p][s];
  int r = 0;
  for (int j = min(s / n, 9); j >= 0; j--) {
    if ((p & (1 << j)) == 0) {
      r += search(n - 1, s - n * j, (p | 1 << j));
    }
  }
  m[n - 1][p][s] = r;
  return r;
}
int main() {
  int n, s;
  memset(m, -1, sizeof(m));
  while (cin >> n >> s) {
    cout << search(n, s, 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int m[10][1024][350];
int search(int n, int s, int p) {
  if (n == 1) {
    if (s > 9)
      return 0;
    if ((p & (1 << s)) == 0)
      return 1;
    else
      return 0;
  }
  //  if(m[n-1][p][s] != -1) return m[n-1][p][s];
  int r = 0;
  for (int j = min(s / n, 9); j >= 0; j--) {
    if ((p & (1 << j)) == 0) {
      r += search(n - 1, s - n * j, (p | 1 << j));
    }
  }
  //  m[n-1][p][s] = r;
  return r;
}
int main() {
  int n, s;
  memset(m, -1, sizeof(m));
  while (cin >> n >> s) {
    cout << search(n, s, 0) << endl;
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int table[10 + 1][331];
bool memo[10];
int n;

void rec(int depth, int sum) {
  table[depth][sum]++;
  for (int i = 0; i <= 9; i++) {
    if (memo[i])
      continue;
    memo[i] = true;
    rec(depth + 1, sum + i * (depth + 1));
    memo[i] = false;
  }
}

void q70() {
  memset(table, 0, sizeof(table));
  memset(memo, 0, sizeof(memo));
  rec(0, 0);
  int n, s;
  for (; cin >> n >> s;) {
    if (n > 10 || s > 330) {
      cout << "0" << endl;
      continue;
    }
    cout << table[n][s] << endl;
  }
}

int main() {
  q70();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

#define MAX_N 10
#define MAX_S 10000
int ans;
int N;

int choose[MAX_N];
int choose_size;
int rest[MAX_N];
int rest_size;

int rec() {
  if (choose_size == N) {
    int sum = 0;
    for (int i = 0; i < N; i++) {
      sum += (i + 1) * choose[i];
    }
    return ans == sum;
  }

  int cnt = 0;
  for (int i = 0; i < rest_size; i++) {
    choose[choose_size++] = rest[i];
    for (int j = i; j < rest_size - 1; j++) {
      rest[j] = rest[j + 1];
    }
    rest_size--;

    cnt += rec();

    rest_size++;
    for (int j = rest_size - 1; j > i; j--) {
      rest[j] = rest[j - 1];
    }
    rest[i] = choose[--choose_size];
  }
  return cnt;
}

void q70() {
  int n, s;
  for (; cin >> n >> s;) {
    N = n;
    ans = s;
    choose_size = 0;
    for (int i = 0; i < MAX_N; i++) {
      rest[i] = i;
    }
    rest_size = 10;
    cout << rec() << endl;
    ;
  }
}

int main() {
  q70();
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

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

int bitCount(unsigned int x) {
  x = (x >> 1 & 0x55555555) + (x & 0x55555555);
  x = (x >> 2 & 0x33333333) + (x & 0x33333333);
  x = (x >> 4 & 0x0f0f0f0f) + (x & 0x0f0f0f0f);
  x = (x >> 8 & 0x00ff00ff) + (x & 0x00ff00ff);
  return (x >> 16) + (x & 0x0000ffff);
}

int dp[1024][512];
int main() {
  dp[0][0] = 1;
  for (int i = 0; i < (1 << 10) - 1; i++) {
    for (int j = 0; j < 512; j++) {
      if (dp[i][j] == 0)
        continue;
      for (int k = 0; k < 10; k++) {
        if (i >> k & 1)
          continue;
        dp[i | 1 << k][j + (bitCount(i) + 1) * k] += dp[i][j];
      }
    }
  }
  int n, s;
  while (cin >> n >> s) {
    if (!n) {
      cout << !s << endl;
      continue;
    }
    if (512 < s) {
      cout << 0 << endl;
      continue;
    }
    int ans = 0;
    int comb = (1 << n) - 1;
    while (comb < 1 << 10) {
      int x = comb & -comb, y = comb + x;
      ans += dp[comb][s];
      comb = ((comb & ~y) / x >> 1) | y;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int bitCount(unsigned int x) {
  x = (x >> 1 & 0x55555555) + (x & 0x55555555);
  x = (x >> 2 & 0x33333333) + (x & 0x33333333);
  x = (x >> 4 & 0x0f0f0f0f) + (x & 0x0f0f0f0f);
  x = (x >> 8 & 0x00ff00ff) + (x & 0x00ff00ff);
  return (x >> 16) + (x & 0x0000ffff);
}

int dp[1024][500];
int main() {
  dp[0][0] = 1;
  for (int i = 0; i < (1 << 10) - 1; i++) {
    for (int j = 0; j < 500; j++) {
      if (dp[i][j] == 0)
        continue;
      for (int k = 0; k < 10; k++) {
        if (i >> k & 1)
          continue;
        dp[i | 1 << k][j + (bitCount(i) + 1) * k] += dp[i][j];
      }
    }
  }
  int n, s;
  while (cin >> n >> s) {
    int ans = 0;
    int comb = (1 << n) - 1;
    while (comb < 1 << 10) {
      int x = comb & -comb, y = comb + x;
      ans += dp[comb][s];
      comb = ((comb & ~y) / x >> 1) | y;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int dp[1 << 10][331];
int n, s;

int dfs(int t, int sum, int k) {
  if (t == 0)
    return sum == s ? 1 : 0;
  if (s < sum)
    return 0;
  if (dp[t][s - sum] >= 0)
    return dp[t][s - sum];
  int res = 0;
  for (int i = 0; i < 10; i++) {
    if ((t & (1 << i)) > 0) {
      res += dfs(t - (1 << i), sum + i * k, k - 1);
    }
  }

  return dp[t][s - sum] = res;
}

int main() {
  for (int i = 0; i < (1 << 10); i++) {
    for (int j = 0; j <= 330; j++) {
      dp[i][j] = -1;
    }
  }
  while (cin >> n >> s) {
    if (s > 330) {
      cout << 0 << endl;
      continue;
    }
    int res = 0;
    for (int i = 1; i < (1 << 10); i++) {
      int t = i, count = 0;
      while (t) {
        count += t % 2;
        t /= 2;
      }
      if (n == count)
        res += dfs(i, 0, count);
    }
    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int n, s;
int dp[1 << 10][10000];

int c(int i, int sum, int t) {
  if (t == 0)
    return (sum == s ? 1 : 0);
  if (sum > s)
    return 0;
  if (dp[i][s - sum] > 0)
    return dp[i][s - sum];
  int res = 0;
  for (int j = 0; j < 10; j++) {
    if ((i & 1 << j) > 0 && c(i - (1 << j), sum + j * t, t - 1))
      res++;
  }
  return dp[i][s - sum] = res;
}

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

  while (cin >> n >> s) {
    int res = 0;
    for (int i = 1; i < 1 << 10; i++) {
      int t = 0;
      for (int j = 0; j < 10; j++)
        if ((i & (1 << j)) > 0)
          t++;
      if (t == n) {
        res += c(i, 0, t);
      }
    }
    cout << res << endl;
  }

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }
void rec(int s, int n, int f);

int ans[12][333];
void rec(int n, int s, int f) {
  if (n == 11)
    return;
  ans[n][s]++;

  n++;
  rep(i, 10) if (!(f >> i & 1)) rec(n, s + i * n, f | 1 << i);
}

int main() {
  int n, s;
  rec(0, 0, 0);
  while (cin >> n >> s)
    cout << (s > 330 || n > 10 ? 0 : ans[n][s]) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }
ll rec(int now, int n, int s, vector<int> f);

ll rec(int now, int n, int s, vector<int> f) {
  if (s < 0)
    return false;
  if (now == n)
    return !s;

  now++;
  vector<int>::iterator it = f.begin();
  int res = 0, len = f.size();
  for (int i = 0; i < len; i++) {
    int buf = (*it);
    it = f.erase(it);
    res += rec(now, n, s - buf * now, f);
    f.push_back(buf);
  }

  return res;
}

int main() {
  int n, s;
  while (cin >> n >> s) {
    vector<int> f;
    rep(i, 10) f.push_back(i);

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

#include <iostream>

int memo[12][1024];

void dfs(int d, int bit, int sum) {
  ++memo[d][sum];
  for (int i = 0; i != 10; ++i) {
    if (bit & (1 << i))
      dfs(d + 1, bit & ~(1 << i), sum + (d + 1) * i);
  }

  return;
}

int main() {
  int n, s;
  dfs(0, (1 << 10) - 1, 0);
  while (std::cin >> n >> s)
    std::cout << (s > 1023 ? 0 : memo[n][s]) << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int n, s;
  while (std::cin >> n >> s) {
    int ans = 0;
    int set = (1 << n) - 1;

    while (set < 1 << 10) {
      std::vector<int> k;

      for (int j = 0; j != 10; ++j)
        if (set >> j & 1)
          k.push_back(j);

      do {
        int sum = 0;
        for (int i = 0; i != n; ++i)
          sum += (i + 1) * k[i];

        if (sum == s)
          ++ans;
      } while (std::next_permutation(k.begin(), k.end()));

      int x = set & -set, y = set + x;
      set = ((set & ~y) / x >> 1) | y;
    }

    std::cout << ans << std::endl;
  }

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

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

int memo[1111][11][1111];

int solve(int bit, int n, int s) {

  if (!n && !s)
    return memo[bit][n][s] = 1;
  if (!n || s < 0)
    return memo[bit][n][s] = 0;
  if (~memo[bit][n][s])
    return memo[bit][n][s];

  int ans = 0;
  for (int i = 0; i < 10; i++) {
    if (!(bit & (1 << i)))
      ans += solve(bit | (1 << i), n - 1, s - i * n);
  }

  return memo[bit][n][s] = ans;
}

int main() {

  int n, s;
  while (cin >> n >> s) {
    memset(memo, -1, sizeof(memo));
    cout << solve(0, n, s) << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int memo[1111][11][1111];

int solve(int bit, int n, int s) {

  if (!n && !s)
    return 1;
  if (!n || s < 0)
    return 0;
  if (memo[bit][n][s])
    return memo[bit][n][s];

  int ans = 0;
  for (int i = 0; i < 10; i++) {
    if (!(bit & (1 << i)))
      ans += solve(bit | (1 << i), n - 1, s - i * n);
  }

  return memo[bit][n][s] = ans;
}

int main() {

  int n, s;
  while (cin >> n >> s)
    cout << solve(0, n, s) << endl;
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

#define DATA_SIZE 10

int pow(int num) {
  if (num <= 1) {
    return 1;
  }
  return num * pow(num - 1);
}

int main() {

  int sum[11][10001]; // sum[i][j]: n = i ?????¨??????????¨??????? j
                      // ??¨????????????????????°

  for (int i = 0; i < 11; i++) {
    for (int j = 0; j < 10001; j++) {
      sum[i][j] = 0;
    }
  }

  int a[DATA_SIZE];

  for (int i = 0; i < DATA_SIZE; i++) {
    a[i] = i;
  }

  do {
    int p = 0;
    for (int i = 1; i <= 10; i++) {
      p += i * a[i - 1];
      sum[i][p]++;
    }
  } while (next_permutation(a, a + DATA_SIZE));

  int n, s;

  while (true) {
    cin >> n >> s;
    if (cin.eof()) {
      break;
    }
    cout << sum[n][s] / pow(10 - n) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

#define DATA_SIZE 10

int pow(int num) {
  if (num <= 1) {
    return 1;
  }
  return num * pow(num - 1);
}

int main() {

  int sum[11][10001]; // sum[i][j]: n = i ?????¨??????????¨??????? j
                      // ??¨????????????????????°

  for (int i = 0; i < 11; i++) {
    for (int j = 0; j < 10001; j++) {
      sum[i][j] = 0;
    }
  }

  int a[DATA_SIZE];

  for (int i = 0; i < DATA_SIZE; i++) {
    a[i] = i;
  }

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

  while (true) {

    int n, s;

    while (true) {
      cin >> n >> s;
      if (cin.eof()) {
        break;
      }
      cout << sum[n][s] / pow(10 - n) << endl;
    }
  }

  return 0;
}