alexjercan/bugnet huggingface dataset:
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <iostream>
#include <map>

using namespace std;

int main() {
  int const xs[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  map<int, int> m[11];
  for (int i = 0; i < 1024; ++i) {
    int n = 0;
    array<int, 10> t;
    for (int j = 0; j < 10; ++j) {
      if (i & 1 << j) {
        t[n++] = xs[j];
      }
    }
    do {
      int sum = 0;
      for (int j = 0; j < n; ++j)
        sum += (j + 1) * t[j];
      ++m[n][sum];
    } while (next_permutation(t.data(), t.data() + n));
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int const xs[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

  int n, s;
  while (cin >> n >> s) {
    int num = 0;
    for (int i = 0; i < 1024; ++i) {
      int v[10];
      int nx = 0;
      for (int j = 0; j < 10; ++j) {
        if (i & 1 << j) {
          v[nx] = xs[j];
          ++nx;
        }
      }
      if (nx != n)
        continue;

      do {
        int sum = 0;
        for (int j = 0; j < n; ++j)
          sum += (j + 1) * v[j];
        if (sum == s)
          ++num;
      } while (next_permutation(v, v + n));
    }
    cout << num << endl;
  }
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define REP(i, a, b) for (int i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)
int memo[11][1024][300];
int func(int k, int used_n, int n, int s, int goal) {
  if (k == n)
    return s == goal;

  int &res = memo[k][used_n][s];
  if (res >= 0)
    return res;
  res = 0;
  rep(j, 10) if (!(used_n & (1 << j))) {
    res += func(k + 1, used_n + (1 << j), n, s + (k + 1) * j, goal);
  }
  return res;
}
int main() {
  int n, s;
  while (cin >> n >> s) {
    memset(memo, -1, sizeof(memo));
    int res = n > 10 ? 0 : func(0, 0, n, 0, s);
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
#define REP(i, a, b) for (int i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)
set<int> res;
void func(int k, int used_n, int n, int s, int goal, int v) {
  if (k == n) {
    if (s == goal)
      res.insert(v);
    return;
  }
  rep(j, 10) if (!(used_n & (1 << j))) {
    func(k + 1, used_n + (1 << j), n, s + (k + 1) * j, goal, 10 * v + j);
  }
}
int main() {
  int n, s;
  while (cin >> n >> s) {
    res.clear();
    func(0, 0, n, 0, s, 0);
    cout << res.size() << endl;
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int dp[11][331][1025];

int solve(int n, int s, int used) {
  if (n == 0 && s == 0)
    return 1;
  if (n <= 0 || s < 0)
    return 0;
  if (dp[n][s][used] != -1)
    return dp[n][s][used];

  int sum = 0;
  for (int i = 0; i < 10; i++) {
    if ((used >> i) % 2 == 0) {
      used += 1 << i;
      sum += solve(n - 1, s - i * n, used);
      used -= 1 << i;
    }
  }

  return dp[n][s][used] = sum;
}

int main(void) {
  int n, s;

  memset(dp, -1, sizeof(dp));

  while (cin >> n >> s) {
    if (0 <= s && s <= 330) {
      int used = 0;
      cout << solve(n, s, used) << endl;
    } else {
      cout << 0 << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int solve(int n, int s, bool used[]) {
  if (n == 0 && s == 0)
    return 1;
  if (n <= 0 || s < 0)
    return 0;

  int sum = 0;
  for (int i = 0; i < 10; i++) {
    if (!used[i]) {
      used[i] = true;
      sum += solve(n - 1, s - i * n, used);
      used[i] = false;
    }
  }

  return sum;
}

int main(void) {
  int n, s;

  while (cin >> n >> s) {
    if (0 <= s && s <= 330) {
      bool used[10];
      fill(used, used + 10, false);
      cout << solve(n, s, used) << endl;
    } else {
      cout << 0 << endl;
    }
  }

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

#include <stdio.h>
#include <string.h>

#define SIZE (750)

int dp[11][1 << 10][SIZE];

int main(void) {
  int n;
  char buf[256];
  int s;

  memset(dp, 0, sizeof(dp));
  dp[0][0][0] = 1;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < (1 << 10); j++) {
      for (int k = 0; k < SIZE; k++) {
        for (int l = 0; l < 10; l++) {
          if (((j >> l) & 1) == 0) {
            dp[i + 1][j | (1 << l)][k + ((i + 1) * l)] += dp[i][j][k];
          }
        }
      }
    }
  }
  while (fgets(buf, 255, stdin) != NULL) {
    sscanf(buf, "%d%d", &n, &s);
    int ans = 0;
    if (s < SIZE) {
      for (int i = 0; i < (1 << 10); i++) {
        ans += dp[n][i][s];
      }
    } else {
      ans = 0;
    }

    printf("%d\\n", ans);
  }

  return (0);
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <string.h>

#define SIZE (750)

int dp[11][1 << 10][SIZE];

int main(void) {
  int n;
  char buf[256];
  int s;

  memset(dp, 0, sizeof(dp));
  dp[0][0][0] = 1;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < (1 << 10); j++) {
      for (int k = 0; k < SIZE; k++) {
        for (int l = 0; l < 10; l++) {
          if (((j >> l) & 1) == 0) {
            dp[i + 1][j | (1 << l)][k + ((i + 1) * l)] += dp[i][j][k];
          }
        }
      }
    }
  }
  while (fgets(buf, 255, stdin) != NULL) {
    sscanf(buf, "%d%d", &n, &s);
    int ans = 0;
    for (int i = 0; i < (1 << 10); i++) {
      ans += dp[n][i][s];
    }

    printf("%d\\n", ans);
  }

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

bool used[10];
int sum;
int dp[11][10005];
void saiki(int c, int d) { // c?????§??§d
  dp[c][d]++;
  if (c == 10) {
    return;
  }
  for (int e = 0; e < 10; e++) {
    if (!used[e]) {
      used[e] = true;
      saiki(c + 1, d + (c + 1) * e);
      used[e] = false;
    }
  }
}
int main() {
  saiki(0, 0);
  int a, b;
  while (cin >> a >> b) {
    cout << dp[a][b] << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

bool used[10];
int sum;
int dp[11][1000];
void saiki(int c, int d) { // c?????§??§d
  dp[c][d]++;
  if (c == 10) {
    return;
  }
  for (int e = 0; e < 10; e++) {
    if (!used[e]) {
      used[e] = true;
      saiki(c + 1, d + (c + 1) * e);
      used[e] = false;
    }
  }
}
int main() {
  saiki(0, 0);
  int a, b;
  while (cin >> a >> b) {
    cout << dp[a][b] << endl;
  }
}
Problem p00070 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
using namespace std;

const int maxN = 10;
const int limitS = 330;
const int gPNT = 1 << 10;
const int IO = limitS + 1;
const int IOO = gPNT * (limitS + 1);

int tb[(maxN + 1) * (gPNT) * (limitS + 1)];

int calc(int n, int pmt, int s) {
  int ans = 0;
  if (s < 0 || s > limitS || n > maxN)
    return 0;
  if (tb[n * IOO + pmt * IO + s] >= 0) {
    return tb[n * IOO + pmt * IO + s];
  }
  if (n == 1) {
    if (s <= 9 && (!(pmt & (1 << s)))) {
      ans = 1;
    }
  } else {
    for (int i = 0; i < 10; ++i) {
      if (!((1 << i) & pmt)) {
        ans += calc(n - 1, pmt + (1 << i), s - i * n);
      }
    }
  }
  tb[n * IOO + pmt * IO + s] = ans;
  return ans;
}

int main() {
  int n, s;

  for (int i = 0; i < (maxN + 1) * (gPNT) * (limitS + 1); ++i) {
    tb[i] = -1;
  }

  while (cin >> n >> s) {
    cout << calc(n, 0, s) << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;

class DP {
public:
  DP(int n, int s) : maxN(n), maxS(s), limitS(0), pmt(1 << 10) {
    tb = new int[(maxN + 1) * (pmt) * (maxS + 1)];
    for (int i = 0; i < (maxN + 1) * (pmt) * (maxS + 1); ++i) {
      tb[i] = -1;
    }
    for (int i = 0; i < n; ++i) {
      limitS += (9 - i) * (n - i);
    }
  }
  ~DP() { delete[] tb; }

  int calc(int n, int pmt, int s) {
    int ans = 0;
    if (s < 0 || s > limitS || n > 10)
      return 0;
    if (tb[(n + 1) * (pmt) * (s + 1)] >= 0) {
      return tb[(n + 1) * (pmt) * (s + 1)];
    }
    if (n == 1) {
      if (s <= 9 && (!(pmt & (1 << s)))) {
        ans = 1;
      }
    } else {
      for (int i = 0; i < 10; ++i) {
        if (!((1 << i) & pmt)) {
          ans += calc(n - 1, pmt + (1 << i), s - i * n);
        }
        tb[(n + 1) * (pmt) * (s + 1)];
      }
    }
    return ans;
  }

private:
  int maxN, maxS, limitS, pmt;
  int *tb;
};

int main() {
  int n, s;

  while (cin >> n >> s) {
    DP *dp = new DP(n, s);
    cout << dp->calc(n, 1 << 10, s) << endl;
    delete dp;
  }

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

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) loop(i, 0, b)

template <class iter>
bool next_combination(iter f1, iter l1, iter f2, iter l2) {
  if ((f1 == l1) || (f2 == l2)) {
    return false;
  }
  iter m1 = l1;
  iter m2 = l2;
  --m2;
  while (--m1 != f1 && !(*m1 < *m2)) {
  }
  bool result = (m1 == f1) && !(*f1 < *m2);
  if (!result) {
    while (f2 != m2 && !(*m1 < *f2)) {
      ++f2;
    }
    f1 = m1;
    iter_swap(f1, f2);
    ++f1;
    ++f2;
  }
  if ((f1 != l1) && (f2 != l2)) {
    m1 = l1;
    m2 = f2;
    while ((m1 != f1) && (m2 != l2)) {
      iter_swap(--m1, m2);
      ++m2;
    }
    reverse(f1, m1);
    reverse(f1, l1);
    reverse(m2, l2);
    reverse(f2, l2);
  }
  return !result;
}

template <class iter>
bool next_combination(iter first, iter middle, iter last) {
  return next_combination(first, middle, middle, last);
}

int ns[11][1000];
int arr[10];
int n, s;
int main() {
  memset(ns, 0, sizeof(ns));
  rep(i, 10) arr[i] = i;
  loop(i, 1, 11) {
    do {
      // rep(k,i) cout<<arr[k]<<" ";
      // cout<<endl;
      do {
        int ts = 0;
        rep(j, i) ts += (j + 1) * arr[j];
        ns[i][ts]++;
      } while (next_permutation(arr, arr + i));
    } while (next_combination(arr, arr + i, arr + 10));
  }
  while (cin >> n >> s) {
    if (1 <= n && n <= 10 && 0 <= s && s <= 1000)
      cout << ns[n][s] << endl;
    else
      cout << 0 << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) loop(i, 0, b)

template <class iter>
bool next_combination(iter f1, iter l1, iter f2, iter l2) {
  if ((f1 == l1) || (f2 == l2)) {
    return false;
  }
  iter m1 = l1;
  iter m2 = l2;
  --m2;
  while (--m1 != f1 && !(*m1 < *m2)) {
  }
  bool result = (m1 == f1) && !(*f1 < *m2);
  if (!result) {
    while (f2 != m2 && !(*m1 < *f2)) {
      ++f2;
    }
    f1 = m1;
    iter_swap(f1, f2);
    ++f1;
    ++f2;
  }
  if ((f1 != l1) && (f2 != l2)) {
    m1 = l1;
    m2 = f2;
    while ((m1 != f1) && (m2 != l2)) {
      iter_swap(--m1, m2);
      ++m2;
    }
    reverse(f1, m1);
    reverse(f1, l1);
    reverse(m2, l2);
    reverse(f2, l2);
  }
  return !result;
}

template <class iter>
bool next_combination(iter first, iter middle, iter last) {
  return next_combination(first, middle, middle, last);
}

int ns[11][1000];
int arr[10];
int n, s;
int main() {
  memset(ns, 0, sizeof(ns));
  rep(i, 10) arr[i] = i;
  loop(i, 1, 10) {
    do {
      // rep(k,i) cout<<arr[k]<<" ";
      // cout<<endl;
      do {
        int ts = 0;
        rep(j, i) ts += (j + 1) * arr[j];
        ns[i][ts]++;
      } while (next_permutation(arr, arr + i));
    } while (next_combination(arr, arr + i, arr + 10));
  }
  while (cin >> n >> s) {
    cout << ns[n][s] << endl;
  }
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, s;

int dp[1 << 10][10010];

int dfs(int bit = 0, int cnt = 0, int sum = 0) {
  if (cnt == n)
    return s == sum;
  if (sum > s)
    return 0;

  int &ret = dp[bit][sum];
  if (~ret)
    return ret;
  ret = 0;

  for (int i = 0; i < 10; ++i) {
    if ((1 << i) & bit)
      continue;
    ret += dfs(bit + (1 << i), cnt + 1, sum + (cnt + 1) * i);
  }

  return ret;
}

int main() {
  while (cin >> n >> s) {
    memset(dp, -1, sizeof(dp));
    cout << dfs() << endl;
  }
}

Error: TLE

Fixed submission:

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

int n, s;

int dfs(int bit = 0, int cnt = 0, int sum = 0) {
  if (cnt == n)
    return s == sum;

  int ret = 0;

  for (int i = 0; i < 10; ++i) {
    if ((1 << i) & bit)
      continue;
    ret += dfs(bit + (1 << i), cnt + 1, sum + (cnt + 1) * i);
  }

  return ret;
}

int main() {
  while (cin >> n >> s) {
    cout << dfs() << endl;
  }
}
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

int main() {
  int n, s;
  static int dp[11][350][1 << 10] = {};
  dp[0][0][(1 << 10) - 1] = 1;

  for (int i = 1; i <= 10; i++) {
    for (int j = 0; j <= 340; j++) {
      rep(k, 10) {
        for (int l = (1 << 10) - 1; l >= 0; l--) {
          if (j - (k * i) >= 0 && (l & (1 << k)) != 0) {
            dp[i][j][(l ^ (1 << k))] += dp[i - 1][j - (k * i)][l];
          }
        }
      }
    }
  }

  while (cin >> n >> s) {
    int ans = 0;
    if (s <= 340) {
      rep(i, 1 << 10) { ans += dp[n][s][i]; }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

int main() {
  int n, s;

  while (cin >> n >> s) {

    static int dp[11][350][1 << 10] = {};
    dp[0][0][(1 << 10) - 1] = 1;

    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= s; j++) {
        rep(k, 10) {
          for (int l = (1 << 10) - 1; l >= 0; l--) {
            if (j - (k * i) >= 0 && (l & (1 << k)) != 0) {
              dp[i][j][(l ^ (1 << k))] += dp[i - 1][j - (k * i)][l];
            }
          }
        }
      }
    }

    int ans = 0;
    if (s <= 340) {
      rep(i, 1 << 10) { ans += dp[n][s][i]; }
    }
    cout << ans << endl;
  }
}
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>

using namespace std;

void calc(int s, bool *list, int n, int ans[]) {
  if (n == 1) {
    for (int i = 0; i < 10; i++) {
      if (list[i])
        ans[s + i]++;
    }
  } else {
    for (int i = 0; i < 10; i++) {
      if (list[i]) {
        list[i] = false;
        calc(s + n * i, list, n - 1, ans);
        list[i] = true;
      }
    }
  }
}

int main() {
  int n, s;
  int ans[11][386];
  bool list[10];
  memset(ans, 0, sizeof(ans));
  for (int i = 1; i <= 10; i++) {
    memset(list, 1, sizeof(list));
    calc(0, list, i, ans[i]);
  }
  while (cin >> n >> s) {
    if (n > 10 || s > 385)
      cout << 0 << endl;
    else
      cout << ans[n][s] << endl;
  }
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;

void calc(int s, bool *list, int n, int ans[285]) {
  if (n == 1) {
    for (int i = 0; i < 10; i++) {
      if (list[i])
        ans[s + i]++;
    }
  } else {
    for (int i = 0; i < 10; i++) {
      if (list[i]) {
        list[i] = false;
        calc(s + n * i, list, n - 1, ans);
        list[i] = true;
      }
    }
  }
}

int main() {
  int n, s;
  int ans[10][285];
  bool list[10];
  memset(ans, 0, sizeof(ans));
  for (int i = 1; i < 10; i++) {
    memset(list, 1, sizeof(list));
    calc(0, list, i, ans[i]);
  }
  while (cin >> n >> s) {
    cout << ans[n][s] << endl;
  }
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

using namespace std;

int n, s;
int memo[1024][1000];

int search(int stat, int sum, int dep) {
  if (dep == n)
    return memo[stat][sum] = (s == sum);
  if (~memo[stat][sum])
    return memo[stat][sum];
  //	if(sum>s)	return 0;

  int cnt = 0;
  for (int i = 0; i < 10; i++) {
    if ((stat & (1 << i)) == 0) {
      stat |= 1 << i;
      cnt += search(stat, sum + i * (n - dep), dep + 1);
      stat &= ~(1 << i);
    }
  }
  return memo[stat][sum] = cnt;
}

int main() {
  for (; ~scanf("%d%d", &n, &s);) {
    for (int i = 0; i < 1024; i++)
      for (int j = 0; j < 1000; j++)
        memo[i][j] = -1;
    if (n > 10 || s >= 1000) {
      puts("0");
      continue;
    }
    printf("%d\\n", search(0, 0, 0));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

using namespace std;

int n, s;

int search(bool *used, int sum, int dep) {
  if (dep == n - 1) {
    if (0 <= s - sum && s - sum < 10 && !used[s - sum])
      return 1;
    return 0;
  }
  if (sum > s)
    return 0;

  int cnt = 0;
  for (int i = 0; i < 10; i++) {
    if (!used[i]) {
      used[i] = true;
      cnt += search(used, sum + i * (n - dep), dep + 1);
      used[i] = false;
    }
  }
  return cnt;
}

int main() {
  for (; ~scanf("%d%d", &n, &s);) {
    if (n > 10) {
      puts("0");
      continue;
    }
    bool b[10] = {};
    printf("%d\\n", search(b, 0, 0));
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
bool used[10];
int dp[11][10000];
void solve(int s, int d, int cnt) {
  if (s < d) {
    dp[s][cnt]++;
    return;
  }

  for (int i = 0; i < 10; ++i) {
    if (!used[i]) {
      used[i] = true;
      solve(s, d + 1, cnt + i * d);
      used[i] = false;
    }
  }

  return;
}

int main() {
  for (int i = 1; i < 11; ++i) {
    fill(used, used + 10, 0);
    solve(i, 1, 0);
  }
  int n, s;
  while (cin >> n >> s) {
    cout << dp[n][s] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
bool used[10];
int dp[11][350];
void solve(int s, int d, int cnt) {
  if (s < d) {
    dp[s][cnt]++;
    return;
  }

  for (int i = 0; i < 10; ++i) {
    if (!used[i]) {
      used[i] = true;
      solve(s, d + 1, cnt + i * d);
      used[i] = false;
    }
  }

  return;
}

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

#include <iostream>
#include <string.h>

using namespace std;
int memo[11][400];

void recursive(int depth, int sum, bool flag[10]) {
  ++memo[depth][sum];
  for (int i = 0; i < 10; ++i) {
    if (flag[i]) {
      continue;
    }
    flag[i] = true;
    recursive(depth + 1, sum + (depth + 1) * i, flag);
    flag[i] = false;
  }
}

void solve() {
  bool flag[10];
  memset(flag, 0, sizeof(flag));
  recursive(0, 0, flag);
  int n, s;
  while (cin >> n >> s) {
    if (s >= 331) {
      cout << 0 << endl;
    } else {
      cout << memo[n][s] << endl;
    }
  }
}

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

Error: 0

Fixed submission:

#include <iostream>
#include <string.h>

using namespace std;
int memo[11][400];

void recursive(int depth, int sum, bool flag[10]) {
  ++memo[depth][sum];
  for (int i = 0; i < 10; ++i) {
    if (flag[i]) {
      continue;
    }
    flag[i] = true;
    recursive(depth + 1, sum + (depth + 1) * i, flag);
    flag[i] = false;
  }
}

void solve() {
  bool flag[10];
  memset(flag, 0, sizeof(flag));
  recursive(0, 0, flag);
  int n, s;
  while (cin >> n >> s) {
    cout << memo[n][s] << endl;
  }
}

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

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); ++i)

int x;
bool u[10];
int t[11][1000];

void dfs(int k) {
  if (k > 10) {
    return;
  }
  rep(i, 10) {
    if (!u[i]) {
      x += i * k;
      u[i] = true;
      ++t[k][x];
      dfs(k + 1);
      x -= i * k;
      u[i] = false;
    }
  }
}

int main() {
  dfs(1);

  int n, s;
  while (scanf("%d%d", &n, &s) != EOF) {
    printf("%d\\n", s < 1000 ? t[n][s] : 0);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); ++i)

int n, s, t;
bool used[10];

int dfs(int k) {
  if (k == 0) {
    return t == s;
  }
  int cnt = 0;
  for (int i = 9; i >= 0; --i) {
    if (!used[i]) {
      t += i * k;
      used[i] = true;
      if (t <= s) {
        cnt += dfs(k - 1);
      }
      t -= i * k;
      used[i] = false;
    }
  }
  return cnt;
}

int main() {
  while (scanf("%d%d", &n, &s) != EOF) {
    printf("%d\\n", dfs(n));
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

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

const int INF = 1e+9;

int ans[16][512];
int n, s;

int dfs(int a, int sum, int c) {
  ans[a][sum]++;
  if (a >= 10)
    return 0;

  int res = 0;
  for (int i = 0; i < 10; ++i) {
    if (!(c >> i & 1)) {
      res += dfs(a + 1, sum + (a + 1) * i, c | 1 << i);
    }
  }
  return res;
}

int main() {
  dfs(0, 0, 0);
  while (cin >> n >> s) {
    cout << (s > 510 ? 0 : ans[n][s]) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

const int INF = 1e+9;

int ans[16][500];
int n, s;

int dfs(int a, int sum, int c) {
  ans[a][sum]++;
  if (a >= 10)
    return 0;

  int res = 0;
  for (int i = 0; i < 10; ++i) {
    if (!(c >> i & 1)) {
      res += dfs(a + 1, sum + (a + 1) * i, c | 1 << i);
    }
  }
  return res;
}

int main() {
  dfs(0, 0, 0);
  while (cin >> n >> s) {
    printf("%d\\n", ans[n][s]);
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

int memo[11][350];
int N, S;
bool used[11];

int const MAX = 330;

void dfs(int cnt, int sum) {
  if (sum > MAX)
    return;
  if (cnt > 10)
    return;

  for (int i = 0; i < 10; i++) {
    if (used[i])
      continue;
    used[i] = 1;
    int next = i * cnt;
    memo[cnt][sum + next]++;
    dfs(cnt + 1, sum + next);
    used[i] = 0;
  }
}

int main() {

  fill(used, used + 10, 0);
  dfs(1, 0);
  while (cin >> N >> S) {
    if (S > MAX)
      cout << 0 << endl;
    else
      cout << memo[N][S] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

int memo[11][350];
int N, S;
bool used[10];

int const MAX = 330;

void dfs(int cnt, int sum) {
  if (sum > MAX)
    return;
  if (cnt > 10)
    return;

  for (int i = 0; i < 10; i++) {
    if (used[i])
      continue;
    used[i] = 1;
    int next = i * cnt;
    memo[cnt][sum + next]++;
    dfs(cnt + 1, sum + next);
    used[i] = 0;
  }
}

int main() {

  fill(used, used + 10, 0);
  dfs(1, 0);
  while (cin >> N >> S) {
    if (S > MAX)
      cout << 0 << endl;
    cout << memo[N][S] << endl;
  }

  return 0;
}