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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define FOR(i, l, r) for (i = l; i <= r; i++)
#define INF 0x3f3f3f3f

using namespace std;

int i, j, k, l, m, n;
static int c[5][6], t[30], z[6];
const int w[24][6] = {
    2, 3, 0, 5, 4, 1, 3, 2, 0, 5, 1, 4, 4, 1, 0, 5, 3, 2, 1, 4, 0, 5, 2, 3,
    2, 3, 1, 4, 0, 5, 3, 2, 1, 4, 5, 0, 0, 5, 1, 4, 3, 2, 5, 0, 1, 4, 2, 3,
    5, 0, 2, 3, 4, 1, 0, 5, 2, 3, 1, 4, 4, 1, 2, 3, 0, 5, 1, 4, 2, 3, 5, 0,
    5, 0, 3, 2, 1, 4, 0, 5, 3, 2, 4, 1, 1, 4, 3, 2, 0, 5, 4, 1, 3, 2, 5, 0,
    2, 3, 4, 1, 5, 0, 3, 2, 4, 1, 0, 5, 5, 0, 4, 1, 3, 2, 0, 5, 4, 1, 2, 3,
    2, 3, 5, 0, 1, 4, 3, 2, 5, 0, 4, 1, 1, 4, 5, 0, 3, 2, 4, 1, 5, 0, 2, 3};
string st;
int dfs(int o) {
  int ans = INF, i, j, k;
  if (o > n) {
    ans = 0;
    FOR(i, 0, 5) {
      memset(t, 0, sizeof(t));
      int ma = 0;
      FOR(j, 0, n)
      ma = max(ma, ++t[c[j][w[z[j]][i]]]);
      ans += (n - ma);
    }
    return ans;
  }
  FOR(i, 0, 23) {
    z[o] = i;
    ans = min(ans, dfs(o + 1));
  }
  return ans;
}
int main() {
  map<string, int> a;
  while (scanf("%d", &n) != EOF && n) {
    FOR(i, 1, n) FOR(j, 0, 5) {
      cin >> st;
      if (!a[st])
        a[st] = ++m;
      c[i][j] = a[st];
    }
    z[1] = 0;
    printf("%d\\n", dfs(2));
    map<string, int>::iterator o = a.begin();
    for (; o != a.end(); o++)
      o->second = 0;
    m = 0;
  }
  return 0;
}

Error: 1

Fixed submission:

#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <stdio.h>
#include <string.h>
#define FOR(i, l, r) for (i = l; i <= r; i++)
#define INF 0x3f3f3f3f
using namespace std;
int i, j, k, l, m, n;
static int c[5][6], t[30], z[6];
const int w[24][6] = {
    2, 3, 0, 5, 4, 1, 3, 2, 0, 5, 1, 4, 4, 1, 0, 5, 3, 2, 1, 4, 0, 5, 2, 3,
    2, 3, 1, 4, 0, 5, 3, 2, 1, 4, 5, 0, 0, 5, 1, 4, 3, 2, 5, 0, 1, 4, 2, 3,
    5, 0, 2, 3, 4, 1, 0, 5, 2, 3, 1, 4, 4, 1, 2, 3, 0, 5, 1, 4, 2, 3, 5, 0,
    5, 0, 3, 2, 1, 4, 0, 5, 3, 2, 4, 1, 1, 4, 3, 2, 0, 5, 4, 1, 3, 2, 5, 0,
    2, 3, 4, 1, 5, 0, 3, 2, 4, 1, 0, 5, 5, 0, 4, 1, 3, 2, 0, 5, 4, 1, 2, 3,
    2, 3, 5, 0, 1, 4, 3, 2, 5, 0, 4, 1, 1, 4, 5, 0, 3, 2, 4, 1, 5, 0, 2, 3};
