prithivMLmods/Coder-Stat huggingface dataset:
Problem p00070 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using namespace std;

int ans[11][410];

void dfs(int dpt, int sum, int bit) {
  ++ans[dpt][sum];
  for (int i = 0; i < 10; ++i) {
    if (bit >> i & 1) {
      dfs(dpt + 1, sum + i * (dpt + 1), bit ^ (1 << i));
    }
  }
}

int main() {
  dfs(0, 0, 0x3ff);
  int n, s;
  while (scanf("%d%d", &n, &s) != EOF) {
    int x = 0;
    x = ans[n][s];
    if (n <= 10 && s <= 400) {
      x = ans[n][s];
    }
    printf("%d\n", x);
  }
}

Fixed submission:
#include <cstdio>
using namespace std;

int ans[11][410];

void dfs(int dpt, int sum, int bit) {
  ++ans[dpt][sum];
  for (int i = 0; i < 10; ++i) {
    if (bit >> i & 1) {
      dfs(dpt + 1, sum + i * (dpt + 1), bit ^ (1 << i));
    }
  }
}

int main() {
  dfs(0, 0, 0x3ff);
  int n, s;
  while (scanf("%d%d", &n, &s) != EOF) {
    int x = 0;
    if (n <= 10 && s <= 400) {
      x = ans[n][s];
    }
    printf("%d\n", x);
  }
}
Problem p00070 (C++)

Buggy submission (status: 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) {
    cout << memo[n][s] << endl;
  }

  return 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) {
    if (s >= 1000)
      cout << 0 << endl;
    else
      cout << memo[n][s] << endl;
  }

  return 0;
}
Problem p00070 (C++)

Buggy submission (status: 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 <= 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;
}

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 < 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;
}
Problem p00070 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p00070 (C++)

Buggy submission (status: 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 << T[n][s] << endl;
  return 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 << (s > 330 ? 0 : T[n][s]) << endl;
  return 0;
}
Problem p00070 (C++)

Buggy submission (status: 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;
        }
      }
    }
  }
}

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;
}

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;
        } 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;
}
Problem p00070 (C++)

Buggy submission (status: 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) {
  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;
}

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) {
  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;
}
Problem p00070 (C++)

Buggy submission (status: 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 > 10) {
      cout << 0 << endl;
      continue;
    }
    int ans = 0;
    REP(i, P) ans += dp[n][s][i];
    cout << ans << endl;
  }
  return 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 >= N || s >= S) {
      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 (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p00070 (C++)

Buggy submission (status: 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) {
    cout << memo[n][s] << endl;
  }
}

int main() {
  solve();
  return (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) {
    if (s >= 331) {
      cout << 0 << endl;
    } else {
      cout << memo[n][s] << endl;
    }
  }
}

int main() {
  solve();
  return (0);
}
Problem p00070 (C++)

Buggy submission (status: 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};
  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;
}

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, 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 (status: 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)
    cout << dp[n][s] / fac[10 - n] << endl;
  return 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)
    if (n < 0 || 10 < n || s < 0 || 330 < s)
      cout << "0" << endl;
    else
      cout << dp[n][s] / fac[10 - n] << endl;
  return 0;
}
Problem p00070 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

template <typename Iterator>
inline bool next_combination(const Iterator first, Iterator k,
                             const Iterator last) {
  if ((first == last) || (first == k) || (last == k))
    return false;
  Iterator itr1 = first;
  Iterator itr2 = last;
  ++itr1;
  if (last == itr1)
    return false;
  itr1 = last;
  --itr1;
  itr1 = k;
  --itr2;
  while (first != itr1) {
    if (*--itr1 < *itr2) {
      Iterator j = k;
      while (!(*itr1 < *j))
        ++j;
      std::iter_swap(itr1, j);
      ++itr1;
      ++j;
      itr2 = k;
      std::rotate(itr1, j, last);
      while (last != j) {
        ++j;
        ++itr2;
      }
      std::rotate(k, itr2, last);
      return true;
    }
  }
  std::rotate(first, k, last);
  return false;
}

