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

#include <iostream>
using namespace std;

int main() {
  int N;
  int tri[200];
  static int ansA[1000000], ansB[1000000];
  /* ??£?????¢?????°?¨???? */
  for (int i = 0; i < 200; i++)
    tri[i] = i * (i + 1) * (i + 2) / 6;

  /* ??????1,000,000?????§?????????????¨???????????????? */
  ansA[0] = ansB[0] = 0;
  for (int i = 1; i < 1000000; i++) {
    ansA[i] = ansB[i] = i; /* ????????????i???????????? */
    for (int j = 1; i - tri[j] >= 0; j++) {
      /* ???????¨?????????§???????????£?????¢????????°????????????????¢????????????????
       */
      ansA[i] = min(ansA[i], ansA[i - tri[j]] + 1);
      /* ?\\???°???????????´??? */
      if (tri[j] % 2 == 1)
        ansB[i] = min(ansB[i], ansB[i - tri[j]] + 1);
    }
  }

  /* input?????????????????????????????? */
  while (cin >> N, N)
    cout << ansA[N] << " " << ansB[N] << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main(void) {

  int i, input, tetras[200] = {0}, ans[100][100] = {{0}, {0}};

  for (i = 1; i <= 180; i++) {
    tetras[i] = i * (i + 1) * (i + 2) / 6;
  }

  int a = 0;
  while (cin >> input) {
    if (input == 0)
      break;
    int temp = input;
    int j = 1, min = 1000000000;
    while (tetras[j] <= input) {
      int n = 0;
      int k = j;
      while (input > 0) {
        input -= tetras[k];
        if (input >= 0) {
          n++;
        } else {
          input += tetras[k];
          k--;
          continue;
        }
      }
      if (min > n)
        min = n;
      j++;
      input = temp;
    }
    ans[a][0] = min;
    j = 1, min = 1000000000;
    while (tetras[j] <= input) {
      int n = 0;
      int k = j;
      while (input > 0) {
        if (tetras[k] % 2 == 0) {
          k--;
          continue;
        }
        input -= tetras[k];
        if (input >= 0) {
          n++;
        } else {
          input += tetras[k];
          k--;
          continue;
        }
      }
      if (min > n)
        min = n;
      j++;
      input = temp;
    }
    ans[a][1] = min;
    a++;
  }

  int b;
  for (b = 0; b < a; b++) {
    cout << ans[b][0] << ' ' << ans[b][1] << endl;
  }

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

// 2013/06/29 Tazoe

#include <iostream>
using namespace std;

int main() {
  int DP[1000000];
  int DP2[1000000];
  DP[0] = 0;
  DP2[0] = 0;
  for (int i = 1; i < 1000000; i++) {
    DP[i] = 1000000;
    DP2[i] = 1000000;
  }

  for (int i = 1;; i++) {
    int p = i * (i + 1) * (i + 2) / 6;

    if (p >= 1000000)
      break;

    DP[p] = 1;

    if (p % 2 == 1) {
      DP2[p] = 1;
    }
  }
  /*
          for(int i=0; i<P.size(); i++){
                  cout << P[i] << endl;
          }
  */
  for (int i = 0; i < 1000000; i++) {
    if (DP[i] != 1)
      continue;

    for (int j = 0; j + i < 1000000; j++) {
      if (DP[j] + 1 < DP[j + i]) {
        DP[j + i] = DP[j] + 1;
      }

      if (DP2[i] == 1 && DP2[j] + 1 < DP2[j + i]) {
        DP2[j + i] = DP2[j] + 1;
      }
    }
  }

  while (true) {
    int n;
    cin >> n;

    if (n == 0)
      break;

    cout << DP[n] << ' ' << DP2[n] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// 2013/06/29 Tazoe

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

int main() {
  vector<int> P;
  P.push_back(1);

  for (int i = 0;; i++) {
    int p = P[i] * (i + 4) / (i + 1);

    if (p >= 1000000)
      break;

    P.push_back(p);
  }
  /*
          for(int i=0; i<P.size(); i++){
                  cout << P[i] << endl;
          }
  */
  int DP[1000000];
  int DP2[1000000];
  DP[0] = 0;
  DP2[0] = 0;
  for (int i = 1; i < 1000000; i++) {
    DP[i] = 1000000;
    DP2[i] = 1000000;
  }

  for (int i = 0; P[i] < 1000000; i++) {
    for (int j = 0; j + P[i] < 1000000; j++) {
      if (DP[j] + 1 < DP[j + P[i]]) {
        DP[j + P[i]] = DP[j] + 1;
      }

      if (P[i] % 2 == 1 && DP2[j] + 1 < DP2[j + P[i]]) {
        DP2[j + P[i]] = DP2[j] + 1;
      }
    }
  }

  while (true) {
    int n;
    cin >> n;

    if (n == 0)
      break;

    cout << DP[n] << ' ' << DP2[n] << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

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

const int size = 200;
const int limit = 1000001;
int dp[limit], dp_odd[limit];
int tetra[size], odd[size];

int main(void) {
  int size_odd = 0;
  for (int i = 1; i < size + 1; ++i) {
    ll t = i * (i + 1) * (i + 2) / 6;
    tetra[i - 1] = t;
    if (t % 2 == 1) {
      odd[size_odd] = t;
      size_odd++;
    }
  }
  int inf = numeric_limits<int>::max();
  rep(i, limit) dp[i] = dp_odd[i] = inf;
  dp[0] = 0;
  dp_odd[0] = 0;
  rep(i, size) {
    for (int j = tetra[i]; j < limit; ++j)
      dp[j] = min(dp[j - tetra[i]] + 1, dp[j]);
  }
  rep(i, size_odd) {
    for (int j = odd[i]; j < limit; ++j)
      dp_odd[j] = min(dp_odd[j - odd[i]] + 1, dp_odd[j]);
  }

  int n;
  while (cin >> n && n) {
    cout << dp[n] << " ";
    cout << dp_odd[n] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

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

const int limit = 200;
int tetra[limit];
int odd[limit];
int ans(int n, int index, int *a) {
  if (n < a[1] || index == 0)
    return n;
  int i;
  for (i = index; i > 0; --i) {
    if (a[i] <= n)
      break;
  }
  return min(1 + ans(n - a[i], i, a), ans(n, i - 1, a));
}

int main(void) {
  int size = 0;
  int inf = numeric_limits<int>::max();
  rep(i, limit) odd[i] = inf;
  for (int i = 1; i < limit + 1; ++i) {
    ll t = i * (i + 1) * (i + 2) / 6;
    tetra[i - 1] = t;
    if (t % 2 == 1) {
      odd[size] = t;
      size++;
    }
  }
  int n;
  while (cin >> n && n) {
    cout << ans(n, limit - 1, tetra) << " ";
    cout << ans(n, limit - 1, odd) << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

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

int main() {
  ll s4[201];
  vector<ll> s4_k;
  REP(i, 201) {
    s4[i] = i * (i + 1) * (i + 2) / 6;
    if (s4[i] & 1) {
      s4_k.push_back(i * (i + 1) * (i + 2) / 6);
      // cout << s4[i] << endl;
    }
  }
  vector<ll> dp1(1000001, INF), dp2(1000001, INF);
  dp1[0] = 0;
  REP(i, 201) {
    REP(j, 1000001) {
      if (s4[i] <= j) {
        dp1[j] = min(dp1[j], dp1[j - s4[i]] + 1);
      }
    }
  }

  dp2[0] = 0;
  REP(i, s4_k.size()) {
    REP(j, 1000001) {
      if (s4_k[i] <= j) {
        dp2[j] = min(dp2[j], dp2[j - s4_k[i]] + 1);
      }
    }
  }

  ll n;
  cin >> n;

  while (n != 0) {
    cout << dp1[n] << ' ' << dp2[n] << endl;

    cin >> n;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

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

int main() {
  ll s4[101];
  vector<ll> s4_k;
  s4[0] = 1;
  for (int i = 1; i < 101; i++) {
    s4[i] = i * (i + 1) * (i + 2) / 6;
    if (s4[i] & 1) {
      s4_k.push_back(i * (i + 1) * (i + 2) / 6);
      // cout << s4[i] << endl;
    }
  }
  vector<ll> dp1(100001, INF), dp2(100001, INF);
  dp1[0] = 0;
  REP(i, 101) {
    REP(j, 100001) {
      if (s4[i] <= j) {
        dp1[j] = min(dp1[j], dp1[j - s4[i]] + 1);
      }
    }
  }

  dp2[0] = 0;
  REP(i, s4_k.size()) {
    REP(j, 100001) {
      if (s4_k[i] <= j) {
        dp2[j] = min(dp2[j], dp2[j - s4_k[i]] + 1);
      }
    }
  }

  ll n;
  cin >> n;
  while (n != 0) {
    cout << dp1[n] << ' ' << dp2[n] << endl;

    cin >> n;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MAX_N = 1000001;
const int INF = 1 << 27;
vector<int> N3, N4;
int T[2][MAX_N];

void makeN4() {
  for (int n = 1;; ++n) {
    int s = n * (n + 1) * (n + 2) / 6;
    if (s >= MAX_N)
      break;
    N4.push_back(s);
  }
}

void solve(bool oddOnly) {
  int *t = T[oddOnly];
  fill(t, t + MAX_N, INF);
  t[0] = 0;

  for (int j = 0; j < N4.size(); ++j) {
    if (oddOnly && N4[j] % 2 == 0)
      continue;
    for (int i = N4[j]; i < MAX_N; ++i) {
      t[i] = min(t[i], t[i - N4[j]] + 1);
    }
  }
  /*
  for(int i = 1; i < MAX_N; ++i) {
    for(int j = 0; j < N4.size() && N4[j] <= i; ++j) {
      if(oddOnly && N4[j]%2 == 0) continue;
      t[i] = min(t[i], t[i-N4[j]] + 1);
    }
  }
  */
}

int main() {
  makeN4();
  solve(false);
  solve(true);
  int N;
  while (cin >> N && N) {
    cout << T[0][N] << " " << T[1][N] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MAX_N = 1000001;
const int INF = 1 << 27;
vector<int> N3, N4;
int T[MAX_N];

void makeN3() {
  N3.push_back(0);
  for (int i = 1, s = 1; s <= MAX_N; s += ++i) {
    N3.push_back(s);
  }
}

void makeN4() {
  N4.push_back(0);
  for (int i = 1, s = N3[1]; i < N3.size() && s <= MAX_N; s += N3[++i]) {
    N4.push_back(s);
  }
}

int solve(int N, bool oddOnly) {
  fill(T, T + N + 1, INF);
  T[0] = 0;
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j < N4.size() && N4[j] <= i; ++j) {
      if (oddOnly && N4[j] % 2 == 0)
        continue;
      T[i] = min(T[i], T[i - N4[j]] + 1);
    }
  }
  return T[N];
}

int main() {
  int N;
  makeN3();
  makeN4();
  while (cin >> N && N) {
    cout << solve(N, false) << " " << solve(N, true) << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define INF 100000000

int main() {
  int table1[201] = {};
  vector<int> table2;
  for (int i = 1; i < 201; i++) {
    table1[i - 1] = i * (i + 1) * (i + 2) / 6;
    if (table1[i - 1] % 2 != 0) {
      table2.push_back(table1[i - 1]);
    }
  }
  int count1[1000001];
  int count2[1000001];
  for (int i = 0; i < 1000001; i++) {
    count1[i] = INF;
    count2[i] = INF;
  }
  count1[0] = 0;
  count2[0] = 0;
  count1[1] = 1;
  count2[1] = 1;
  for (int i = 2; i < 1000001; i++) {
    for (int j = 0; j < 201; j++) {
      if (table1[j] > i)
        break;
      count1[i] = min(count1[i], count1[i - table1[j]] + 1);
    }
  }
  for (int i = 2; i < 1000001; i++) {
    for (int j = 0; j < 201; j++) {
      if (table2[j] > i)
        break;
      count2[i] = min(count2[i], count2[i - table2[j]] + 1);
    }
  }
  int n;
  while (cin >> n && n != 0) {
    cout << count1[n] << " " << count2[n] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define INF 100000000

int main() {
  int table1[201] = {};
  vector<int> table2;
  for (int i = 1; i < 201; i++) {
    table1[i - 1] = i * (i + 1) * (i + 2) / 6;
    if (table1[i - 1] % 2 != 0) {
      table2.push_back(table1[i - 1]);
    }
  }
  int count1[100001];
  int count2[100001];
  for (int i = 0; i < 100001; i++) {
    count1[i] = INF;
    count2[i] = INF;
  }
  count1[0] = 0;
  count2[0] = 0;
  count1[1] = 1;
  count2[1] = 1;
  for (int i = 2; i < 100001; i++) {
    for (int j = 0; j < 201; j++) {
      if (table1[j] > i)
        break;
      count1[i] = min(count1[i], count1[i - table1[j]] + 1);
    }
  }
  for (int i = 2; i < 100001; i++) {
    for (int j = 0; j < 201; j++) {
      if (table2[j] > i)
        break;
      count2[i] = min(count2[i], count2[i - table2[j]] + 1);
    }
  }
  int n;
  while (cin >> n && n != 0) {
    cout << count1[n] << " " << count2[n] << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const double pi = 2 * acos(0.0);
const double eps = 1e-8;

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int Cost;
struct Edge {
  int src, dst;
  Cost cost;
  Edge(int s, int d, Cost c) : src(s), dst(d), cost(c) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Cost> Array;
typedef vector<Array> Matrix;

int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};

#define MAX_N 1000000
int memo[MAX_N + 1];
int memo_odd[MAX_N + 1];

int plc(int n) { return n * (n + 1) * (n + 2) / 6; }

void create_table() {
  fill(memo, memo + MAX_N + 1, 1 << 30);
  fill(memo_odd, memo_odd + MAX_N + 1, 1 << 30);
  for (int i = 1; plc(i) <= MAX_N + 1; i++) {
    memo[plc(i)] = 1;
    if (plc(i) % 2 == 0)
      continue;
    memo_odd[plc(i)] = 1;
  }
  for (int i = 1; i <= MAX_N; i++) {
    for (int j = 1; plc(j) <= i; j++) {
      memo[i] = min(memo[i], memo[i - plc(j)] + 1);
      if (plc(j) % 2 == 0)
        continue;
      memo_odd[i] = min(memo_odd[i], memo_odd[i - plc(j)] + 1);
    }
  }
}

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  create_table();
  while (cin >> n, n) {
    cout << memo[n] << " " << memo_odd[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

const double pi = 2 * acos(0.0);
const double eps = 1e-8;

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int Cost;
struct Edge {
  int src, dst;
  Cost cost;
  Edge(int s, int d, Cost c) : src(s), dst(d), cost(c) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Cost> Array;
typedef vector<Array> Matrix;

int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};

#define MAX_N 1000000
int memo[MAX_N + 1];

int plc(int n) { return n * (n + 1) * (n + 2) / 6; }

int solve(int n, bool skip_even) {
  fill(memo, memo + MAX_N + 1, 1 << 30);
  for (int i = 1; plc(i) <= n; i++) {
    if (skip_even && plc(i) % 2 == 0)
      continue;
    memo[plc(i)] = 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; plc(j) <= i; j++) {
      if (skip_even && plc(j) % 2 == 0)
        continue;
      memo[i] = min(memo[i], memo[i - plc(j)] + 1);
    }
  }
  return memo[n];
}

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  while (cin >> n, n) {
    cout << solve(n, false) << " " << solve(n, true) << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<P, P> P2;
const int INF = 1 << 30;
const double EPS = 1E-9;

int c[1000000];
int co[1000000];
int po[300];

int main() {
  int n;
  fill(c, c + 1000000, INF);
  fill(co, co + 1000000, INF);
  c[0] = co[0] = 0;
  for (int i = 0; i < 300; i++) {
    po[i] = i * (i + 1) * (i + 2) / 6;
  }
  for (int i = 0; i < 1000000; i++) {
    for (int j = 1; j < 300; j++) {
      if (i + po[j] >= 1000000)
        break;
      if (c[i + po[j]] > c[i] + 1) {
        c[i + po[j]] = c[i] + 1;
      }
    }
  }
  for (int i = 0; i < 1000000; i++) {
    for (int j = 1; j < 300; j++) {
      if (!(po[j] & 1))
        continue;
      if (i + po[j] >= 1000000)
        break;
      if (co[i + po[j]] > co[i] + 1) {
        co[i + po[j]] = co[i] + 1;
      }
    }
  }
  while (cin >> n && n) {
    cout << c[n] << " " << co[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

const int INF = 2140000000;
int nums[1000000];
int nums_k[1000000];

int main() {
  int n;
  int p;
  queue<int> q, q_k;
  for (int i = 0; i < 1000000; i++) {
    nums_k[i] = INF;
    nums[i] = INF;
  }
  for (int i = 0; i * (i + 1) * (i + 2) / 6 < 1000000; i++) {
    int j = i * (i + 1) * (i + 2) / 6;
    nums[j] = 1;
    q.push(j);
    if (j % 2 == 1) {
      q_k.push(j);
      nums_k[j] = 1;
    }
  }
  while (!q.empty()) {
    p = q.front();
    q.pop();
    for (int i = 1; i * (i + 1) * (i + 2) / 6 < 1000000 - p; i++) {
      int j = i * (i + 1) * (i + 2) / 6;
      if (nums[p + j] == INF) {
        nums[p + j] = nums[p] + 1;
        q.push(p + j);
      }
    }
  }
  while (!q_k.empty()) {
    p = q_k.front();
    q_k.pop();
    for (int i = 1; i * (i * 1) * (i + 2) / 6 < 1000000 - p; i++) {
      int j = i * (i + 1) * (i + 2) / 6;
      if (j % 2 == 1 && nums_k[p + j] == INF) {
        nums_k[p + j] = nums_k[p] + 1;
        q_k.push(p + j);
      }
    }
  }
  while (cin >> n) {
    if (n == 0)
      break;
    cout << nums[n] << " " << nums_k[n] << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define INF 1 << 28
using namespace std;

int p[222];
int memo[1000001];
int memo2[1000001];

int solve(int n) {

  if (!n)
    return 0;
  if (memo[n])
    return memo[n];

  int ans = INF;
  for (int i = 1; p[i] <= n; i++)
    ans = min(ans, solve(n - p[i]) + 1);

  return memo[n] = ans;
}

int solve2(int n) {

  if (!n)
    return 0;
  if (memo2[n])
    return memo2[n];

  int ans = INF;
  for (int i = 1; p[i] <= n; i++) {
    if (p[i] & 1)
      ans = min(ans, solve2(n - p[i]) + 1);
  }

  return memo2[n] = ans;
}

int main() {

  int n;

  for (int i = 0; i <= 181; i++)
    p[i] = i * (i + 1) * (i + 2) / 6;
  for (int i = 1; i <= 1000000; i += 100)
    solve(i), solve2(i);

  while (cin >> n, n)
    cout << solve(n) << " " << solve2(n) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#define INF 1 << 28
using namespace std;

int p[222];
int memo[1000001];
int memo2[1000001];

int solve(int n) {

  if (!n)
    return 0;
  if (memo[n])
    return memo[n];

  int ans = INF;
  for (int i = 1; p[i] <= n; i++)
    ans = min(ans, solve(n - p[i]) + 1);

  return memo[n] = ans;
}

int solve2(int n) {

  if (!n)
    return 0;
  if (memo2[n])
    return memo2[n];

  int ans = INF;
  for (int i = 1; p[i] <= n; i++) {
    if (p[i] & 1)
      ans = min(ans, solve2(n - p[i]) + 1);
  }

  return memo2[n] = ans;
}

int main() {

  int n;

  for (int i = 0; i <= 181; i++)
    p[i] = i * (i + 1) * (i + 2) / 6;
  // for(int i=1;i<=1000000;i++) solve(i), solve2(i);

  while (cin >> n, n)
    cout << solve(n) << " " << solve2(n) << endl;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define INF 1 << 28
using namespace std;

int p[1000001];
int memo[1000001];
int memo2[1000001];

int solve(int n) {

  // cout << n << endl;

  if (!n)
    return 0;
  if (n < 0)
    return INF;
  if (memo[n])
    return memo[n];

  int ans = INF;
  for (int i = 1; p[i] <= n; i++)
    ans = min(ans, solve(n - p[i]) + 1);

  return memo[n] = ans;
}

int solve2(int n) {

  if (!n)
    return 0;
  if (n < 0)
    return INF;
  if (memo2[n])
    return memo2[n];

  int ans = INF;
  for (int i = 1; p[i] <= n; i++) {
    if (!(p[i] % 2))
      continue;
    ans = min(ans, solve2(n - p[i]) + 1);
  }

  return memo2[n] = ans;
}

int main() {

  int n;

  for (int i = 0; i <= 1000000; i++)
    p[i] = i * (i + 1) * (i + 2) / 6;
  for (int i = 1; i <= 1000000; i++)
    solve(i), solve2(i);

  while (cin >> n, n)
    cout << solve(n) << " " << solve2(n) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#define INF 1 << 28
using namespace std;

int p[222];
int memo[1111111];
int memo2[1111111];

int solve(int n) {

  if (!n)
    return 0;
  if (~memo[n])
    return memo[n];

  int ans = INF;
  for (int i = 1; i < 180; i++)
    if (n >= p[i])
      ans = min(ans, solve(n - p[i]) + 1);

  return memo[n] = ans;
}

int solve2(int n) {

  if (!n)
    return 0;
  if (n < 0)
    return INF;
  if (~memo2[n])
    return memo2[n];

  int ans = INF;
  for (int i = 1; i < 180; i++)
    if (p[i] & 1 && n >= p[i])
      ans = min(ans, solve2(n - p[i]) + 1);

  return memo2[n] = ans;
}

int main() {

  int n;

  memset(memo, -1, sizeof(memo));
  memset(memo2, -1, sizeof(memo2));

  for (int i = 0; i < 181; i++)
    p[i] = i * (i + 1) * (i + 2) / 6;
  for (int i = 0; i < 111111; i++) {
    solve(i), solve2(i);
  }

  while (cin >> n, n)
    cout << solve(n) << " " << solve2(n) << endl;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#define INF 1 << 28
using namespace std;

int p[222];
int memo[1111111];
int memo2[1111111];

int solve(int n) {

  if (!n)
    return 0;
  if (~memo[n])
    return memo[n];

  int ans = INF;
  for (int i = 180; i > 0; i--)
    if (n >= p[i])
      ans = min(ans, solve(n - p[i]) + 1);

  return memo[n] = ans;
}

int solve2(int n) {

  if (!n)
    return 0;
  if (n < 0)
    return INF;
  if (~memo2[n])
    return memo2[n];

  int ans = INF;
  for (int i = 180; i > 0; i--)
    if (p[i] & 1 && n >= p[i])
      ans = min(ans, solve2(n - p[i]) + 1);

  return memo2[n] = ans;
}

int main() {

  int n;

  memset(memo, -1, sizeof(memo));
  memset(memo2, -1, sizeof(memo2));

  for (int i = 0; i < 181; i++)
    p[i] = i * (i + 1) * (i + 2) / 6;
  while (cin >> n, n)
    cout << solve(n) << " " << solve2(n) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#define INF 1 << 28
using namespace std;

int p[222];
int memo[1111111];
int memo2[1111111];

int solve(int n) {

  if (!n)
    return 0;
  if (~memo[n])
    return memo[n];

  int ans = INF;
  for (int i = 1; i < 181; i++)
    if (n >= p[i])
      ans = min(ans, solve(n - p[i]) + 1);

  return memo[n] = ans;
}

int solve2(int n) {

  if (!n)
    return 0;
  if (n < 0)
    return INF;
  if (~memo2[n])
    return memo2[n];

  int ans = INF;
  for (int i = 1; i < 181; i++)
    if (p[i] & 1 && n >= p[i])
      ans = min(ans, solve2(n - p[i]) + 1);

  return memo2[n] = ans;
}

int main() {

  int n;

  memset(memo, -1, sizeof(memo));
  memset(memo2, -1, sizeof(memo2));

  for (int i = 0; i < 181; i++)
    p[i] = i * (i + 1) * (i + 2) / 6;
  while (cin >> n, n)
    cout << solve(n) << " " << solve2(n) << endl;
}