string st;
int dfs(int o) {
  int ans = INF, i, j, k;
  if (o > n) {
    ans = 0;
    FOR(i, 0, 5) {
      memset(t, 0, sizeof(t));
      int ma = 0;
      FOR(j, 0, n) ma = max(ma, ++t[c[j][w[z[j]][i]]]);
      ans += (n - ma);
    }
    return ans;
  }
  FOR(i, 0, 23) {
    z[o] = i;
    ans = min(ans, dfs(o + 1));
  }
  return ans;
}
int main() {

  map<string, int> a;
  while (scanf("%d", &n) != EOF && n) {
    FOR(i, 1, n) FOR(j, 0, 5) {
      cin >> st;
      if (!a[st])
        a[st] = ++m;
      c[i][j] = a[st];
    }
    z[1] = 0;
    printf("%d\\n", dfs(2));
    map<string, int>::iterator o = a.begin();
    for (; o != a.end(); o++)
      o->second = 0;
    // a.begin()=a.end();
    m = 0;
  }
  return 1;
}
Problem p00841 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#define INF 2000000
#define t first
#define x second
using namespace std;
typedef pair<double, int> P;
double b, v, e, f;
int r;

double mk(int x) {
  if (x >= r)
    return 1.0 / (v - e * (x - r));
  return 1.0 / (v - f * (r - x));
}

int main() {
  while (1) {
    int n, a[110] = {};
    cin >> n;
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++)
      cin >> a[i];
    cin >> b;
    cin >> r >> v >> e >> f;
    P dp[10100] = {};
    for (int i = 1; i <= a[n]; i++)
      dp[i] = make_pair(INF, 0);

    for (int i = 0; i < n; i++) {
      int tx;
      double tt;
      // No Change
      tx = dp[a[i]].x, tt = dp[a[i]].t;
      for (int j = a[i] + 1; j <= a[n]; j++) {
        double k = mk(tx);
        tt += k, tx++;
        dp[j] = min(dp[j], make_pair(tt, tx));
      }

      // Change
      tx = 0, tt = dp[a[i]].t + b;
      for (int j = a[i] + 1; j <= a[n]; j++) {
        double k = mk(tx);
        tt += k;
        tx++;
        dp[j] = min(dp[j], make_pair(tt, tx));
      }
    }
    printf("%.4f\\n", dp[a[n]].t);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#define INF 2000000
#define t first
#define x second
using namespace std;
typedef pair<double, int> P;
double b, v, e, f;
int r;

double mk(int x) {
  if (x >= r)
    return 1.0 / (v - e * (x - r));
  return 1.0 / (v - f * (r - x));
}

int main() {
  while (1) {
    int n, a[110];
    cin >> n;
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++)
      cin >> a[i];
    cin >> b;
    cin >> r >> v >> e >> f;
    P dp[10100] = {};
    for (int i = 1; i <= a[n]; i++)
      dp[i] = make_pair(INF, 0);

    for (int i = 0; i < n; i++) {
      int tx;
      double tt;
      // No Change
      tx = dp[a[i]].x, tt = dp[a[i]].t;
      for (int j = a[i] + 1; j <= a[n]; j++) {
        double k = mk(tx);
        tt += k, tx++;
        dp[j] = min(dp[j], make_pair(tt, tx));
      }

      // Change
      tx = 0, tt = dp[a[i]].t + b;
      for (int j = a[i] + 1; j <= a[n]; j++) {
        double k = mk(tx);
        tt += k;
        tx++;
        dp[j] = min(dp[j], make_pair(tt, tx));
      }
    }
    printf("%.4f\\n", dp[a[n]].t);
  }
  return 0;
}
Problem p00841 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
typedef ll int__;
#define rep(i, j) for (int__ i = 0; i < (int__)(j); i++)

bool solve() {
  int n;
  cin >> n;
  if (n == 0)
    return false;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  double b;
  int r;
  double v, e, f;
  cin >> b >> r >> v >> e >> f;

  vector<double> interval_time(a[n - 1] + 1);
  interval_time[0] = 0;
  rep(x, interval_time.size() - 1) {
    interval_time[x + 1] = interval_time[x];
    if (x >= r)
      interval_time[x + 1] += 1.0 / (v - e * (double)(x - r));
    else
      interval_time[x + 1] += 1.0 / (v - f * (double)(r - x));
  }

  vector<double> DP(n);
  rep(i, n) {
    DP[i] = interval_time[a[i]];
    rep(j, i) { DP[i] = min(DP[i], DP[j] + interval_time[a[i] - a[j]] + b); }
  }
  printf("%.10f\\n", DP.back());

  return true;
}

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