int main() {
  vector<int> v;
  for (int i = 0; i < 10; i++)
    v.push_back(i);

  int n, s;
  while (cin >> n >> s) {
    int ret = 0;
    do {
      int sum = 0;
      for (int j = 0; j < n; j++)
        sum += v[j] * (j + 1);
      if (sum < s)
        continue;
      vector<int> vv;
      for (int i = 0; i < n; i++)
        vv.push_back(v[i]);
      do {
        int sum = 0;
        for (int j = 0; j < n; j++)
          sum += vv[j] * (j + 1);
        if (sum == s)
          ret++;
      } while (next_permutation(vv.begin(), vv.end()));
    } while (next_combination(v.begin(), v.begin() + n, v.end()));
    cout << ret << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

template <typename Iterator>
inline bool next_combination(const Iterator first, Iterator k,
                             const Iterator last) {
  if ((first == last) || (first == k) || (last == k))
    return false;
  Iterator itr1 = first;
  Iterator itr2 = last;
  ++itr1;
  if (last == itr1)
    return false;
  itr1 = last;
  --itr1;
  itr1 = k;
  --itr2;
  while (first != itr1) {
    if (*--itr1 < *itr2) {
      Iterator j = k;
      while (!(*itr1 < *j))
        ++j;
      std::iter_swap(itr1, j);
      ++itr1;
      ++j;
      itr2 = k;
      std::rotate(itr1, j, last);
      while (last != j) {
        ++j;
        ++itr2;
      }
      std::rotate(k, itr2, last);
      return true;
    }
  }
  std::rotate(first, k, last);
  return false;
}

int main() {
  vector<int> v;
  for (int i = 0; i < 10; i++)
    v.push_back(i);

  int n, s;
  while (cin >> n >> s) {
    int ret = 0;
    do {
      int sum = 0;
      for (int j = 0; j < n; j++)
        sum += v[j] * (j + 1);
      if (sum < s)
        continue;
      else if (sum == s) {
        ret++;
        continue;
      }
      vector<int> vv;
      for (int i = 0; i < n; i++)
        vv.push_back(v[i]);
      do {
        int sum = 0;
        for (int j = 0; j < n; j++)
          sum += vv[j] * (j + 1);
        if (sum == s)
          ret++;
      } while (next_permutation(vv.begin(), vv.end()));
    } while (next_combination(v.begin(), v.begin() + n, v.end()));
    cout << ret << endl;
  }
}
Problem p00070 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iostream>
using namespace std;
int dp[11][1024][341];
int main() {
  dp[0][0][0] = 1;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 1024; j++) {
      for (int k = 0; k < 331; k++) {
        if (dp[i][j][k] == 0)
          continue;
        for (int l = 0; l < 10; l++) {
          if (!((j >> l) & 1)) {
            dp[i + 1][j | (1 << l)][(i + 1) * l + k] += dp[i][j][k];
          }
        }
      }
    }
  }
  int n, s;
  while (cin >> n >> s) {
    int sum = 0;
    for (int i = 0; i < 1024; i++) {
      sum += dp[n][i][s];
    }
    cout << sum << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <iostream>
using namespace std;
int dp[11][1024][341];
int main() {
  dp[0][0][0] = 1;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 1024; j++) {
      for (int k = 0; k < 331; k++) {
        if (dp[i][j][k] == 0)
          continue;
        for (int l = 0; l < 10; l++) {
          if (!((j >> l) & 1)) {
            dp[i + 1][j | (1 << l)][(i + 1) * l + k] += dp[i][j][k];
          }
        }
      }
    }
  }
  int n, s;
  while (cin >> n >> s) {
    int sum = 0;
    if (n > 10 || s > 330) {
      cout << 0 << endl;
      continue;
    }
    for (int i = 0; i < 1024; i++) {
      sum += dp[n][i][s];
    }
    cout << sum << endl;
  }
}
Problem p00070 (C++)

Buggy submission (status: Runtime Error):
// AOJ 0070 Combination of Number Sequences

#include <iostream>

using namespace std;

void set_count(int n, int sum, int used);

const int MAX_N = 10;
const int MAX_SUM = 330;

int count_s[MAX_N + 1][MAX_SUM + 1];

int main() {
  int n, s;

  set_count(1, 0, 0);

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

    cout << count_s[n][s] << endl;
  }

  return 0;
}

void set_count(int depth, int sum, int used) {
  int new_sum;
  int i, used_i;

  for (i = 0; i < 10; i++) {
    used_i = 1 << i;
    if (used & used_i)
      continue;

    new_sum = sum + depth * i;
    count_s[depth][new_sum] += 1;

    if (depth < MAX_N)
      set_count(depth + 1, new_sum, used | used_i);
  }
}

