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

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

vector<vector<int>> ans(10 + 1, vector<int>(330 + 1, 0));

void rec(int n, int use, int sum, int depth) {
  ans[n][sum]++;
  for (int i = 0; i < 10; i++) {
    if ((use >> i) & 1)
      continue;
    rec(n + 1, use ^ (1 << i), sum + depth * i, depth + 1);
  }
  return;
}

int main() {
  int n, s;
  rec(0, 0, 0, 1);
  while (cin >> n >> s)
    cout << ans[n][s] << endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int ans[11][331] = {0};
void rec(int n, int use, int sum, int depth) {
  if (sum != 0)
    ans[n][sum]++;
  for (int i = 0; i < 10; i++) {
    if ((use >> i) & 1)
      continue;
    rec(n + 1, use ^ (1 << i), sum + depth * i, depth + 1);
  }
  return;
}

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

#include <bits/stdc++.h>
#define range(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
int n, s;
vector<int> used(10);
int memo[11][1000];
int sum;
void dfs(int d) {
  memo[d][sum]++;
  if (d == 10)
    return;

  rep(i, 10) {
    if (used[i])
      continue;

    used[i] = true;
    sum += (d + 1) * i;

    dfs(d + 1);

    used[i] = false;
    sum -= (d + 1) * i;
  }
}

int main(void) {
  dfs(0);
  while (cin >> n >> s) {
    if (s >= 1000)
      cout << 0 << endl;
    else
      cout << memo[n][s] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
int n, s;
vector<int> used(10);
int memo[11][1000];
int sum;
void dfs(int d) {
  memo[d][sum]++;
  if (d == 10)
    return;

  rep(i, 10) {
    if (used[i])
      continue;

    used[i] = true;
    sum += (d + 1) * i;

    dfs(d + 1);

    used[i] = false;
    sum -= (d + 1) * i;
  }
}

int main(void) {
  dfs(0);
  while (cin >> n >> s) {
    cout << memo[n][s] << endl;
  }

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

#include <bits/stdc++.h>
#define SIZE 300005
#define MOD 1000000007LL
#define INF 1 << 30
#define LLINF 1LL << 60
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define DOWN(i, b, a) for (int i = b; i >= a; i--)
#define SET(a, c) memset(a, c, sizeof a)
#define FORALL(i, c)                                                           \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FOREACH(i, c) for (auto(i) : (c))
#define BIT(i, j) ((i) >> (j)) & 1
#define ALL(o) (o).begin(), (o).end()
#define ERASE(o) (o).erase(unique((o).begin(), (o).end()), (o).end())
#define SQ(x) ((x) * (x))
using namespace std;
typedef long long ll;
typedef valarray<int> Array;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
typedef pair<double, double> Pdd;
template <typename T> inline void priv(vector<T> a) {
  REP(i, a.size()) { cout << a[i] << ((i == a.size() - 1) ? "\\n" : " "); }
}
ll gcd(ll a, ll b) {
  int c = max(a, b);
  int d = min(a, b);
  return c == 0 || d == 0 ? c : gcd(c % d, d);
}
ll lcm(ll a, ll b) { return a == 0 || b == 0 ? 0 : a * b / gcd(a, b); }

int a[10];
int T[11][335];

void memo(int m, int s) {
  if (m > 11)
    return;
  FOR(i, 0, 9) {
    if (a[i]) {
      a[i] = 0;
      T[m][s + m * i]++;
      memo(m + 1, s + m * i);
      a[i] = 1;
    }
  }
}

void init() {
  fill_n(a, 10, 1);
  REP(i, 11) fill_n(T[i], 335, 0);
  memo(1, 0);
}

int main() {
  init();
  int n, s;
  while (cin >> n >> s)
    cout << (s > 330 ? 0 : T[n][s]) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define SIZE 300005
#define MOD 1000000007LL
#define INF 1 << 30
#define LLINF 1LL << 60
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define DOWN(i, b, a) for (int i = b; i >= a; i--)
#define SET(a, c) memset(a, c, sizeof a)
#define FORALL(i, c)                                                           \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FOREACH(i, c) for (auto(i) : (c))
#define BIT(i, j) ((i) >> (j)) & 1
#define ALL(o) (o).begin(), (o).end()
#define ERASE(o) (o).erase(unique((o).begin(), (o).end()), (o).end())
#define SQ(x) ((x) * (x))
using namespace std;
typedef long long ll;
typedef valarray<int> Array;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
typedef pair<double, double> Pdd;
template <typename T> inline void priv(vector<T> a) {
  REP(i, a.size()) { cout << a[i] << ((i == a.size() - 1) ? "\\n" : " "); }
}
ll gcd(ll a, ll b) {
  int c = max(a, b);
  int d = min(a, b);
  return c == 0 || d == 0 ? c : gcd(c % d, d);
}
ll lcm(ll a, ll b) { return a == 0 || b == 0 ? 0 : a * b / gcd(a, b); }

int a[10];
int T[11][335];

void memo(int m, int s) {
  if (m > 11)
    return;
  FOR(i, 0, 9) {
    if (a[i]) {
      a[i] = 0;
      T[m][s + m * i]++;
      memo(m + 1, s + m * i);
      a[i] = 1;
    }
  }
}

void init() {
  fill_n(a, 10, 1);
  REP(i, 11) fill_n(T[i], 335, 0);
  memo(1, 0);
}

int main() {
  init();
  int n, s;
  while (cin >> n >> s)
    cout << T[n][s] << endl;
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

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

  int n, s;
  while (cin >> n >> s) {
    cout << table[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};

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

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

  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 <cstring>
#include <iostream>
using namespace std;

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

int rec(int sum, int cnt, int bit) {
  if (sum < 0)
    return 0;
  if (cnt == n)
    return sum == 0;
  if (dp[sum][cnt][bit] != -1)
    return dp[sum][cnt][bit];
  int res = 0;
  for (int i = 0; i <= 9; i++) {
    if (bit >> i & 1)
      res += rec(sum - i * (cnt + 1), cnt + 1, bit & ~(1 << i));
  }
  return dp[sum][cnt][bit] = res;
}

int main() {
  while (cin >> n >> s) {
    memset(dp, -1, sizeof(dp));
    int ans = 0;
    if (s > 330)
      s = 0;
    cout << rec(s, 0, (1 << 10) - 1) << endl;
  }
}

Error: TLE

Fixed submission:

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

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

int rec(int sum, int cnt, int bit) {
  if (sum < 0)
    return 0;
  if (cnt == n)
    return sum == 0;
  if (dp[sum][cnt][bit])
    return dp[sum][cnt][bit];
  int res = 0;
  for (int i = 0; i <= 9; i++) {
    if (bit >> i & 1)
      res += rec(sum - i * (cnt + 1), cnt + 1, bit & ~(1 << i));
  }
  return dp[sum][cnt][bit] = res;
}

int main() {
  while (cin >> n >> s) {
    memset(dp, 0, sizeof(dp));
    int ans = 0;
    if (s > 330)
      s = 0;
    cout << rec(s, 0, (1 << 10) - 1) << endl;
  }
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define MAX 10
#define S_MAX 330

using namespace std;

int gn;
int ganses[MAX + 1][S_MAX + 1];
int gp[MAX];
int gnums[MAX];
int qq[MAX + 1][MAX + 1];

/* genperm1 from http://www5.airnet.ne.jp/tomy/cpro/pe1.htm (C) Tomy */
void genperm1(int p[], int n) {
  int c[MAX], *pc, *q;
  int k, t;

  for (k = 1, q = p, pc = c; k <= n;)
    *q++ = *pc++ = k++;
  k = 1;
  pc = c;
  do {
    int tmp = 0;

    t = *(p + k);
    *(p + k) = *(q = p + ((k & 1) ? *pc : 0));
    *q = t;
    for (int i = 0; i < n; i++)
      tmp += qq[i + 1][gnums[p[i] - 1]];
    ganses[n][tmp]++;
    k = 1;
    pc = c;
    while (*pc == 0)
      *pc++ = k++;
    (*pc)--;
  } while (k < n);
}

void comb(int n, int k) {
  bool loop = false;

  for (int i = 0; i < k; i++)
    gnums[i] = i;

  do {
    loop = false;

    // TODO
    genperm1(gp, k);

    for (int pk = k - 1; pk >= 0; pk--) {
      gnums[pk]++;
      if (gnums[pk] <= n - (k - pk)) {
        for (int j = pk + 1; j < k; j++)
          gnums[j] = gnums[j - 1] + 1;
        pk++;
        loop = true;
        break;
      }
    }
  } while (loop);
}

int main() {
  for (int i = 0; i < sizeof(qq) / sizeof(qq[0]); i++) {
    for (int j = 0; j < sizeof(qq[0]) / sizeof(qq[0][0]); j++) {
      qq[i][j] = i * j;
    }
  }
  for (int i = 1; i <= MAX; i++) {
    comb(MAX, i);
  }
  while (1) {
    int n, s, ans = 0;

    cin >> n >> s;
    if (cin.eof())
      break;

    if (s >= 0 && s <= S_MAX)
      ans = ganses[n][s];

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define MAX 10
#define S_MAX 330

using namespace std;

int ans;
int s;
int gp[MAX];
int gnums[MAX];
int qq[MAX + 1][MAX + 1];

/* genperm1 from http://www5.airnet.ne.jp/tomy/cpro/pe1.htm (C) Tomy */
void genperm1(int p[], int n) {
  int c[MAX], *pc, *q;
  int k, t;

  for (k = 1, q = p, pc = c; k <= n;)
    *q++ = *pc++ = k++;
  k = 1;
  pc = c;
  do {
    int tmp = 0;

    t = *(p + k);
    *(p + k) = *(q = p + ((k & 1) ? *pc : 0));
    *q = t;
    for (int i = 0; i < n; i++) {
      tmp += qq[i + 1][gnums[p[i] - 1]];
      if (tmp > s)
        break;
    }
    if (tmp == s)
      ans++;
    k = 1;
    pc = c;
    while (*pc == 0)
      *pc++ = k++;
    (*pc)--;
  } while (k < n);
}

void comb(int n, int k) {
  bool loop = false;

  for (int i = 0; i < k; i++)
    gnums[i] = i;

  do {
    loop = false;

    // TODO
    genperm1(gp, k);

    for (int pk = k - 1; pk >= 0; pk--) {
      gnums[pk]++;
      if (gnums[pk] <= n - (k - pk)) {
        for (int j = pk + 1; j < k; j++) {
          gnums[j] = gnums[j - 1] + 1;
        }
        pk++;
        loop = true;
        break;
      }
    }
  } while (loop);
}

int main(int c, char *argv[]) {
  for (int i = 0; i < sizeof(qq) / sizeof(qq[0]); i++) {
    for (int j = 0; j < sizeof(qq[0]) / sizeof(qq[0][0]); j++) {
      qq[i][j] = i * j;
    }
  }
  while (1) {
    int n;

    cin >> n >> s;
    if (cin.eof())
      break;

    ans = 0;
    if (s >= 0 && s <= S_MAX)
      comb(10, n);

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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int dp[11][331][1 << 10];
int main() {
  memset(dp, 0, sizeof(dp));
  dp[0][0][0] = 1;
  for (int multiply = 1; multiply <= 10; multiply++) {
    for (int used = 0; used <= (1 << 10) - 1; used++) {
      for (int base = 330; base >= 0; base--) {
        if (dp[multiply - 1][base][used] == 0)
          continue;
        for (int to = 0; to <= 9; to++) {
          if (used & (1 << to))
            continue;
          if (to * multiply + base > 330)
            continue;

          dp[multiply][to * multiply + base][used | (1 << to)] +=
              dp[multiply - 1][base][used];
        }
      }
    }
  }

  int n, target_sum;
  while (~scanf("%d %d", &n, &target_sum)) {
    if (target_sum > 330 || n >= 11) {
      printf("0\\n");
    } else {
      int res = 0;
      for (int S = 0; S <= (1 << 10) - 1; S++) {
        res += dp[n][target_sum][S];
      }
      printf("%d\\n", res);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int dp[11][331][(1 << 11)];
int main() {
  memset(dp, 0, sizeof(dp));
  dp[0][0][0] = 1;
  for (int multiply = 1; multiply <= 9; multiply++) {
    for (int used = 0; used <= (1 << 10) - 1; used++) {
      for (int base = 330; base >= 0; base--) {

        if (dp[multiply - 1][base][used] == 0)
          continue;

        for (int to = 9; to >= 0; to--) {
          if (used & (1 << to))
            continue;
          if (to * multiply + base >= 330)
            continue;

          dp[multiply][to * multiply + base][used | (1 << to)] +=
              dp[multiply - 1][base][used];
        }
      }
    }
  }

  int n, target_sum;
  while (~scanf("%d %d", &n, &target_sum)) {
    int res = 0;
    for (int S = 0; S <= (1 << 10) - 1; S++) {
      res += dp[n][target_sum][S];
    }
    printf("%d\\n", res);
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

template <unsigned T> bool nextBitset(bitset<T> &bs, int digit) {
  if (bs.none())
    return false;
  bitset<T> x, y, z;
  x = bs.to_ulong() & (~(bs.to_ulong()) + 1ULL);
  y = bs.to_ulong() + x.to_ulong() + 0ULL;
  z = bs & ~y;
  if (bs[digit - 1] && bs == z)
    return false;
  bs = ((z.to_ulong() / x.to_ulong()) >> 1) + 0ULL;
  bs |= y;
  return true;
}

int main() {
  vector<vector<int>> dp(1 << 10, vector<int>(331, 0));
  dp[0][0] = 1;
  for (int i = 0; i < (1 << 10); ++i) {
    bitset<10> bs(i);
    for (int j = 0; j < 10; ++j) {
      if (bs[j])
        continue;
      bitset<10> bs1 = bs;
      bs1[j] = true;
      int a = j * bs1.count();
      for (int k = 0; k + a < 331; ++k)
        dp[bs1.to_ulong()][k + a] += dp[bs.to_ulong()][k];
    }
  }

  for (;;) {
    int n, s;
    if (!(cin >> n >> s))
      return 0;

    if (n > 10 || s > 330) {
      cout << 0 << endl;
      continue;
    }

    int ret = 0;
    bitset<10> bs((1 << n) - 1);
    do {
      ret += dp[bs.to_ulong()][s];
    } while (nextBitset(bs, 10));
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int solve(int x, int n, int s, bitset<10> &check) {
  if (x == n) {
    if (s == 0)
      return 1;
    else
      return 0;
  }

  int ret = 0;
  for (int i = 0; i < 10; ++i) {
    if (check[i])
      continue;
    check[i] = true;
    ret += solve(x + 1, n, s - i * (x + 1), check);
    check[i] = false;
  }
  return ret;
}

int main() {
  for (;;) {
    int n, s;
    if (!(cin >> n >> s))
      return 0;

    bitset<10> check;
    cout << solve(0, n, s, check) << endl;
  }
}
Problem p00070 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int a[10][331] = {0};
  int p[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  int q[10] = {362880, 40320, 5040, 720, 120, 24, 6, 2, 1, 1};
  int s, n;
  do {
    s = 0;
    for (int i = 0; i < 10; ++i) {
      s += (i + 1) * p[i];
      a[i][s]++;
    }
  } while (next_permutation(p, p + 10));
  while (cin >> n >> s) {
    if (s > 330)
      cout << 0;
    else
      cout << a[n - 1][s] / q[n - 1];
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int a[10][331] = {0};
  int p[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  int q[10] = {362880, 40320, 5040, 720, 120, 24, 6, 2, 1};
  int s, n;
  do {
    s = 0;
    for (int i = 0; i < 10; ++i) {
      s += (i + 1) * p[i];
      a[i][s]++;
    }
  } while (next_permutation(p, p + 10));
  while (cin >> n >> s) {
    if (s > 330)
      cout << 0;
    else
      cout << a[n - 1][s] / q[n - 1];
    cout << endl;
  }
  return 0;
}
Problem p00070 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int n, s;
int cnt = 0;

map<int, int> dp[1 << 10];

map<int, int> dfs(int d, int si) {
  int sz = 0;
  for (int i = 0; i < n; i++) {
    if ((si >> i) & 1) {
      sz++;
    }
  }

  if (dp[si].size() != 0) {
    return dp[si];
  }
  map<int, int> ret;
  if (d == n) {
    return ret;
  }
  for (int i = 0; i < 10; i++) {
    if ((si >> i) & 1 == 1) {
      map<int, int> ss = dfs(d + 1, (si & ~(1 << i)));
      if (ss.size() == 0) {
        ret[((d + 1) * i)]++;
      } else {
        if (d == 0) {
          // cout<<endl;
        }
        for (map<int, int>::iterator it = ss.begin(); it != ss.end(); it++) {
          if ((it->first) + (d + 1) * i <= s)
            ret[(it->first) + (d + 1) * i] += it->second;
        }
      }
    }
  }
  dp[si] = ret;
  return dp[si];
}

// bool used[10];
// void dfs2(int d,int sum){
//	if(d==n){
//		if(sum==s)
//			cnt++;
//		return;
//	}
//	for(int i = 0; i < 10; i++){
//		if(!used[i]){
//			used[i]=true;
//			dfs2(d+1,sum+(d+1)*i);
//			used[i]=false;
//		}
//	}
// }

int main() {

  while (cin >> n >> s) {
    //	fill(used,used+10,false);
    cnt = 0;
    map<int, int> m = dfs(0, (1 << 10) - 1);
    int minNum = 0;
    for (int i = n - 1; i >= 0; i--) {
      minNum += (n - 1 - i) * (n - (n - 1 - i));
    }
    if (minNum > s)
      cout << 0 << endl;
    else
      cout << m[s] << endl;
    for (int j = 0; j < (1 << 10); j++) {
      dp[j].clear();
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int n, s;
int cnt = 0;

int vvv[10];
int size;

map<int, int> dp[1 << 10];

map<int, int> dfs(int d, int si) {
  int sz = 0;
  for (int i = 0; i < n; i++) {
    if ((si >> i) & 1) {
      sz++;
    }
  }

  if (dp[si].size() != 0) {
    return dp[si];
  }
  map<int, int> ret;
  if (d == n)
    return ret;
  for (int i = 0; i < 10; i++) {
    if ((si >> i) & 1 == 1) {
      map<int, int> ss = dfs(d + 1, (si & ~(1 << i)));
      if (ss.size() == 0) {
        ret[((d + 1) * i)]++;
      } else {
        for (map<int, int>::iterator it = ss.begin(); it != ss.end(); it++) {
          ret[(it->first) + (d + 1) * i] += it->second;
        }
      }
    }
  }
  dp[si] = ret;
  return dp[si];
}

bool used[10];
void dfs2(int d, int sum) {
  if (d == n) {
    if (sum == s)
      cnt++;
    return;
  }
  for (int i = 0; i < 10; i++) {
    if (!used[i]) {
      used[i] = true;
      dfs2(d + 1, sum + (d + 1) * i);
      used[i] = false;
    }
  }
}

int main() {

  while (cin >> n >> s) {
    fill(used, used + 10, false);
    size = 0;
    cnt = 0;
    map<int, int> m = dfs(0, (1 << 10) - 1);
    cout << m[s] << endl;
    for (int j = 0; j < (1 << 10); j++) {
      dp[j].clear();
    }
  }

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

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

int dp[(1 << 10) + 10][1010];

void solve() {
  dp[0][0] = 1;
  for (int i = 0; i < (1 << 10); i++) {
    int m = __builtin_popcount(i) + 1;
    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;
        int nex = (i | (1 << k));
        int nexv = j + k * m;
        dp[nex][nexv] += dp[i][j];
      }
    }
  }
}

bool isValid(int n, int s) {
  return (0 <= n && n <= 10) && (0 <= s && s <= 1000);
}

int main() {
  solve();
  int n, s;
  while (cin >> n >> s) {
    if (!isValid(n, s)) {
      cout << 0 << endl;
      continue;
    }

    int ans = 0;
    for (int i = 0; i < (1 << 10); i++) {
      int m = __builtin_popcount(i);
      if (n == m)
        ans += dp[i][s];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int dp[(1 << 10) + 1][1001];

void solve() {
  dp[0][0] = 1;
  for (int i = 0; i < (1 << 10); i++) {
    int m = __builtin_popcount(i) + 1;
    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;
        int nex = (i | (1 << k));
        int nexv = j + k * m;
        dp[nex][nexv] += dp[i][j];
      }
    }
  }
}

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

#include <iostream>

void solve(const int &sum, const int &s, int limit, long long int &cnt,
           const int &used) {

  int tmp;
  int est_sum;

  for (int i = 0; i < 10; ++i) {
    if ((used & (1 << i)) == 0) {
      tmp = i * limit + sum;
      if (limit > 1) {
        est_sum = 9 * (limit - 1) * (limit - 1);
        if (est_sum + tmp >= s) {
          solve(tmp, s, limit - 1, cnt, used | (1 << i));
        }
      } else {
        if (tmp == s) {
          ++cnt;
          return;
        } else if (tmp > s) {
          return;
        }
      }
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int s, n;

  while (std::cin >> n >> s) {
    long long int cnt = 0;
    solve(0, s, n, cnt, 0);
    std::cout << cnt << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

void solve(const int &sum, const int &s, int limit, long long int &cnt,
           const int &used) {

  int tmp;
  int est_sum;

  for (int i = 0; i < 10; ++i) {
    if ((used & (1 << i)) == 0) {
      tmp = i * limit + sum;
      if (limit > 1) {
        est_sum = 9 * (limit - 1) * (limit - 1);
        if (est_sum + tmp >= s) {
          solve(tmp, s, limit - 1, cnt, used | (1 << i));
        }
      } else {
        if (tmp == s) {
          ++cnt;
          return;
        }
      }
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int s, n;

  while (std::cin >> n >> s) {
    long long int cnt = 0;
    solve(0, s, n, cnt, 0);
    std::cout << cnt << std::endl;
  }

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

#include <algorithm>
#include <stdio.h>
int main() {
  int m[10][331] = {0};
  int n, s, i, a[10], f[11] = {1};
  for (i = 0; i < 10; ++i)
    a[i] = i;
  for (i = 1; i < 10; ++i)
    f[i] = f[i - 1] * i;
  do {
    for (s = i = 0; i < 10; ++i) {
      s += (i + 1) * a[i];
      ++m[i][s];
    }
  } while (std::next_permutation(a, a + 10));
  while (scanf("%d%d", &n, &s) > 0)
    if (s > 330)
      puts("0");
    else
      printf("%d\\n", m[n - 1][s] / f[10 - n]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
int main() {
  int m[10][331] = {0};
  int n, s, i, a[10], f[11] = {1};
  for (i = 0; i < 10; ++i)
    a[i] = i;
  for (i = 1; i < 10; ++i)
    f[i] = f[i - 1] * i;
  do {
    for (s = i = 0; i < 10; ++i) {
      s += (i + 1) * a[i];
      ++m[i][s];
    }
  } while (std::next_permutation(a, a + 10));
  while (scanf("%d%d", &n, &s) > 0)
    printf("%d\\n", m[n - 1][s] / f[10 - n]);
  return 0;
}