Error: WA

Fixed submission:

/*
  file : /1262.comp.cpp
  date : 2015-10-19
  summary :
  {
  ??\\???
  n         0< . <=100   ?????§????????????????????°
  a1 ... an 0< . <=10^4  start???????????§??????????????????????????¢
  b         0< . < 100.0 ?£??????????
  r v e f   0< . , v-e*(an-1-r)>=0.01 , v-f*r >=0.01 ?????????????????????

  ????????§????????????????????§????????§????£?????????????
  x=0 ?????? x=an ?????§???????????????????????????????±???????
  }
  status :
  {

  }
  memo :
  {

  }
 */

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef ll int__;
#define rep(i, j) for (int__ i = 0; i < (int__)(j); i++)
#define repeat(i, j, k) for (int__ i = (j); i < (int__)(k); i++)
#define all(v) v.begin(), v.end()

bool solve() {
  int n;
  cin >> n;
  if (n == 0)
    return false;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  double b, v, e, f;
  int r;
  cin >> b >> r >> v >> e >> f;

  vector<double> interval_time(a[n - 1] + 1);
  interval_time[0] = 0;
  rep(x, interval_time.size()) {
    interval_time[x + 1] = interval_time[x];
    if (x >= r)
      interval_time[x + 1] += 1.0 / (v - e * (double)(x - r));
    else
      interval_time[x + 1] += 1.0 / (v - f * (double)(r - x));
  }

  vector<double> DP(n);
  rep(i, n) {
    DP[i] = interval_time[a[i]];
    rep(j, i) { DP[i] = min(DP[i], DP[j] + interval_time[a[i] - a[j]] + b); }
  }

  printf("%.10f\\n", DP.back());

  return true;
}

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

#include <algorithm>
#include <cstdio>
#include <vector>

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

using namespace std;

int d[1500][1500];

int N, deg[1500];
void solve(int root, vector<int> V) {
  int n = V.size();
  if (V.empty())
    return;

  // root と u を両端とする列を作る
  // 残りの頂点は root-u 間にある d[root][u]+1 個の頂点のどれかにぶら下がる
  int u = V[0];
  vector<int> ch[32];
  for (int i = 1; i < V.size(); i++) {
    int v = V[i];
    ch[(d[root][u] + d[root][v] - d[u][v]) / 2].push_back(v);
  }

  // 列の各頂点を根にして, 再帰的に木の形を求める
  int par;
  rep(i, d[root][u] + 1) {
    int v; // 次の根
    if (i == 0) {
      v = root;
    } else if (i == d[root][u]) {
      v = u;
    } else {
      v = N++;
      rep(j, ch[i].size()) {
        int w = ch[i][j];
        d[v][w] = d[w][v] = d[root][w] - i;
      }
    }

    if (i != 0) {
      deg[par]++;
      deg[v]++;
    }

    solve(v, ch[i]);

    par = v;
  }
}