Fixed submission:
// AOJ 0070 Combination of Number Sequences

#include <iostream>

using namespace std;

void set_count(int n, int sum, int used);

const int MAX_N = 10;
const int MAX_SUM = 330;

int count_s[MAX_N + 1][MAX_SUM + 1];

int main() {
  int n, s;

  set_count(1, 0, 0);

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

    if (1 <= n && n <= 10 && 0 <= s && s <= 330)
      cout << count_s[n][s] << endl;
    else
      cout << 0 << endl;
  }

  return 0;
}

void set_count(int depth, int sum, int used) {
  int new_sum;
  int i, used_i;

  for (i = 0; i < 10; i++) {
    used_i = 1 << i;
    if (used & used_i)
      continue;

    new_sum = sum + depth * i;
    count_s[depth][new_sum] += 1;

    if (depth < MAX_N)
      set_count(depth + 1, new_sum, used | used_i);
  }
}
Problem p00070 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>
int a[10][331];
bool e[10];
void m(int d, int s) {
  for (int i = 0; i < 10; i++) {
    if (e[i])
      continue;
    a[d][s + d * i]++;
    if (d < 10) {
      e[i] = true;
      m(d + 1, s + d * i);
      e[i] = false;
    }
  }
}
int main(void) {
  memset(a, 0, sizeof(a));
  memset(e, 0, sizeof(e));
  m(1, 0);
  int n, s;
  while (~scanf("%d%d", &n, &s)) {
    if (n < 0 || n > 10 || s < 0 || s > 330)
      puts("0");
    else
      printf("%d\n", a[n][s]);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
#include <string.h>
int a[11][331];
bool e[10];
void m(int d, int s) {
  for (int i = 0; i < 10; i++) {
    if (e[i])
      continue;
    a[d][s + d * i]++;
    if (d < 10) {
      e[i] = true;
      m(d + 1, s + d * i);
      e[i] = false;
    }
  }
}
int main(void) {
  memset(a, 0, sizeof(a));
  memset(e, 0, sizeof(e));
  m(1, 0);
  int n, s;
  while (~scanf("%d%d", &n, &s)) {
    if (n < 0 || n > 10 || s < 0 || s > 330)
      puts("0");
    else
      printf("%d\n", a[n][s]);
  }
  return 0;
}
Problem p00070 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int dp[1024][500];

int main() {
  int n, s;
  dp[0][0] = 1;
  for (int i = 0; i < 350; i++) {
    for (int j = 0; j < 1024; j++) {
      int cnt = 1;
      for (int k = 0; k < 10; k++)
        if (j & (1 << k))
          cnt++;
      for (int k = 0; k < 10; k++) {
        if (j & (1 << k))
          continue;
        dp[j | (1 << k)][i + cnt * k] += dp[j][i];
      }
    }
  }
  while (cin >> n >> s) {
    int res = 0;
    for (int i = 0; i < 1024; i++) {
      int c = 0;
      for (int j = 0; j < 10; j++)
        if (i & (1 << j))
          c++;
      if (c == n)
        res += dp[i][s];
    }
    cout << res << endl;
  }
}

Fixed submission:
#include <iostream>

using namespace std;

int dp[1024][500];

int main() {
  int n, s;
  dp[0][0] = 1;
  for (int i = 0; i < 350; i++) {
    for (int j = 0; j < 1024; j++) {
      int cnt = 1;
      for (int k = 0; k < 10; k++)
        if (j & (1 << k))
          cnt++;
      for (int k = 0; k < 10; k++) {
        if (j & (1 << k))
          continue;
        dp[j | (1 << k)][i + cnt * k] += dp[j][i];
      }
    }
  }
  while (cin >> n >> s) {
    int res = 0;
    for (int i = 0; i < 1024; i++) {
      if (s > 400)
        continue;
      int c = 0;
      for (int j = 0; j < 10; j++)
        if (i & (1 << j))
          c++;
      if (c == n)
        res += dp[i][s];
    }
    cout << res << endl;
  }
}
Problem p00070 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>

using namespace std;

int table[10 + 1][330 + 1];
int memo[10];

void numnum(int size, int depth, int sum) {
  if (size + 1 == depth) {
    table[size][sum]++;
  } else {
    for (int i = 0; i <= 9; ++i) {
      if (memo[i] == 0) {
        memo[i] = 1;
        numnum(size, depth + 1, sum + i * depth);
        memo[i] = 0;
      } // end if
    }   // end for
  }     // end if
}

int main() {
  memset(table, 0, sizeof(table));

  for (int i = 1; i <= 10; ++i) {
    memset(memo, 0, sizeof(memo));
    numnum(i, 1, 0);
  } // end for

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

  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>

using namespace std;

int table[10 + 1][10000];
int memo[10];

void numnum(int size, int depth, int sum) {
  if (size + 1 == depth) {
    table[size][sum]++;
  } else {
    for (int i = 0; i <= 9; ++i) {
      if (memo[i] == 0) {
        memo[i] = 1;
        numnum(size, depth + 1, sum + i * depth);
        memo[i] = 0;
      } // end if
    }   // end for
  }     // end if
}

int main() {
  memset(table, 0, sizeof(table));

  for (int i = 1; i <= 10; ++i) {
    memset(memo, 0, sizeof(memo));
    numnum(i, 1, 0);
  } // end for

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

  return 0;
}
Problem p00070 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>

using namespace std;

int table[10 + 1][330 + 2];
int memo[10];

void numnum(int size, int depth, int sum) {
  if (size + 1 == depth) {
    table[size][sum]++;
  } else {
    for (int i = 0; i <= 9; ++i) {
      if (memo[i] == 0) {
        memo[i] = 1;
        numnum(size, depth + 1, sum + i * depth);
        memo[i] = 0;
      } // end if
    }   // end for
  }     // end if
}

int main() {
  memset(table, 0, sizeof(table));

  for (int i = 1; i <= 10; ++i) {
    memset(memo, 0, sizeof(memo));
    numnum(i, 1, 0);
  } // end for

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

  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>

using namespace std;

// http://d.hatena.ne.jp/Nekonyan/20110508
// Nekonyanツつウツづアツづ個コツーツドツづーツ参ツ考ツづ可つオツづ慊つオツつスツ。

int table[10 + 1][10000];
int memo[10];

void numnum(int size, int depth, int sum) {
  if (size + 1 == depth) {
    table[size][sum]++;
  } else {
    for (int i = 0; i <= 9; ++i) {
      if (memo[i] == 0) {
        memo[i] = 1;
        numnum(size, depth + 1, sum + i * depth);
        memo[i] = 0;
      } // end if
    }   // end for
  }     // end if
}

int main() {
  memset(table, 0, sizeof(table));

  for (int i = 1; i <= 10; ++i) {
    memset(memo, 0, sizeof(memo));
    numnum(i, 1, 0);
  } // end for

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

  return 0;
}
Problem p00070 (C++)

Buggy submission (status: 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)
    printf("%d\n", m[n - 1][s] / f[10 - n]);
  return 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)
    if (s > 330)
      puts("0");
    else
      printf("%d\n", m[n - 1][s] / f[10 - n]);
  return 0;
}
Problem p00070 (C++)

Buggy submission (status: 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;
    for (int i = 0; i < (1 << 10); i++) {
      ans += dp[n][i][s];
    }

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

  return (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;
    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);
}
Problem p00070 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int main() {
  const int N = 10;

  int dp[1 << N][331] = {};
  int res = 0;
  REP(i, N) { dp[1 << i][i] = 1; }
  REP(S, 1 << N) {
    vector<int> se;
    REP(j, N)
    if ((S >> j) & 1) {
      se.push_back(j);
      // cout << j << " ";
    }
    FOR(it, se) {
      REP(i, 331) {
        if (i + (*it) * se.size() >= 331)
          break;
        dp[S][i + (*it) * se.size()] += dp[S - (1 << *it)][i];
      }
    }
    // REP(i,331) {
    //   if (dp[S][i])cout << i << " ";
    // }cout << endl;
  }
  int table[11][331] = {};
  for (int i = 1; i <= N; ++i) {
    int comb = (1 << i) - 1;
    while (comb < 1 << 10) {
      REP(j, 331) { table[i][j] += dp[comb][j]; }

      int x = comb & -comb, y = comb + x;
      comb = ((comb & ~y) / x >> 1) | y;
    }
    // if (i == 3) {
    //   REP(j,331) {
    //     cout << table[i][j] << endl;
    //   }
    // }
    // cout << table[i][10] << endl;
  }

  int n, s;
  while (cin >> n >> s) {
    if (n < 0 || n > 10 || s < 0 || s >= 331)
      cout << 0 << endl;
    cout << table[n][s] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int main() {
  const int N = 10;

  int dp[1 << N][331] = {};
  int res = 0;
  REP(i, N) { dp[1 << i][i] = 1; }
  REP(S, 1 << N) {
    vector<int> se;
    REP(j, N)
    if ((S >> j) & 1) {
      se.push_back(j);
      // cout << j << " ";
    }
    FOR(it, se) {
      REP(i, 331) {
        if (i + (*it) * se.size() >= 331)
          break;
        dp[S][i + (*it) * se.size()] += dp[S - (1 << *it)][i];
      }
    }
    // REP(i,331) {
    //   if (dp[S][i])cout << i << " ";
    // }cout << endl;
  }
  int table[11][331] = {};
  for (int i = 1; i <= N; ++i) {
    int comb = (1 << i) - 1;
    while (comb < 1 << 10) {
      REP(j, 331) { table[i][j] += dp[comb][j]; }

      int x = comb & -comb, y = comb + x;
      comb = ((comb & ~y) / x >> 1) | y;
    }
    // if (i == 3) {
    //   REP(j,331) {
    //     cout << table[i][j] << endl;
    //   }
    // }
    // cout << table[i][10] << endl;
  }

  int n, s;
  while (cin >> n >> s) {
    if (n < 0 || n > 10 || s < 0 || s >= 331)
      cout << 0 << endl;
    else
      cout << table[n][s] << endl;
  }
}
Problem p00071 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  cin.ignore();
  for (int k = 0; k < n; ++k) {
    int grid[8][8];
    int x, y;
    for (int i = 0; i < 8; ++i) {
      string str;
      cin >> str;
      for (int j = 0; j < 8; ++j) {
        grid[i][j] = str[j] - '0';
      }
    }
    cin >> x;
    cin >> y;
    cin.ignore();

    int dx[12] = {1, 2, 3, 0, 0, 0, -1, -2, -3, 0, 0, 0};
    int dy[12] = {0, 0, 0, 1, 2, 3, 0, 0, 0, -1, -2, -3};
    vector<pair<int, int>> p;
    p.push_back(make_pair(x - 1, y - 1));
    while (!p.empty()) {
      auto pos = p.back();
      p.pop_back();
      for (int i = 0; i < 12; ++i) {
        int nx = pos.first + dx[i];
        int ny = pos.second + dy[i];
        if (0 <= nx && nx < 8 && 0 <= ny && ny < 8) {
          if (grid[ny][nx] == 1)
            p.push_back(make_pair(nx, ny));
        }
      }
    }

    printf("Data %d:\n", k + 1);
    for (int i = 0; i < 8; ++i) {
      for (int j = 0; j < 8; ++j) {
        cout << grid[i][j];
      }
      cout << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  cin.ignore();
  for (int k = 0; k < n; ++k) {
    int grid[8][8];
    int x, y;
    for (int i = 0; i < 8; ++i) {
      string str;
      cin >> str;
      for (int j = 0; j < 8; ++j) {
        grid[i][j] = str[j] - '0';
      }
    }
    cin >> x;
    cin >> y;
    cin.ignore();

    int dx[12] = {1, 2, 3, 0, 0, 0, -1, -2, -3, 0, 0, 0};
    int dy[12] = {0, 0, 0, 1, 2, 3, 0, 0, 0, -1, -2, -3};
    vector<pair<int, int>> p;
    p.push_back(make_pair(x - 1, y - 1));
    while (!p.empty()) {
      auto pos = p.back();
      grid[pos.second][pos.first] = 0;
      p.pop_back();
      for (int i = 0; i < 12; ++i) {
        int nx = pos.first + dx[i];
        int ny = pos.second + dy[i];
        if (0 <= nx && nx < 8 && 0 <= ny && ny < 8) {
          if (grid[ny][nx] == 1)
            p.push_back(make_pair(nx, ny));
        }
      }
    }

    printf("Data %d:\n", k + 1);
    for (int i = 0; i < 8; ++i) {
      for (int j = 0; j < 8; ++j) {
        cout << grid[i][j];
      }
      cout << endl;
    }
  }
  return 0;
}
Problem p00072 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;

#define MAX_V 111
#define INF (1 << 21)

int cost[MAX_V][MAX_V] = {{0}};
int mincost[MAX_V] = {};
bool used[MAX_V] = {};
int V;

int prim(void);
int min(int a, int b);

int main() {
  int m;

  scanf("%d", &V);

  while (1) {
    if (V == 0)
      break;
    scanf("%d", &m);

    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        cost[i][j] = INF;
      }
    }

    for (int i = 0; i < m; i++) {
      int a, b, c;
      scanf("%d,%d,%d", &a, &b, &c);
      cost[a][b] = c / 100 - 1;
      cost[b][a] = c / 100 - 1;
    }

    printf("%d\n", prim());

    scanf("%d", &V);
  }

  return 0;
}

int min(int a, int b) {
  if (a > b)
    return b;
  return a;
}

int prim(void) {
  for (int i = 0; i < V; i++) {
    mincost[i] = INF;
    used[i] = false;
  }

  mincost[0] = 0;

  int res = 0;

  while (true) {
    int v = -1;
    for (int u = 0; u < V; u++) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v]))
        v = u;
    }

    if (v == -1)
      break;
    used[v] = true;
    res += mincost[v];

    for (int u = 0; u < V; u++) {
      // if(cost[v][u]==0);
      // else{
      mincost[u] = min(mincost[u], cost[v][u]);
      //     }
    }
  }
  return res;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;

