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

#include <iostream>
using namespace std;

int n, s, res;

void dfs(int decided, int i, int sum) {
  if (decided == n) {
    if (sum == s)
      res++;
    return;
  }

  // 0~9を選り好みしてn個決められなかったとき終了
  if (i > 9)
    return;

  // iを足さない
  dfs(decided, i + 1, sum);

  // iを足す
  dfs(decided + 1, i + 1, sum + i);
}

int main() {
  while (true) {
    res = 0;
    cin >> n >> s;
    if (n == 0 && s == 0)
      break;
    dfs(0, 0, 0);
    cout << res << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int n, s;

int dfs(int i, int sum) {
  if (i == n) {
    if (sum == s)
      return sum;
  }

  // iを足さない
  if (dfs(i + 1, sum) != 0)
    return sum;

  // iを足す
  if (dfs(i + 1, sum + i) != 0)
    return sum;

  return 0;
}

int main() {
  while (true) {
    cin >> n, s;
    if (n == 0 && s == 0)
      break;
    cout << dfs(0, 0) << endl;
  }
  return 0;
}
Problem p00030 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
int n, s;
int dfs(int x, int y, int sum) {
  if (y == n)
    return (sum == s);
  if (x == 10)
    return 0;
  return dfs(x + 1, y, sum) + dfs(x + 1, y + 1, sum + x);
}
int main() {
  while (scanf("%d %d", &n, &s), n | s) {
    printf("%d\\n", dfs(0, 0, 0));
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
bool used[10];
int n, s;
int dfs(int x, int y, int sum) {
  if (x == n)
    return (sum == s);
  if (sum >= s)
    return 0;
  int ret = 0;
  for (int i = y + 1; i <= 9; i++) {
    ret += dfs(x + 1, y, sum);
    if (!used[i]) {
      used[i] = true;
      ret += dfs(x + 1, i, sum + i);
      used[i] = false;
    }
  }
  return ret;
}
int main() {
  while (scanf("%d %d", &n, &s), n | s) {
    memset(used, 0, sizeof(used));
    printf("%d\\n", dfs(0, -1, 0));
  }
}
Problem p00030 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
string::size_type size(string x) {
  string::size_type size = x.size();
  return size;
}
#define fu(i, l, k) for (int i = l; i < k; i++)
#define fd(i, l, k) for (int i = l; i > k; i--)
#define fi first
#define se second
double pi() { return M_PI; }
typedef set<int> set_i;
typedef set<string> set_s;
int n, k;
int ans = 0;
void dfs(int i, int j, int sum) {
  if (i == n) {
    if (sum == k)
      ans++;
  } else if (j <= 9 && sum <= k) {
    dfs(i + 1, j + 1, sum + j);
    dfs(i, j + 1, sum);
  }
}

int main() {
  while (cin >> n >> k) {
    if (n == 0 && k == 0)
      return 0;
    ans = 0;
    dfs(0, 0, 0);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
string::size_type size(string x) {
  string::size_type size = x.size();
  return size;
}
#define fu(i, l, k) for (int i = l; i < k; i++)
#define fd(i, l, k) for (int i = l; i > k; i--)
#define fi first
#define se second
double pi() { return M_PI; }
typedef set<int> set_i;
typedef set<string> set_s;
int ans = 0, sum = 0, s;
void calc(int n, int sum, int s) {
  if (n == 0) {
    if (sum == s)
      ans++;
  } else {
    for (int i = 0; i < 10; i++) {
      if (n != 0)
        calc(n - 1, sum += i, s);
    }
  }
}
int main() {
  int e = 0, n[53] = {}, s[53] = {};
  while (1) {
    cin >> n[e] >> s[e];
    if (n[e] == 0 && s[e] == 0)
      break;
    e++;
  }
  for (int q = 0; q < e; q++) {
    sum = 0;
    if (n[q] - 1 > s[q] || s[q] > 45)
      cout << "0" << endl;
    else {
      calc(n[q], sum, s[q]);
      cout << ans / 2 << endl;
      ans = 0;
    }
  }
}
Problem p00030 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int x, y, z[51] = {};

int mySet(int d, int e) {
  int a = 0;
  for (int i = 1; i < d; i++) {
    if (z[i] >= z[i + 1]) {
      return 0;
    }
  }
  if (d == x) {
    if (e == y) {
      return 1;
    } else {
      return 0;
    }
  }
  for (int j = 0; j <= 9; j++) {
    z[d + 1] = j;
    a += mySet(d + 1, e + j);
  }
  return a;
}

int main(void) {
  while (1) {
    cin >> x >> y;
    if (x == 0 && y == 0) {
      break;
    } else {
      cout << mySet(0, 0) << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int x, y, z[50] = {};

int mySet(int d, int e) {
  int a = 0;
  if (d == x) {
    if (e == y) {
      return 1;
    } else {
      return 0;
    }
  }
  for (int i = 0; i <= 9; i++) {
    z[d] = i;
    a += mySet(d + 1, e + i);
  }
  return a;
}

int main(void) {
  while (1) {
    cin >> x >> y;
    if (x == 0 && y == 0) {
      break;
    } else {
      cout << mySet(0, 0) << endl;
    }
  }
  return 0;
}
Problem p00030 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

int dp[105][10][10], s, n;
int f(int a, int b, int c) {
  if (dp[a][b][c] != -1)
    return dp[a][b][c];
  if (a == s && c == n && b <= 10)
    return 1;
  if (a > s || c > n || b == 10)
    return 0;
  int res = 0;
  res += f(a + b, b + 1, c + 1);
  res += f(a, b + 1, c);
  return dp[a][b][c] = res;
}

signed main(void) {
  while (1) {
    cin >> n >> s;
    if (!n && !s)
      break;
    REP(i, 105) REP(j, 10) REP(k, 10) dp[i][j][k] = -1;
    cout << f(0, 0, 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

int dp[105][10][10], s, n;
int f(int a, int b, int c) {
  if (dp[a][b][c] != -1)
    return dp[a][b][c];
  if (a == s && c == n && b <= 10)
    return 1;
  if (a > s || c > n || b == 10)
    return 0;
  int res = 0;
  res += f(a + b, b + 1, c + 1);
  res += f(a, b + 1, c);
  return dp[a][b][c] = res;
}

signed main(void) {
  while (1) {
    cin >> n >> s;
    REP(i, 105) REP(j, 10) REP(k, 10) dp[i][j][k] = -1;
    cout << f(0, 0, 0) << endl;
  }
  return 0;
}
Problem p00030 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int research(int n, int s, int v, int t) {
  if (n == 0 && v == s) {
    return 1;
  }
  if (v > s || n < 0) {
    return 0;
  }
  int c = 0;
  for (int i = t + 1; i <= 9; i++) {
    c += research(n - 1, s, i + v, i);
  }

  return c;
}

int main() {
  int n, s, c;

  while (1) {
    cin >> n >> s;
    if (n == 0 && s == 0) {
      break;
    }

    c = 0;
    for (int i = 0; i <= 9; i++) {
      c += research(n - 1, s, i, i);
    }

    cout << c << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int t;
int func(int n, int s, int v, int p) {
  int c = 0;

  if (n == 1 && s - v < t) {
    t = v;
    return 1;
  } else if (n == 0 && v != 0) {
    return 0;
  } else if (n == 0 && v == 0) {
    t = p;
    return 1;
  }

  for (int i = p; i < t; i++) {
    c += func(n - 1, s, v - i, i + 1);
  }

  return c;
}

int main(int argc, char **argv) {
  int n, s;

  while (cin >> n >> s) {
    if (n == 0 && s == 0) {
      break;
    }
    t = s;
    cout << func(n, t, s, 0) << endl;
    ;
  }
  return 0;
}
Problem p00030 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int n, s, j, k;
  int a[11][46];
  for (int s = 0; s < 46; s++)
    for (int n = 0; n < 11; n++)
      a[n][s] = 0;
  n = s = 0;
  for (int i = 0; i < 1024; i++) {
    for (j = i, n = 0; j > 0; j /= 2)
      n += j % 2;
    for (j = i, s = 0, k = 0; j > 0; j /= 2, k++)
      s += j % 2 * k;
    a[n][s]++;
  }
  while (true) {
    cin >> n >> s;
    if (!n && !s)
      break;
    if (s < 46)
      cout << a[n][s] << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}
//  for(cin>>n>>s;n||s;cout<<a[n][s]<<endl,cin>>n>>s);

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, s, j, k;
  int a[11][46];
  for (int s = 0; s < 46; s++)
    for (int n = 0; n < 11; n++)
      a[n][s] = 0;
  n = s = 0;
  for (int i = 0; i < 1024; i++) {
    for (j = i, n = 0; j > 0; j /= 2)
      n += j % 2;
    for (j = i, s = 0, k = 0; j > 0; j /= 2, k++)
      s += j % 2 * k;
    a[n][s]++;
  }
  while (true) {
    cin >> n >> s;
    if (!n && !s)
      break;
    cout << a[n][s] << endl;
  }
  return 0;
}
//  for(cin>>n>>s;n||s;cout<<a[n][s]<<endl,cin>>n>>s);
Problem p00030 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <complex>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define EPS 1e-8

int memo[11][50];
int main() {
  int n, s;

  for (int i = 0; i < (1 << 10); i++) {
    int sum = 0;
    int bitc = 0;
    for (int j = 0; (1 << j) <= i; j++)
      if (i & (1 << j)) {
        sum += j;
        bitc++;
      }
    memo[bitc][sum]++;
    // printf("bitc:%d  , sum:%d\\n",bitc,sum);
  }

  while (scanf("%d%d", &n, &s), n | s) {
    printf("%d\\n", s < 50 ? memo[n][s] : 0);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <climits>
#include <complex>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define EPS 1e-8

int memo[16][1000];
int main() {
  int n, s;

  for (int i = 0; i < (1 << 10); i++) {
    int sum = 0;
    int bitc = 0;
    for (int j = 0; (1 << j) <= i; j++)
      if (i & (1 << j)) {
        sum += j;
        bitc++;
      }
    memo[bitc][sum]++;
    printf("bitc:%d  , sum:%d\\n", bitc, sum);
  }

  while (scanf("%d%d", &n, &s), n | s) {
    printf("%d\\n", memo[n][s]);
  }
  return 0;
}
Problem p00030 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;
int n, s;

int cnt = 0;

int func(string m, vector<bool> b, int num) {
  if (m.size() == n) {
    int tmp = 0;
    for (int i = 0; i < n; i++)
      tmp += m[i] - '0';
    if (tmp == s)
      cnt++;
    // if( tmp == s ) cerr << m << endl;
    return 0;
  }

  for (int i = num + 1; i < 10; i++) {
    if (b[i])
      continue;
    b[i] = true;
    func(m + to_string(i), b, i);
    b[i] = false;
  }
  return 0;
}

int main(void) {

  while (cin >> n >> s) {
    if (n == 0 && s == 0)
      break;

    vector<bool> b(10, false);
    for (int i = 0; i < 10; i++) {
      b[i] = true;
      func(to_string(i), b, i);
      b[i] = false;
    }
    cout << cnt << endl;
    cnt = 0;
  }

  return 0;
}

// EOF

Error: 0
015
024
123


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;
int n, s;

int cnt = 0;

int func(string m, vector<bool> b, int num) {
  if (m.size() == n) {
    int tmp = 0;
    for (int i = 0; i < n; i++)
      tmp += m[i] - '0';
    if (tmp == s)
      cnt++;
    if (tmp == s)
      cerr << m << endl;
    return 0;
  }

  for (int i = num + 1; i < 10; i++) {
    if (b[i])
      continue;
    b[i] = true;
    func(m + to_string(i), b, i);
    b[i] = false;
  }
}

int main(void) {

  while (cin >> n >> s) {
    if (n == 0 && s == 0)
      break;

    vector<bool> b(10, false);
    for (int i = 0; i < 10; i++) {
      b[i] = true;
      func(to_string(i), b, i);
      b[i] = false;
    }
    cout << cnt << endl;
    cnt = 0;
  }

  return 0;
}

// EOF
Problem p00030 (C++) — buggy submission, verdict: Runtime Error

// 0030
// template
// include-------------------------------------
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// typedef--------------------------------------
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

#define MP make_pair

// repetition-----------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

// constant-------------------------------------
const double PI = acos(-1.0);

// cout,cin-------------------------------------
#define COUT(x) cout << (x) << endl;
#define CIN(x) cin >> (x);

int dfs(int n, int s, int now) {
  int ans = 0;
  if (now > 10)
    return 0;
  if (n == 0)
    return s == 0 ? 1 : 0;
  ans += dfs(n - 1, s - now, now + 1);
  ans += dfs(n, s, now + 1);
  return ans;
}

int main() {

  int n, s;
  while (cin >> n >> s) {
    if (n == 0 && s == 0)
      break;
    COUT(dfs(n, s, 0))
  }

  return 0;
}

Error: -11

Fixed submission:

// 0030
// template
// include-------------------------------------
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// typedef--------------------------------------
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

#define MP make_pair

// repetition-----------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

// constant-------------------------------------
const double PI = acos(-1.0);

// cout,cin-------------------------------------
#define COUT(x) cout << (x) << endl;
#define CIN(x) cin >> (x);

int dfs(int n, int s, int p, int ans) {
  if (p > 10)
    return 0;
  if (n == 0)
    return s == 0 ? 1 : 0;
  for (int i = p; i < 10; i++)
    ans += dfs(n--, s - i, p++, ans);
  return ans;
}

int main() {

  int n, s;
  while (cin >> n >> s) {
    if (n == 0 && s == 0)
      break;
    COUT(dfs(n, s, 0, 0))
  }

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

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

typedef vector<int> vi;

int n, s;

//??????p????????¶ c???????????°??? sum????¨?
int dfs(int p, int c, int sum) {
  if (c == 10)
    return p == 0 && sum == s;

  return dfs(p - 1, c + 1, sum + c) + dfs(p, c + 1, sum);
}

int main() {
  while (true) {
    cin >> n >> s;
    if (!n && !s)
      break;

    cout << dfs(n, 0, 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

typedef vector<int> vi;

int n, s;

// p????????¶ c???????????°??? sum????¨?
int dfs(int p, int c, int sum) {
  if (c > 100)
    return 0;
  if (p == n)
    return sum == s;
  return dfs(p + 1, c + 1, sum + c) + dfs(p, c + 1, sum);
}

int main() {
  while (true) {
    cin >> n >> s;
    if (!n && !s)
      break;
    cout << dfs(0, 0, 0) << endl;
  }
  return 0;
}
Problem p00030 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define TEMP_T template <typename T>
TEMP_T void sort(T &v) { sort(v.begin(), v.end()); }
TEMP_T void revs(T &v) { reverse(v.begin(), v.end()); }
TEMP_T void uniq(T &v) {
  sort(v);
  v.erase(unique(v.begin(), v.end()));
}
TEMP_T void show(T &v, char delim = ' ', char end = '\\n') {
  for (int i = 0; i < v.size() - 1; i++)
    cout << v[i] << delim;
  cout << v[v.size() - 1] << end;
}
TEMP_T T cums(T &v) {
  T r;
  r.push_back(v[0]);
  for (int i = 1; i < v.size(); i++)
    r.push_back(r[i - 1] + v[i]);
  return r;
}
TEMP_T void maxe(T &v, T m) { v = max(v, m); }
TEMP_T void mine(T &v, T m) { v = min(v, m); }

static inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}

struct Solver {
  int pattern[64];
  int n;
  Solver(int n) : n(n) { memset(pattern, 0, sizeof(pattern)); }
  void solve(int it = 0, int c = 0, int s = 0) {
    if (it == 10) {
      if (c == n) {
        pattern[s]++;
      }
      return;
    }
    solve(it + 1, c + 1, s + it);
    solve(it + 1, c, s);
  }
};

int main() {
  int n, s;
  while (n = in(), s = in(), !(n == 0 && s == 0)) {
    Solver solver(n);
    solver.solve();
    if (s < 64) {
      printf("%d\\n", solver.pattern[s]);
    } else
      puts("0");
  }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define TEMP_T template <typename T>
TEMP_T void sort(T &v) { sort(v.begin(), v.end()); }
TEMP_T void revs(T &v) { reverse(v.begin(), v.end()); }
TEMP_T void uniq(T &v) {
  sort(v);
  v.erase(unique(v.begin(), v.end()));
}
TEMP_T void show(T &v, char delim = ' ', char end = '\\n') {
  for (int i = 0; i < v.size() - 1; i++)
    cout << v[i] << delim;
  cout << v[v.size() - 1] << end;
}
TEMP_T T cums(T &v) {
  T r;
  r.push_back(v[0]);
  for (int i = 1; i < v.size(); i++)
    r.push_back(r[i - 1] + v[i]);
  return r;
}
TEMP_T void maxe(T &v, T m) { v = max(v, m); }
TEMP_T void mine(T &v, T m) { v = min(v, m); }

static inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}

struct Solver {
  int pattern[64];
  int n;
  Solver(int n) : n(n) { memset(pattern, 0, sizeof(pattern)); }
  void solve(int it = 0, int c = 0, int s = 0) {
    if (it <= 10 && c == n) {
      pattern[s]++;
      return;
    }
    solve(it + 1, c + 1, s + it);
    solve(it + 1, c, s);
  }
};

int main() {
  int n, s;
  while (n = in(), s = in(), n != 0 && s != 0) {
    Solver solver(n);
    solver.solve();
    if (s < 64) {
      printf("%d\\n", solver.pattern[s]);
    } else
      puts("0");
  }

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

#include <iostream>
using namespace std;

int dp[11][46] = {0};

int main() {
  dp[0][0] = 1;
  for (int k = 0; k < 10; k++) {
    for (int i = k; i >= 0; i--)
      for (int j = 0; j < 46 - k; j++) {
        dp[i + 1][j + k] += dp[i][j];
      }
  }

  int N, S;
  while (cin >> N >> S, N || S) {
    if (0 <= N && N < 11 && 0 <= S && S < 46) {
      cout << dp[N][S] << endl;
    } else {
      cout << 0 << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int dp[11][46] = {0};

int main() {
  dp[0][0] = 1;
  for (int k = 0; k < 10; k++) {
    for (int i = k; i >= 0; i--)
      for (int j = 0; j < 46 - k; j++) {
        dp[i + 1][j + k] += dp[i][j];
      }
  }

  int N, S;
  while (cin >> N >> S, N || S) {
    cout << dp[N][S] << endl;
  }
}
Problem p00030 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> comb;

inline int first(int n) { return ((1 << n) - 1); }

int nextSet(int x) {
  int small, ripple, newSmall, ones;

  small = x & -x;
  ripple = x + small;
  newSmall = ripple & -ripple;
  ones = ((newSmall / small) >> 1) - 1;

  return (ripple | ones);
}

comb setComb(int s, int n) {
  comb c;
  for (int i = 1; i <= n; i++) {
    if (s & 1)
      c.push_back(i - 1);
    s >>= 1;
  }
  return c;
}

int main() {
  int n = 10, k, x, s, sum, ans;
  comb c;

  while (cin >> k >> s, k || s) {
    ans = 0;
    x = first(k);
    while (!(x & ~first(n))) {
      c = setComb(x, n);
      sum = 0;
      for (int i = 0; i < c.size(); i++) {
        sum += c.at(i);
      }
      if (sum == s)
        ans++;
      x = nextSet(x);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>
int ans, n, s;

void p(int count, int sum, int num) {

  if (sum > s || count > n)
    return;

  for (int i = num + 1; i <= 9, sum + i <= s; i++) {
    if ((sum + i) == s && count == n)
      ans++;
    p(count + 1, sum + i, i);
  }
}

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

    p(1, 0, -1);
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00030 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int n, s;

int dfs(int a, int m, int c) {
  if (a == s && c == n)
    return 1;
  if (a > s || c > n)
    return 0;
  if (m > 9)
    return 0;
  int t = 0;
  for (int i = m; a + i <= s && i < 10; i++) {
    t += dfs(a + i, i + 1, c + 1);
  }
  return t;
}

int main() {
  while (true) {
    cin >> n >> s;
    if (n == 0 && s == 0)
      break;
    cout << dfs(0, 0, 0) << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

const int LN = 10, LS = 10000;
int dp[LN + 1][LN + 1], n, s;

void set() {
  for (int i = 0; i <= LS; i++) {
    dp[1][i] = 1;
  }
  for (int i = 1; i < LN; i++) {
    for (int j = 0; j <= LS; j++) {
      dp[i + 1][j] = 0;
      if (j >= i)
        dp[i + 1][j] += dp[i][j - i];
      if (j > i)
        dp[i + 1][j] += dp[i + 1][j - i - 1];
    }
  }
}

int main() {
  set();
  while (true) {
    cin >> n >> s;
    if (n == 0 && s == 0)
      break;
    cout << dp[n][s] << endl;
  }
  return 0;
}
Problem p00030 (C++) — buggy submission, verdict: Runtime Error

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

int n, s, ans, dp[1 << 8][15];

void rec(int base, int sum, int used) {
  if (sum == s) {
    if (used == n)
      ans += 1;
  }
  if (sum > s || used > n)
    return;
  for (int i = base; i < 10; i++) {
    rec(i + 1, sum + i, used + 1);
  }
}

int main(void) {
  while (scanf("%d%d", &n, &s) == 2 && !(n + s == 0)) {
    ans = 0;
    memset(dp, 0, sizeof(dp));
    rec(0, 0, 0);
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

int N, S, ans, dp[100][100][100];

void rec(int base, int x, int used) {
  if (x > S || used > N)
    return;
  if (x == S && used == N) {
    if (!dp[base][x][used]) {
      dp[base][x][used] = 1;
      ans += 1;
    }
    return;
  }
  dp[base][x][used] = 1;
  for (int i = base; i <= 9; i++) {
    rec(i + 1, x + i, used + 1);
  }
}

int main(void) {
  while (1) {
    scanf("%d%d", &N, &S);
    if (N == 0 && S == 0)
      break;
    ans = 0;
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i <= S; i++) {
      rec(i + 1, i, 1);
    }
    printf("%d\\n", ans);
  }
  return 0;
}