int main() {
  for (int n; scanf("%d", &n), n;) {
    rep(u, n) rep(v, n) scanf("%d", d[u] + v);

    vector<int> V;
    rep(i, n - 1) V.push_back(i + 1);
    N = n;
    rep(u, 1500) deg[u] = 0;
    solve(0, V);

    vector<int> ans(deg + n, deg + N);
    sort(ans.begin(), ans.end());
    rep(i, ans.size()) printf("%d%c", ans[i], i + 1 < ans.size() ? ' ' : '\\n');
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

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

using namespace std;

int d[3000][3000];

int N, deg[3000];
void solve(int root, vector<int> V) {
  int n = V.size();
  if (V.empty())
    return;

  // root と u を両端とする列を作る
  // 残りの頂点は root-u 間にある d[root][u]+1 個の頂点のどれかにぶら下がる
  int u = V[0];
  vector<int> ch[32];
  for (int i = 1; i < V.size(); i++) {
    int v = V[i];
    ch[(d[root][u] + d[root][v] - d[u][v]) / 2].push_back(v);
  }

  // 列の各頂点を根にして, 再帰的に木の形を求める
  int par;
  rep(i, d[root][u] + 1) {
    int v; // 次の根
    if (i == 0) {
      v = root;
    } else if (i < d[root][u]) {
      v = N++;
      rep(j, ch[u].size()) {
        int w = ch[u][j];
        d[v][w] = d[w][v] = d[root][w] - i;
      }
    } else {
      v = u;
    }

    if (i != 0) {
      deg[par]++;
      deg[v]++;
    }

    solve(v, ch[i]);

    par = v;
  }
}

int main() {
  for (int n; scanf("%d", &n), n;) {
    rep(u, n) rep(v, n) scanf("%d", d[u] + v);

    vector<int> V;
    rep(i, n - 1) V.push_back(i + 1);
    N = n;
    rep(u, 3000) deg[u] = 0;
    solve(0, V);

    vector<int> ans;
    rep(u, N) if (deg[u] >= 2) ans.push_back(deg[u]);
    sort(ans.begin(), ans.end());
    rep(i, ans.size()) printf("%d%c", ans[i], i + 1 < ans.size() ? ' ' : '\\n');
  }

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

// 31
#include <cmath>
#include <complex>
#include <iostream>

using namespace std;

typedef complex<double> P;

P p[50];
int n;

double cr(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }

bool in(P a, P b, P c, P d) {
  bool f[2] = {};
  for (int i = 0; i < 3; i++) {
    P pa[] = {b, c, d};
    double c = cr(a - pa[i], pa[i + 1] - pa[i]);
    if (fabs(c) > 1e-9) {
      f[c > 0] = true;
    }
  }
  return !(f[0] && f[1]);
}

bool ip(P cp) {
  bool f = false;
  for (int px = 0; px < n; px++) {
    int nx = (px + 1) % n;
    f |= cr(p[nx] - cp, p[px] - cp) > 1e-9;
  }
  return !f;
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      p[i] = P(x, y);
    }
    int i;
    for (i = 0; i < n; i++) {
      for (int j = 0; j < i; j++) {
        for (int k = 0; k < n; k++) {
          for (int l = 0; l < k; l++) {
            if (fabs(cr(p[i] - p[j], p[k] - p[l])) < 1e-9)
              continue;
            double a = cr(p[k] - p[l], p[i] - p[l]);
            double b = cr(p[k] - p[l], p[j] - p[l]);
            P ic = p[i] + (p[j] - p[i]) * a / (a - b);
            int m;
            for (m = 0; m < n; m++) {
              if (!in(ic, p[m], p[(m + 1) % n], p[(m + 2) % n]))
                break;
            }
            if (m != n && ip(ic))
              goto end;
          }
        }
      }
    }
  end:
    cout << (i != n) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 31
#include <cmath>
#include <complex>
#include <iostream>

using namespace std;

typedef complex<double> P;

P p[50];
int n;

double cr(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }

bool in(P a, P b, P c, P d) {
  bool f[2] = {};
  for (int i = 0; i < 3; i++) {
    P pa[] = {b, c, d};
    double c = cr(a - pa[i], pa[i + 1] - pa[i]);
    if (fabs(c) > 1e-9) {
      f[c > 0] = true;
    }
  }
  return !(f[0] && f[1]);
}

bool ip(P cp) {
  bool f = false;
  for (int px = 0; px < n; px++) {
    int nx = (px + 1) % n;
    f |= cr(p[nx] - cp, p[px] - cp) > 1e-9;
  }
  return !f;
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      p[i] = P(x, y);
    }
    int i;
    for (i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          for (int l = 0; l < n; l++) {
            if (fabs(cr(p[i] - p[j], p[k] - p[l])) < 1e-9)
              continue;
            double a = cr(p[k] - p[l], p[i] - p[l]);
            double b = cr(p[k] - p[l], p[j] - p[l]);
            P ic = p[i] + (p[j] - p[i]) * a / (a - b);
            int m;
            for (m = 0; m < n; m++) {
              if (!in(ic, p[m], p[(m + 1) % n], p[(m + 2) % n]))
                break;
            }
            if (m != n && ip(ic))
              goto end;
          }
        }
      }
    }
  end:
    cout << (i != n) << endl;
  }
  return 0;
}
Problem p00848 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> PP;