#define MAX_V 111
#define INF (1 << 21)

int cost[MAX_V][MAX_V] = {{0}};
int mincost[MAX_V] = {};
bool used[MAX_V] = {};
int V;

int prim(void);
int min(int a, int b);

int main() {
  int m;

  scanf("%d", &V);

  while (1) {
    if (V == 0)
      break;
    scanf("%d", &m);

    for (int i = 0; i < V; i++) {
      for (int j = 0; j < V; j++) {
        cost[i][j] = INF;
      }
    }

    for (int i = 0; i < m; i++) {
      int a, b, c;
      scanf("%d,%d,%d", &a, &b, &c);
      cost[a][b] = c / 100 - 1;
      cost[b][a] = c / 100 - 1;
    }

    printf("%d\n", prim());

    scanf("%d", &V);
  }

  return 0;
}

int min(int a, int b) {
  if (a > b)
    return b;
  return a;
}

int prim(void) {
  for (int i = 0; i < V; i++) {
    mincost[i] = INF;
    used[i] = false;
  }

  mincost[0] = 0;

  int res = 0;

  while (true) {
    int v = -1;
    for (int u = 0; u < V; u++) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v]))
        v = u;
    }

    if (v == -1)
      break;
    used[v] = true;
    res += mincost[v];

    for (int u = 0; u < V; u++) {
      // if(cost[v][u]==0);
      // else{
      mincost[u] = min(mincost[u], cost[v][u]);
      //     }
    }
  }
  return res;
}
Problem p00072 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

