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

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

#define INF (1 << 28)

int t[1000 * 1000 + 1];
int ot[1000 * 1000 + 1];

void init() {
  fill(t, t + 1000 * 1000 + 1, INF);
  fill(ot, ot + 1000 * 1000 + 1, INF);
  for (int i = 1;; i++) {
    int n = i * (i + 1) * (i + 2) / 6;
    if (n > 1000 * 1000)
      break;
    t[n] = 1;
    if (n % 2)
      ot[n] = 1;
  }

  for (int i = 1; i <= 1000 * 1000; i++) {
    for (int j = 1;; j++) {
      int p = j * (j + 1) * (j + 2) / 6;
      if (i <= p)
        break;
      t[i] = min(t[i], t[i - p] + 1);
      if (p % 2)
        ot[i] = min(ot[i], ot[i - p] + 1);
    }
  }
}

int main() {
  init();
  int n;
  while (scanf("%d", &n), n) {
    printf("%d %d\\n", t[n], ot[n]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

#define INF (1 << 28)

int t[1000 * 1000 + 1];
int ot[1000 * 1000 + 1];

void init() {
  memset(t, -1, sizeof(t));
  memset(ot, -1, sizeof(ot));
  for (int i = 1;; i++) {
    int n = i * (i + 1) * (i + 2) / 6;
    if (n > 1000 * 1000)
      break;
    t[n] = 1;
    if (n % 2)
      ot[n] = 1;
  }
}

int count_t(int n) {
  if (t[n] >= 0)
    return t[n];
  int ans = INF;
  for (int i = 1;; i++) {
    int p = i * (i + 1) * (i + 2) / 6;
    if (n <= p)
      break;
    ans = min(ans, count_t(n - p) + 1);
  }
  return t[n] = ans;
}

int count_odd_t(int n) {
  if (ot[n] >= 0)
    return ot[n];
  int ans = INF;
  for (int i = 1;; i++) {
    int p = i * (i + 1) * (i + 2) / 6;
    if (n <= p)
      break;
    if (p % 2)
      ans = min(ans, count_odd_t(n - p) + 1);
  }
  return ot[n] = ans;
}

int main() {
  init();
  int n;
  while (scanf("%d", &n), n) {
    printf("%d %d\\n", count_t(n), count_odd_t(n));
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int i, j, k;
  int x, y;
  int inf = 1 << 10;
  int m[200] = {};
  int memo[1000005] = {};
  int memo2[1000005] = {};
  vector<int> odd;
  fill(memo, memo + 1000005, inf);
  fill(memo2, memo2 + 1000005, inf);
  for (i = 1; i < 201; i++) {
    m[i - 1] = i * (i + 1) * (i + 2) / 6;
    if (m[i - 1] % 2 == 1)
      odd.push_back(m[i - 1]);
  }
  memo[0] = 0;
  for (i = 0; i < 200; i++) {
    for (j = m[i]; j < 1000005; j++) {
      memo[j] = min(memo[j], memo[j - m[i]] + 1);
    }
  }
  memo2[0] = 0;
  for (i = 0; i < odd.size(); i++) {
    for (j = odd[i]; j < 1000005; j++) {
      memo2[j] = min(memo2[j], memo2[j - odd[i]] + 1);
    }
  }
  int n;
  cin >> n;
  while (n != 0) {
    cout << memo[n] << " " << memo2[n] << endl;
    cin >> n;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int x, y;
int m[1000005] = {};
int memo[1000005] = {};
int memo2[1000005] = {};
vector<int> odd;
void saiki(int n, int d) {
  int i, j;
  // cout << n << ":" << d << ":" << x << endl;
  if (n == 0) {
    // cout << x << d << endl;
    x = min(x, d);
  } else if (n > 0) {
    if (memo[n] > 0) {
      x = min(x, memo[n] + d);
    } else {
      for (i = 1; m[i] <= n; i++)
        ;
      i--;
      int b = x;
      for (j = i; j > 0; j--) {
        if (x > d + 1)
          saiki(n - m[j], d + 1);
      }
      if (x < b)
        memo[n] = x - d;

      // cout << n << ":" << memo[n]<< endl;
    }
  }
}
void saiki2(int n, int d) {
  int i, j;
  // cout << n << ":" << d << endl;
  if (n == 0) {
    y = min(y, d);
  } else if (n > 0) {
    if (memo2[n] > 0) {
      y = min(y, memo2[n] + d);
    } else {
      for (i = 0; odd[i] <= n; i++)
        ;
      i--;
      int b = y;
      // cout << i << ":" << n << ":" << odd[i] << endl;
      for (j = i; j >= 0; j--) {
        if (y > d + 1) {
          saiki2(n - odd[j], d + 1);
        }
      }
      if (y < b)
        memo2[n] = y - d;
    }
  }
}
int main() {
  int i, j, k;
  fill(memo, memo + 1000005, -1);
  fill(memo2, memo2 + 1000005, -1);
  for (i = 1; i < 1000005; i++) {
    m[i] = i * (i + 1) * (i + 2) / 6;
    if (m[i] % 2 == 1)
      odd.push_back(m[i]);
  }
  int n;
  cin >> n;
  while (n != 0) {
    x = 1 << 10;
    y = x;
    saiki(n, 0);
    saiki2(n, 0);
    cout << x << " " << y << endl;
    cin >> n;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30

using namespace std;
typedef long long ll;

vector<int> v, v2;
int d[1000005];
int d2[1000005];

int main() {
  rep(i, 200) {
    int t = i * (i + 1) * (i + 2);
    t /= 6;

    if (t % 2 == 0) {
      v.push_back(t);
    } else {
      v.push_back(t);
      v2.push_back(t);
    }
  }

  fill(d, d + 1000005, INF);
  fill(d2, d2 + 1000005, INF);

  d[0] = 0;
  d2[0] = 0;

  rep(i, 1000005) {
    rep(j, v.size()) {
      int next = i + v[j];
      if (next < 1000005) {
        d[next] = min(d[next], d[i] + 1);
      }
    }

    rep(j, v2.size()) {
      int next = i + v2[j];
      if (next < 1000005) {
        d2[next] = min(d2[next], d2[i] + 1);
      }
    }
  }

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

  return 0;
}

Error: MLE

Fixed submission:

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

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

struct edge {
  int from, to;
  int cost;

  edge(int t, int c) : to(t), cost(c) {}
  edge(int f, int t, int c) : from(f), to(t), cost(c) {}

  bool operator<(const edge &e) const { return cost < e.cost; }
};

vector<edge> G[1000005];
vector<edge> G2[1000005];
int d[1000005];
int d2[1000005];

void dijkstra(int s, int n) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + n, INF);

  d[s] = 0;
  que.push(P(0, s));

  while (que.size()) {
    P p = que.top();
    que.pop();

    int v = p.second;
    if (d[v] < p.first)
      continue;

    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }

  priority_queue<P, vector<P>, greater<P>> que2;
  fill(d2, d2 + n, INF);

  d2[s] = 0;
  que2.push(P(0, s));

  while (que2.size()) {
    P p = que2.top();
    que2.pop();

    int v = p.second;
    if (d2[v] < p.first)
      continue;

    rep(i, G2[v].size()) {
      edge e = G2[v][i];
      if (d2[e.to] > d2[v] + e.cost) {
        d2[e.to] = d2[v] + e.cost;
        que2.push(P(d2[e.to], e.to));
      }
    }
  }
}

int main() {
  vector<int> v;
  REP(i, 1, 200) {
    int t = i * (i + 1) * (i + 2);
    t /= 6;
    v.push_back(t);
  }

  rep(i, 100005) {
    rep(j, v.size()) {
      if (i + v[j] > 1000000)
        continue;

      if (v[j] % 2 == 0) {
        G[i].push_back(edge(i + v[j], 1));
      } else {
        G[i].push_back(edge(i + v[j], 1));
        G2[i].push_back(edge(i + v[j], 1));
      }
    }
  }

  dijkstra(0, 1000005);

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

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

#include <stdio.h>
#include <string.h>
#define INF 1000001

int main(void) {
  int i, j, n, c, small1, small2;
  int dp1[1000000], dp2[1000000];
  scanf("%d", &n);
  while (n != 0) {
    for (i = 0; i <= n; i++)
      dp1[i] = dp2[i] = INF;
    dp1[0] = dp2[0] = 0;
    for (i = 0; i < n; i++) {
      small1 = dp1[n];
      if (small1 > dp1[i] + 1 && dp1[i] != INF) {
        for (j = 1, c = j * (j + 1) * (j + 2); c / 6 + i <= n;
             j++, c = j * (j + 1) * (j + 2)) {
          if (dp1[c / 6 + i] > dp1[i] + 1 && dp1[i] != INF) {
            dp1[c / 6 + i] = dp1[i] + 1;
          }
        }
      }
    }
    for (i = 0; i < n; i++) {
      small2 = dp2[n];
      if (small2 > dp2[i] + 1 && dp2[i] != INF) {
        for (j = 1, c = j * (j + 1) * (j + 2); c / 6 + i <= n;
             j++, c = j * (j + 1) * (j + 2)) {
          if (dp2[c / 6 + i] > dp2[i] + 1 && (c / 6) % 2 == 1 && dp2[i] != INF)
            dp2[c / 6 + i] = dp2[i] + 1;
        }
      }
    }
    printf("%d %d\\n", dp1[n], dp2[n]);
    scanf("%d", &n);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>
#define INF 1000001

int main(void) {
  int i, j, n, c, small1, small2;
  int dp1[1000000], dp2[1000000];
  scanf("%d", &n);
  while (n != 0) {
    for (i = 0; i <= n; i++)
      dp1[i] = dp2[i] = INF;
    dp1[0] = dp2[0] = 0;
    for (i = 0; i < n; i++) {
      small1 = dp1[n];
      for (j = i; j < n && dp1[j] >= small1; j++)
        ;
      if (j == n)
        break;
      if (j != i)
        i = j - 1;
      if (dp1[i] != INF) {
        for (j = 1, c = j * (j + 1) * (j + 2); c / 6 + i <= n;
             j++, c = j * (j + 1) * (j + 2)) {
          if (dp1[c / 6 + i] > dp1[i] + 1 && dp1[i] != INF)
            dp1[c / 6 + i] = dp1[i] + 1;
        }
      }
    }
    for (i = 0; i < n; i++) {
      small2 = dp2[n];
      for (j = i; j < n && dp2[j] >= small2; j++)
        ;
      if (j == n)
        break;
      if (j != i)
        i = j - 1;
      if (dp2[i] != INF) {
        for (j = 1, c = j * (j + 1) * (j + 2); c / 6 + i <= n;
             j++, c = j * (j + 1) * (j + 2)) {
          if (dp2[c / 6 + i] > dp2[i] + 1 && (c / 6) % 2 == 1 && dp2[i] != INF)
            dp2[c / 6 + i] = dp2[i] + 1;
        }
      }
    }
    printf("%d %d\\n", dp1[n], dp2[n]);
    scanf("%d", &n);
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

// #define int long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define Rep(i, n) for (int i = 0; i < (n); i++)
#define Rrep(i, a, n) for (int i = (a); i < (n); i++)
#define All(v) v.begin(), v.end()

typedef pair<int, int> Pii;
typedef pair<int, Pii> Pip;
// const int INF = 1107110711071107;

int dp[1000001];
int dp_odd[1000001];
int four[300];
int x;

signed main() {
  Rep(i, 210) four[i] = i * (i + 1) * (i + 2) / 6;

  Rep(i, 1000001) dp[i] = i;
  Rrep(i, 2, 300) {
    Rep(j, 1000001) {
      if (j + four[i] <= 1000000) {
        dp[j + four[i]] = min(dp[j + four[i]], dp[j] + 1);
      }
    }
  }

  Rep(i, 1000001) dp_odd[i] = i;
  Rrep(i, 2, 50) {
    Rep(j, 1000001) {
      dp_odd[j] = min(dp_odd[j], dp_odd[j]);
      if (j + four[(i - 1) * 4 + 1] <= 1000000) {
        dp_odd[j + four[(i - 1) * 4 + 1]] =
            min(dp_odd[j + four[(i - 1) * 4 + 1]], dp_odd[j] + 1);
      }
    }
  }

  while (cin >> x, x) {
    cout << dp[x] << " " << dp_odd[x] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

// #define int long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define Rep(i, n) for (int i = 0; i < (n); i++)
#define Rrep(i, a, n) for (int i = (a); i < (n); i++)
#define All(v) v.begin(), v.end()

typedef pair<int, int> Pii;
typedef pair<int, Pii> Pip;
// const int INF = 1107110711071107;

int dp[200][1000001];
int dp_odd[40][1000001];
int four[210];
int x;

signed main() {
  Rep(i, 210) four[i] = i * (i + 1) * (i + 2) / 6;

  memset(dp, 1e6 + 1, sizeof(dp));
  Rep(i, 1000001) dp[1][i] = i;
  Rrep(i, 2, 200) {
    Rep(j, 1000001) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      if (j + four[i] <= 1000000) {
        dp[i][j + four[i]] = min(dp[i][j + four[i]], dp[i][j] + 1);
      }
    }
  }

  memset(dp_odd, 1e6 + 1, sizeof(dp_odd));
  Rep(i, 1000001) dp_odd[1][i] = i;
  Rrep(i, 2, 40) {
    Rep(j, 1000001) {
      dp_odd[i][j] = min(dp_odd[i][j], dp_odd[i - 1][j]);
      if (j + four[(i - 1) * 4 + 1] <= 1000000) {
        dp_odd[i][j + four[(i - 1) * 4 + 1]] =
            min(dp_odd[i][j + four[(i - 1) * 4 + 1]], dp_odd[i][j] + 1);
      }
    }
  }

  while (cin >> x, x) {
    cout << dp[199][x] << " " << dp_odd[39][x] << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

int N;
int dp[1000001], dpodd[1000001];

int main() {

  // 前計算でdpテーブルを計算。8sもあるのでオーダーは怪しいけど大丈夫のはず
  for (int i = 1; i <= 1000000; i++)
    dp[i] = 1 << 29, dpodd[i] = 1 << 29;

  dp[0] = 0, dpodd[0] = 0;
  for (int i = 1;; i++) {
    int cur = i * (i + 1) * (i + 2) / 6;

    if (cur > 1000000)
      break;

    for (int j = cur; j <= 1000000; j++) {
      if (cur % 2 == 1) {

        dpodd[j] = min(dpodd[j], dpodd[j - cur] + 1);
      }

      dp[j] = min(dp[j], dp[j - cur] + 1);
    }
  }

  while (1) {
    cin >> N;
    if (N == 0)
      break;
    cout << dp[N] << " " << dpodd[N] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int N;
int dp[1000001], dpodd[1000001];

int main() {

  // 前計算でdpテーブルを計算。8sもあるのでオーダーは怪しいけど大丈夫のはず

  for (int i = 0; i <= 1000000; i++)
    dp[i] = 1145141919, dpodd[i] = 1145141919;

  dp[0] = 0, dpodd[0] = 0;
  for (int i = 1;; i++) {
    int cur = i * (i + 1) * (i + 2) / 6;

    if (cur > 1000000)
      continue;

    for (int j = cur; j <= 1000000; j++) {
      if (i % 2 == 1) {
        dpodd[j] = min(dpodd[j], dpodd[j - cur] + 1);
      }

      dp[j] = min(dp[j], dp[j - cur] + 1);
    }
  }

  while (1) {
    cin >> N;
    if (N == 0)
      break;
    cout << dp[N] << " " << dpodd[N] << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef pair<int, pii> pip;

const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;

int dp[1000010];
int dp2[1000010];

int main() {
  int a = 1, b;
  vi v;
  vi u;
  while ((b = a * (a + 1) * (a + 2) / 6) < 1e6) {
    v.pb(b);
    if (b % 2 == 1) {
      u.pb(b);
    }
    a++;
  }
  // cout<<v.size()<<endl;
  for (int i = 0; i < 1000010; i++) {
    dp[i] = INF;
    dp2[i] = INF;
  }
  dp[0] = 0;
  dp2[0] = 0;
  for (int i = 0; i < v.size(); i++) {
    for (int j = v[i]; j < 1000010; j++) {
      dp[j] = min(dp[j], dp[j - v[i]] + 1);
      if (i < u.size() && j >= u[i]) {
        dp2[j] = min(dp2[j], dp2[j - u[i]] + 1);
      }
    }
  }
  // cout<<"OK"<<endl;
  int n;
  while (cin >> n && n > 0) {
    cout << dp[n] << " " << dp2[n] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef pair<int, pii> pip;

const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;

int dp[1000010];
int dp2[1000010];

int main() {
  int a = 1, b;
  vi v;
  vi u;
  while ((b = a * (a + 1) * (a + 2) / 6) < 1e6) {
    v.pb(b);
    if (b % 2 == 1) {
      u.pb(b);
    }
    a++;
  }
  // cout<<v.size()<<endl;
  for (int i = 0; i < 1000010; i++) {
    dp[i] = INF;
    dp2[i] = INF;
  }
  dp[0] = 0;
  dp2[0] = 0;
  for (int i = 0; i < v.size(); i++) {
    for (int j = v[i]; j < 1000010; j++) {
      dp[j] = min(dp[j], dp[j - v[i]] + 1);
      if (i < u.size()) {
        dp2[j] = min(dp2[j], dp2[j - u[i]] + 1);
      }
    }
  }
  // cout<<"OK"<<endl;
  int n;
  while (cin >> n && n > 0) {
    cout << dp[n] << " " << dp2[n] << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, m = 0;
  vector<int> v;
  for (int i = 1; (i * (i + 1) * (i + 2)) / 6 < 1e6; ++i) {
    v.push_back((i * (i + 1) * (i + 2)) / 6);
    m++;
  }
  vector<int> dp(1e6, 1e9);
  vector<int> dp1(1e6, 1e9);
  dp[0] = 0;
  dp1[0] = 0;
  for (int i = 0; i <= 1e6; ++i) {
    for (int j = 0; j < m; ++j) {
      if (i + v[j] >= 1e6)
        break;
      dp[i + v[j]] = min(dp[i] + 1, dp[i + v[j]]);
      if (v[j] % 2 == 0)
        continue;
      dp1[i + v[j]] = min(dp1[i] + 1, dp1[i + v[j]]);
    }
  }
  while (cin >> n, n) {
    cout << dp[n] << " " << dp1[n] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  vector<int> v;
  for (int i = 1; (i * (i + 1) * (i + 2)) / 6 < 1e6; ++i) {
    v.push_back((i * (i + 1) * (i + 2)) / 6);
  }
  while (cin >> n, n) {
    vector<int> dp(1e6, 1e9);
    vector<int> dp1(1e6, 1e9);
    dp[0] = 0;
    dp1[0] = 0;
    for (int i = 0; i <= n; ++i) {
      for (int j = 0; j < v.size(); ++j) {
        if (i + v[j] >= 1e6)
          break;
        dp[i + v[j]] = min(dp[i] + 1, dp[i + v[j]]);
        if (v[j] % 2 == 0)
          continue;
        dp1[i + v[j]] = min(dp1[i] + 1, dp1[i + v[j]]);
      }
    }
    cout << dp[n] << " " << dp1[n] << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>

#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))

short tbl[2000000], otbl[2000000];

int main(void) {
  memset(tbl, 0x7F, sizeof(tbl));
  memset(otbl, 0x7F, sizeof(otbl));

  for (int i = 1; i * (i + 1) * (i + 2) / 6 < 1050000; i++)
    tbl[i * (i + 1) * (i + 2) / 6] = 1;
  for (int i = 1; i < 1005000; i++)
    for (int j = 1; i - j * (j + 1) * (j + 2) / 6 >= 0; j++)
      tbl[i] = MIN(tbl[i], 1 + tbl[i - j * (j + 1) * (j + 2) / 6]);

  for (int i = 1; i * (i + 1) * (i + 2) / 6 < 1050000; i += 4)
    otbl[i * (i + 1) * (i + 2) / 6] = 1;
  for (int i = 1; i < 1005000; i++)
    for (int j = 1; i - j * (j + 1) * (j + 2) / 6 >= 0; j += 4)
      otbl[i] = MIN(otbl[i], 1 + otbl[i - j * (j + 1) * (j + 2) / 6]);

  while (1) {
    int n;
    scanf("%d", &n);
    if (!n)
      break;
    printf("%d %d\\n", tbl[n], otbl[n]);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

vector<long long int> list, oddlist;
int listcnt, oddlistcnt;

int oddrec(int target, int sum, int cnt, int maxcnt) {
  int res = 0xFFFF;
  if (maxcnt < cnt)
    return 0xFFFF;
  if (sum == target)
    return cnt;
  for (int i = oddlistcnt - 1; 0 <= i; i--) {
    if (sum + oddlist[i] > target)
      continue;
    res = min(res, oddrec(target, sum + oddlist[i], cnt + 1, maxcnt));
  }
  return res;
}

int main(void) {
  for (long long int i = 1; i * (i + 1) * (i + 2) / 6 < 1e6; i++) {
    list.push_back(i * (i + 1) * (i + 2) / 6);
    if (i * (i + 1) * (i + 2) / 6 % 2)
      oddlist.push_back(i * (i + 1) * (i + 2) / 6);
  }

  listcnt = list.size();
  oddlistcnt = oddlist.size();
  while (true) {
    int n;
    scanf("%d", &n);
    if (!n)
      break;

    int res = 5, oddres = 1000;
    for (int i = listcnt - 1; 0 <= i; i--) {
      if (list[i] > n)
        continue;
      if (list[i] == n) {
        res = 1;
        break;
      }
      if (res <= 2)
        continue;
      for (int j = listcnt - 1; 0 <= j; j--) {
        if (list[i] + list[j] > n)
          continue;
        if (list[i] + list[j] == n)
          res = 2;
        if (res <= 3)
          continue;
        for (int k = listcnt - 1; 0 <= k; k--) {
          if (list[i] + list[j] + list[k] > n)
            continue;
          if (list[i] + list[j] + list[k] == n)
            res = 3;
          if (res <= 4)
            continue;
          for (int l = listcnt - 1; 0 <= l; l--) {
            if (list[i] + list[j] + list[k] + list[l] > n)
              continue;
            if (list[i] + list[j] + list[k] + list[l] == n)
              res = 4;
            if (res <= 4)
              break;
          }
        }
      }
    }

    if ((oddres = oddrec(n, 0, 0, 1)) == 0xFFFF) {
      for (int i = 2;; i = i * 3 / 2) {
        if ((oddres = oddrec(n, 0, 0, i)) != 0xFFFF)
          break;
      }
    }

    printf("%d %d\\n", res, oddres);
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SET(a, c) memset(a, c, sizeof(a))
#define PB push_back
using namespace std;

const int MAX_N = 1e6;
const int INF = 0x3f3f3f3f;

int dp[MAX_N], dp_odd[MAX_N];

void solve(int n) {
  vector<int> m;

  SET(dp, INF);
  SET(dp_odd, INF);

  REP(i, MAX_N) {
    m.PB((i + 1) * (i + 2) * (i + 3) / 6);
    if (m.back() > MAX_N)
      break;
  }

  dp[0] = 0;
  dp_odd[0] = 0;
  REP(i, m.size()) {
    REP(j, n + 1) {
      if (j < m[i])
        continue;
      dp[j] = min(dp[j], dp[j - m[i]] + 1);
      if (m[i] % 2)
        dp_odd[j] = min(dp_odd[j], dp_odd[j - m[i]] + 1);
    }
  }
}

int main() {
  int n;
  solve(MAX_N);
  while (1) {
    scanf("%d", &n);
    if (!n)
      break;
    printf("%d %d\\n", dp[n], dp_odd[n]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SET(a, c) memset(a, c, sizeof(a))
#define PB push_back
using namespace std;

const int MAX_N = 1e6;
const int INF = 0x3f3f3f3f;
int dp[MAX_N], dp_odd[MAX_N];

void solve(int n) {
  vector<int> m;

  REP(i, MAX_N) {
    m.PB((i + 1) * (i + 2) * (i + 3) / 6);
    if (m.back() > n)
      break;
  }

  REP(i, n + 1) dp[i] = dp_odd[i] = i;
  REP(i, m.size()) {
    if (i == 1)
      continue;
    REP(j, n + 1) {
      if (j < m[i])
        continue;
      dp[j] = min(dp[j], dp[j - m[i]] + 1);
      if (m[i] % 2)
        dp_odd[j] = min(dp_odd[j], dp_odd[j - m[i]] + 1);
    }
  }
  printf("%d %d\\n", dp[n], dp_odd[n]);
}

int main() {
  int n;
  while (1) {
    scanf("%d", &n);
    if (!n)
      break;
    solve(n);
  }

  return 0;
}