vector<int> primes;
const int maxn = 1121, maxk = 15;
map<PP, int> memo;
// int memo[maxn][maxk][maxn];
int l;

int rec(int n, int k, int p) {
  //  printf("%d %d %d\\n", n, k ,p);
  if (k < 0)
    return 0;
  if (n == 0 && k == 0)
    return 1;
  PP tri = PP(P(n, k), p);
  if (memo.count(tri) == 0) {
    int res = 0;
    for (int i = 0; i < l && primes[i] < p && primes[i] <= n; i++) {
      res += rec(n - primes[i], k - 1, primes[i]);
    }
    memo[tri] = res;
  }
  return memo[tri];
}

int main() {
  vector<bool> nonprime(maxn, false);
  for (int i = 2; i < maxn; i++) {
    if (!nonprime[i]) {
      primes.push_back(i);
      for (int j = i + i; j < maxn; j += i)
        nonprime[j] = true;
    }
  }
  l = primes.size();
  //  fill_n(memo[0][0], maxn * maxn * maxk, -1);
  //  for (int i = 0; i < maxn; i++)
  //	memo[0][0][i] = 1;

  while (true) {
    int n, k;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    cout << rec(n, k, n + 1) << endl;
  }
}

Error: MLE

Fixed submission:

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

using namespace std;

vector<int> primes;
const int maxn = 1121, maxk = 15;
int memo[maxn][maxk][maxn];
int l;

int rec(int n, int k, int p) {
  //  printf("%d %d %d\\n", n, k ,p);
  if (k < 0)
    return 0;
  if (memo[n][k][p] < 0) {
    int res = 0;
    for (int i = 0; i < l && primes[i] < p && primes[i] <= n; i++) {
      res += rec(n - primes[i], k - 1, primes[i]);
    }
    memo[n][k][p] = res;
  }
  return memo[n][k][p];
}

int main() {
  vector<bool> nonprime(maxn, false);
  for (int i = 2; i < maxn; i++) {
    if (!nonprime[i]) {
      primes.push_back(i);
      for (int j = i + i; j < maxn; j += i)
        nonprime[j] = true;
    }
  }
  l = primes.size();
  fill_n(memo[0][0], maxn * maxn * maxk, -1);
  for (int i = 0; i < maxn; i++)
    memo[0][0][i] = 1;

  while (true) {
    int n, k;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    cout << rec(n, k, n + 1) << endl;
  }
}
Problem p00848 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

vector<int> sieve_of_eratosthenes(int n) {
  vector<int> primes(n);
  for (int i = 2; i < n; ++i)
    primes[i] = i;
  for (int i = 2; i * i < n; ++i)
    if (primes[i])
      for (int j = i * i; j < n; j += i)
        primes[j] = 0;
  return primes;
}

int dp[15][200][1121];