const int INF = 1000000000;
const int MAX_V = 101;

int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];

int V;

int prim() {
  for (int i = 0; i < V; ++i) {
    mincost[i] = INF;
    used[i] = false;
  }

  mincost[0] = 0;
  int res = 0;

  while (true) {
    int v = -1;
    for (int u = 0; u < V; u++) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v]))
        v = u;
    }

    if (v == -1)
      break;
    used[v] = true;
    res += mincost[v];

    for (int u = 0; u < V; u++) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }
  return res;
}

int main() {
  int m;
  while (cin >> V >> m, V) {
    for (int i = 0; i < V; i++) {
      for (int j = 0; j < m; j++) {
        cost[i][j] = INF;
      }
    }
    for (int i = 0; i < m; i++) {
      char ch;
      int a, b, c;
      cin >> a >> ch >> b >> ch >> c;
      cost[a][b] = c;
      cost[b][a] = c;
    }
    int ans = prim();
    cout << (ans / 100) - (V - 1) << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

const int INF = 1000000000;
const int MAX_V = 10000;

int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];

int V;

int prim() {
  for (int i = 0; i < V; ++i) {
    mincost[i] = INF;
    used[i] = false;
  }

  mincost[0] = 0;
  int res = 0;

  while (true) {
    int v = -1;
    for (int u = 0; u < V; u++) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v]))
        v = u;
    }

    if (v == -1)
      break;
    used[v] = true;
    res += mincost[v];

    for (int u = 0; u < V; u++) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }
  return res;
}

int main() {
  int m;
  while (cin >> V >> m, V) {
    for (int i = 0; i < V; i++) {
      for (int j = 0; j < m; j++) {
        cost[i][j] = INF;
      }
    }
    for (int i = 0; i < m; i++) {
      char ch;
      int a, b, c;
      cin >> a >> ch >> b >> ch >> c;
      cost[a][b] = c;
      cost[b][a] = c;
    }
    int ans = prim();
    cout << (ans / 100) - (V - 1) << endl;
  }
  return 0;
}
Problem p00072 (C++)

Buggy submission (status: Runtime Error):
// kruskal tree
#include <algorithm>
#include <cstdio>
using namespace std;
#define M 999
int parent[M], a[M], b[M];
pair<int, int> node[M];
int root(int a) { return parent[a] == a ? a : parent[a] = root(parent[a]); }
int unite(int a, int b) {
  int x = root(a), y = root(b);
  if (x == y)
    return 0;
  parent[x] = y;
  return 1;
}
int main() {
  int i, s, n, m;
  for (; scanf("%d%d", &n, &m) > 1; printf("%d\n", s / 100 - n + 1)) {
    for (i = 0; i < m; i++)
      scanf("%d,%d,%d", a + i, b + i, &node[i].first), node[i].second = i;
    sort(node, node + m);
    for (i = 0; i < n; i++)
      parent[i] = i;
    for (s = i = 0; i < m; i++)
      if (unite(a[node[i].second], b[node[i].second]))
        s += node[i].first;
  }
}