int main() {
  vector<int> a = sieve_of_eratosthenes(1121);
  vector<int> prime;
  for (int i = 0; i < a.size(); i++)
    if (a[i] != 0)
      prime.push_back(a[i]);

  int n, k;
  while (cin >> n >> k && n != 0) {
    memset(dp, 0, sizeof(dp));

    dp[0][0][0] = 1;

    for (int i = 0; i < k; i++) {
      for (int j = 0; j < prime.size(); j++) {
        for (int num = 0; num <= n; num++) {
          if (dp[i][j][num] == 0)
            continue;
          for (int next = j; next < prime.size(); next++) {
            if (num + prime[next] > n)
              break;
            dp[i + 1][next + 1][num + prime[next]] += dp[i][j][num];
          }
        }
      }
    }
    int ans = 0;
    for (int i = 0; i <= prime.size(); i++) {
      ans += dp[k][i][n];
    }
    cout << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

vector<int> sieve_of_eratosthenes(int n) {
  vector<int> primes(n);
  for (int i = 2; i < n; ++i)
    primes[i] = i;
  for (int i = 2; i * i < n; ++i)
    if (primes[i])
      for (int j = i * i; j < n; j += i)
        primes[j] = 0;
  return primes;
}

int dp[15][1121][1121];

int main() {
  vector<int> a = sieve_of_eratosthenes(1121);
  vector<int> prime;
  for (int i = 0; i < a.size(); i++)
    if (a[i] != 0)
      prime.push_back(a[i]);

  int n, k;
  while (cin >> n >> k && n != 0) {
    memset(dp, 0, sizeof(dp));

    dp[0][0][0] = 1;

    for (int i = 0; i < k; i++) {
      for (int j = 0; j < prime.size(); j++) {
        for (int num = 0; num <= n; num++) {
          if (dp[i][j][num] == 0)
            continue;
          for (int next = j; next < prime.size(); next++) {
            if (num + prime[next] > n)
              break;
            dp[i + 1][next + 1][num + prime[next]] += dp[i][j][num];
          }
        }
      }
    }
    int ans = 0;
    for (int i = 0; i <= prime.size(); i++) {
      ans += dp[k][i][n];
    }
    cout << ans << endl;
  }

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

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

using namespace std;

typedef long long int lli;

lli memo[1121][15][188];
vector<int> prm;
int l;

lli func(int n, int k, int p, int t, int c) {
  if (n == t && k == c) {
    return 1;
  }
  if (n == t) {
    return 0;
  }
  if (k == c) {
    return 0;
  }
  if (p == l) {
    return 0;
  }
  if (memo[t][c][p] >= 0) {
    return memo[t][c][p];
  }
  lli u = 0;
  for (int i = p; i < l; i++) {
    if ((t + prm[i]) > n) {
      break;
    }
    u += func(n, k, i + 1, t + prm[i], c + 1);
  }
  memo[t][c][p] = u;

  return u;
}

int main() {
  int n, k;
  bool a[1121];
  fill(a, a + 1121, true);
  a[0] = a[1] = false;

  for (int i = 2; i < 1121; i++) {
    if (a[i]) {
      prm.push_back(i);
      for (int j = i + i; j < 1121; j += i) {
        a[j] = false;
      }
    }
  }
  l = prm.size();

  while (cin >> n >> k && (k + n)) {
    fill(&memo[0][0][0], &memo[1120][14][187], -1);
    cout << func(n, k, 0, 0, 0) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

typedef long long int lli;

lli memo[1121][15][188];

lli func(vector<int> prm, int n, int k, int c, int l, int t) {
  if (k == c && t == n) {
    return 1;
  }
  if (k == c) {
    return 0;
  }
  if (t >= n) {
    return 0;
  }
  if (l < 0) {
    return 0;
  }
  if (memo[t][c][l] >= 0) {
    return memo[t][c][l];
  }

  lli u = 0;
  for (int i = l; i >= 0; i--) {
    if (t + prm[i] <= n) {
      u += func(prm, n, k, c + 1, i - 1, t + prm[i]);
    }
  }
  memo[t][c][l] = u;

  return u;
}

int main() {
  int n, k;
  vector<int> prm;
  bool a[1121];
  fill(a, a + 1121, true);
  a[0] = a[1] = false;

  for (int i = 2; i < 1121; i++) {
    if (a[i]) {
      prm.push_back(i);
      for (int j = i + i; j < 1121; j += i) {
        a[j] = false;
      }
    }
  }

  while (cin >> n >> k && (k + n)) {
    fill(&memo[0][0][0], &memo[1120][14][187], -1);
    cout << func(prm, n, k, 0, prm.size() - 1, 0) << endl;
  }

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

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

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

int dp[1130][15][200];
bool flg[1130];
vector<int> p;

void mkPrime(void) {
  memset(flg, 1, sizeof(flg));
  flg[0] = flg[1] = 0;

  REP(i, 2, 1121) {
    if (flg[i]) {
      for (int j = i * 2; j <= 1120; j += i) {
        flg[j] = false;
      }
    }
  }
}

int rec(int n, int k, int idx) {
  if (n == 0 && k == 0)
    return 1;
  if (n < 0 || k < 0)
    return 0;
  if (dp[n][k][idx] != -1)
    return dp[n][k][idx];

  int res = 0;
  for (int i = idx; p[i] <= n; i++) {
    res += rec(n - p[i], k - 1, i + 1);
  }
  return dp[n][k][idx] = res;
}

int main(void) {
  memset(dp, -1, sizeof(dp));
  mkPrime();
  rep(i, 1130) if (flg[i]) p.push_back(i);

  int n, k;
  while (cin >> n >> k && (n || k)) {
    cout << rec(n, k, 0) << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

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

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

int dp[1130][15][1130];
bool flg[1130];
vector<int> p;

void mkPrime(void) {
  memset(flg, 1, sizeof(flg));
  flg[0] = flg[1] = 0;

  REP(i, 2, 1121) {
    if (flg[i]) {
      for (int j = i * 2; j <= 1120; j += i) {
        flg[j] = false;
      }
    }
  }
}

int rec(int n, int k, int idx) {
  if (n == 0 && k == 0)
    return 1;
  if (n < 0 || k < 0)
    return 0;
  if (dp[n][k][idx] != -1)
    return dp[n][k][idx];

  int res = 0;
  for (int i = idx; p[i] <= n; i++) {
    res += rec(n - p[i], k - 1, i + 1);
  }
  return dp[n][k][idx] = res;
}

int main(void) {
  memset(dp, -1, sizeof(dp));
  mkPrime();
  rep(i, 1121) if (flg[i]) p.push_back(i);

  int n, k;
  while (cin >> n >> k && (n || k)) {
    cout << rec(n, k, 0) << endl;
  }

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

// 37
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int cmp[1121] = {};
  vector<int> prm;
  for (int i = 2; i <= 1120; i++) {
    if (!cmp[i]) {
      prm.push_back(i);
      for (int j = 2; i * j <= 1120; j++) {
        cmp[i * j] = true;
      }
    }
  }
  static int dp[1121][200][15];
  dp[0][0][0] = 1;
  for (int i = 0; i < 1121; i++) {
    for (int j = 0; j <= prm.size(); j++) {
      for (int l = j + 1; l <= prm.size(); l++) {
        for (int k = 0; k < 14; k++) {
          if (i + prm[l - 1] <= 1120) {
            dp[i + prm[l - 1]][l][k + 1] += dp[i][j][k];
          }
        }
      }
    }
  }
  for (int n, k; cin >> n >> k, n | k;) {
    int a = 0;
    for (int i = 0; i <= prm.size(); i++) {
      a += dp[n][i][k];
    }
    cout << a << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

// 37
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int cmp[1121] = {};
  vector<int> prm;
  for (int i = 2; i <= 1120; i++) {
    if (!cmp[i]) {
      prm.push_back(i);
      for (int j = 2; i * j <= 1120; j++) {
        cmp[i * j] = true;
      }
    }
  }
  static int dp[1121][1000][15];
  dp[0][0][0] = 1;
  for (int i = 0; i < 1121; i++) {
    for (int j = 0; j <= prm.size(); j++) {
      for (int l = j + 1; l <= prm.size(); l++) {
        for (int k = 0; k < 14; k++) {
          if (i + prm[l - 1] <= 1120) {
            dp[i + prm[l - 1]][l][k + 1] += dp[i][j][k];
          }
        }
      }
    }
  }
  for (int n, k; cin >> n >> k, n | k;) {
    int a = 0;
    for (int i = 0; i <= prm.size(); i++) {
      a += dp[n][i][k];
    }
    cout << a << endl;
  }
  return 0;
}