Fixed submission:
// kruskal tree
#include <algorithm>
#include <cstdio>
using namespace std;
#define M 99999
int parent[M], a[M], b[M];
pair<int, int> node[M];
int root(int a) { return parent[a] == a ? a : parent[a] = root(parent[a]); }
int unite(int a, int b) {
  int x = root(a), y = root(b);
  if (x == y)
    return 0;
  parent[x] = y;
  return 1;
}
int main() {
  int i, s, n, m;
  for (; scanf("%d%d", &n, &m) > 1; printf("%d\n", s / 100 - n + 1)) {
    for (i = 0; i < m; i++)
      scanf("%d,%d,%d", a + i, b + i, &node[i].first), node[i].second = i;
    sort(node, node + m);
    for (i = 0; i < n; i++)
      parent[i] = i;
    for (s = i = 0; i < m; i++)
      if (unite(a[node[i].second], b[node[i].second]))
        s += node[i].first;
  }
}
Problem p00072 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <functional>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> Pair;

struct Edge {
  int len;
  int to;
};

int n;
int m;
bool used[10000];
vector<Edge> es[10000];
priority_queue<Pair, vector<Pair>, greater<Pair>> q;

int CalcMSTCost() {
  int ret = 0;

  fill(used, used + n, false);
  q.push(Pair(0, 0));
  while (!q.empty()) {
    Pair p = q.top();
    q.pop();
    int d = p.first;
    int v = p.second;

    if (used[v])
      continue;
    used[v] = true;
    ret += d;
    for (int i = 0; i < es[v].size(); i++) {
      q.push(Pair(es[v][i].len, es[v][i].to));
    }
  }

  return ret;
}

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      return 0;

    for (int i = 0; i < n; i++) {
      es[i].clear();
    }

    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
      int a, b;
      struct Edge e;
      scanf("%d,%d,%d", &a, &b, &e.len);
      --a;
      --b;
      e.len -= 100;
      e.to = b;
      es[a].push_back(e);
      e.to = a;
      es[b].push_back(e);
    }

    printf("%d\n", CalcMSTCost() / 100);
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> Pair;

struct Edge {
  int len;
  int to;
};

int n;
int m;
bool used[10000];
vector<Edge> es[10000];
priority_queue<Pair, vector<Pair>, greater<Pair>> q;

int CalcMSTCost() {
  int ret = 0;

  fill(used, used + n, false);
  q.push(Pair(0, 0));
  while (!q.empty()) {
    Pair p = q.top();
    q.pop();
    int d = p.first;
    int v = p.second;

    if (used[v])
      continue;
    used[v] = true;
    ret += d;
    for (int i = 0; i < es[v].size(); i++) {
      q.push(Pair(es[v][i].len, es[v][i].to));
    }
  }

  return ret;
}

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      return 0;

    for (int i = 0; i < n; i++) {
      es[i].clear();
    }

    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
      int a, b;
      struct Edge e;
      scanf("%d,%d,%d", &a, &b, &e.len);
      e.len -= 100;
      e.to = b;
      es[a].push_back(e);
      e.to = a;
      es[b].push_back(e);
    }

    printf("%d\n", CalcMSTCost() / 100);
  }
}