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

#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <string>
#include <vector>
#define loop(i, a, b) for (int i = a; i < int(b); i++)
#define rep(i, n) loop(i, 0, n)

using namespace std;

typedef vector<int> vi;
int const inf = 1 << 28;
int main() {
  vi q, r;
  for (int i = 1;; i++) {
    int pq = i * (i + 1) * (i + 2) / 6;
    q.push_back(pq);
    if (pq & 1)
      r.push_back(pq);

    if (pq > 1e6 + 100)
      break;
  }

  int n = 1000000;

  vi dp(n + 1, inf);
  dp[0] = 0;
  rep(i, q.size()) {
    rep(j, n + 1) {
      if (j - q[i] >= 0) {
        dp[j] = min(dp[j], dp[j - q[i]] + 1);
      }
    }
  }

  vi dp2(n + 1, inf);
  dp2[0] = 0;
  rep(i, r.size()) {
    rep(j, n + 1) {
      if (j - r[i] >= 0) {
        dp2[j] = min(dp2[j], dp2[j - r[i]] + 1);
      }
    }
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <string>
#include <vector>
#define loop(i, a, b) for (int i = a; i < int(b); i++)
#define rep(i, n) loop(i, 0, n)

using namespace std;

typedef vector<int> vi;
int const inf = 1 << 28;
int main() {
  vi q, r;
  for (int i = 1;; i++) {
    int pq = i * (i + 1) * (i + 2) / 6;
    q.push_back(pq);
    if (pq & 1)
      r.push_back(pq);

    if (pq > 1e6 + 100)
      break;
  }

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

    vi dp(n + 1, inf);
    dp[0] = 0;
    rep(i, q.size()) {
      rep(j, n + 1) {
        if (j - q[i] >= 0) {
          dp[j] = min(dp[j], dp[j - q[i]] + 1);
        }
      }
    }
    cout << dp[n] << " ";

    vi dp2(n + 1, inf);
    dp2[0] = 0;
    rep(i, r.size()) {
      rep(j, n + 1) {
        if (j - r[i] >= 0) {
          dp2[j] = min(dp2[j], dp2[j - r[i]] + 1);
        }
      }
    }
    cout << dp2[n] << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000
#define MAXN 1000000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int dp[MAXN + 1];
int dp2[MAXN + 1];

int main(void) {
  for (int j = 0; j <= MAXN; j++) {
    dp[j] = INF;
    dp2[j] = INF;
  }
  dp[0] = 0;
  dp[1] = 1;
  dp2[0] = 0;
  dp2[1] = 1;
  // DP
  for (int i = 1; i < 200; i++) {
    int plus = i * (i + 1) * (i + 2) / 6;
    for (int j = 1; j <= MAXN; j++) {
      if (j - plus >= 0)
        dp[j] = min(dp[j], dp[j - plus] + 1);
      else
        dp[j] = dp[j];
    }
    if (plus % 2 == 1) {
      for (int j = 1; j <= MAXN; j++) {
        if (j - plus >= 0)
          dp2[j] = min(dp2[j], dp2[j - plus] + 1);
        else
          dp2[j] = dp2[j];
      }
    } else {
      for (int j = 1; j <= MAXN; j++) {
        dp2[j] = dp2[j];
      }
    }
  }
  //    for (int i = 1; i < 5; i++) {
  //        for (int j = 1; j <= 10; j++) {
  //            cout << dp2[i][j] << " ";
  //        }
  //        cout << endl;
  //    }
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cout << dp[n] << " " << dp2[n] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000
#define MAXN 1000000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int dp[200][MAXN + 1];
int dp2[200][MAXN + 1];

int main(void) {
  for (int i = 0; i < 200; i++) {
    for (int j = 0; j <= MAXN; j++) {
      dp[i][j] = INF;
      dp2[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  dp[0][1] = 1;
  dp2[0][0] = 0;
  dp2[0][1] = 1;
  // DP
  for (int i = 1; i < 200; i++) {
    int plus = i * (i + 1) * (i + 2) / 6;
    dp[i][0] = 0;
    dp2[i][0] = 0;
    for (int j = 1; j <= MAXN; j++) {
      if (j - plus >= 0)
        dp[i][j] = min(dp[i - 1][j], dp[i][j - plus] + 1);
      else
        dp[i][j] = dp[i - 1][j];
    }
    if (plus % 2 == 1) {
      for (int j = 1; j <= MAXN; j++) {
        if (j - plus >= 0)
          dp2[i][j] = min(dp2[i - 1][j], dp2[i][j - plus] + 1);
        else
          dp2[i][j] = dp2[i - 1][j];
      }
    } else {
      for (int j = 1; j <= MAXN; j++) {
        dp2[i][j] = dp2[i - 1][j];
      }
    }
  }
  //    for (int i = 1; i < 5; i++) {
  //        for (int j = 1; j <= 10; j++) {
  //            cout << dp2[i][j] << " ";
  //        }
  //        cout << endl;
  //    }
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cout << dp[199][n] << " " << dp2[99][n] << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define INF (1 << 28)
#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = (j); i <= (k); i++)
#define fs first
#define sc second
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int dp1[3][1000000];
int dp2[3][1000000];
int main() {
  vi v1, v2;
  for (int i = 1;; i++) {
    int t = i * (i + 1) * (i + 2) / 6;
    if (t > 1000000)
      break;
    v1.pb(t);
    if (t & 1)
      v2.pb(t);
  }
  rep(i, 1000000) dp1[0][i] = dp2[0][i] = i;
  rep(i, v1.size() - 1) {
    int n = (i + 1) & 1, c = i & 1;
    rep(j, 1000000) {
      dp1[2][j] = dp1[n][j] = dp1[c][j];
      if (j >= v1[i + 1])
        dp1[2][j] = dp1[n][j] = min(dp1[n][j], dp1[n][j - v1[i + 1]] + 1);
    }
  }
  rep(i, v2.size() - 1) {
    int n = (i + 1) & 1, c = 1 & i;
    rep(j, 1000000) {
      dp2[2][j] = dp2[n][j] = dp2[c][j];
      if (j >= v2[i + 1])
        dp2[2][j] = dp2[n][j] = min(dp2[n][j], dp2[n][j - v2[i + 1]] + 1);
    }
  }
  int n;
  while (cin >> n, n) {
    cout << dp1[2][n] << " " << dp2[2][n] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define INF (1 << 28)
#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = (j); i <= (k); i++)
#define fs first
#define sc second
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int dp1[181][1000000];
int dp2[181][1000000];
int main() {
  vi v1, v2;
  for (int i = 1;; i++) {
    int t = i * (i + 1) * (i + 2) / 6;
    if (t > 1000000)
      break;
    v1.pb(t);
    if (t & 1)
      v2.pb(t);
  }
  rep(i, 1000000) dp1[0][i] = dp2[0][i] = i;
  rep(i, v1.size() - 1) {
    rep(j, 1000000) {
      dp1[i + 1][j] = dp1[i][j];
      if (j >= v1[i + 1])
        dp1[i + 1][j] = min(dp1[i + 1][j], dp1[i + 1][j - v1[i + 1]] + 1);
    }
  }
  rep(i, v2.size() - 1) {
    rep(j, 1000000) {
      dp2[i + 1][j] = dp2[i][j];
      if (j >= v2[i + 1])
        dp2[i + 1][j] = min(dp2[i + 1][j], dp2[i + 1][j - v2[i + 1]] + 1);
    }
  }
  int n;
  while (cin >> n, n) {
    cout << dp1[v1.size() - 1][n] << " " << dp2[v2.size() - 1][n] << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  constexpr int N = 1e6;
  V<> a, b;
  for (int i = 1;; ++i) {
    a.push_back(i * (i + 1) * (i + 2) / 6);
    if (a.back() >= N) {
      a.pop_back();
      break;
    }
    if (a.back() & 1)
      b.push_back(a.back());
  }
  V<> p(N, -1), q(N, -1);
  p[0] = q[0] = 0;
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int i = que.front();
    que.pop();
    for (int e : a)
      if (i + e < N and p[i + e] == -1) {
        p[i + e] = p[i] + 1;
        que.push(i + e);
      }
  }
  que.push(0);
  while (!que.empty()) {
    int i = que.front();
    que.pop();
    for (int e : b)
      if (i + e < N and q[i + e] == -1) {
        q[i + e] = q[i] + 1;
        que.push(i + e);
      }
  }
  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;
    cout << p[n] << ' ' << q[n] << '\\n';
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  constexpr int N = 1e4;
  V<> a, b;
  for (int i = 1;; ++i) {
    a.push_back(i * (i + 1) * (i + 2) / 6);
    if (a.back() >= N) {
      a.pop_back();
      break;
    }
    if (a.back() & 1)
      b.push_back(a.back());
  }
  V<> p(N, -1), q(N, -1);
  p[0] = q[0] = 0;
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int i = que.front();
    que.pop();
    for (int e : a)
      if (i + e < N and p[i + e] == -1) {
        p[i + e] = p[i] + 1;
        que.push(i + e);
      }
  }
  que.push(0);
  while (!que.empty()) {
    int i = que.front();
    que.pop();
    for (int e : b)
      if (i + e < N and q[i + e] == -1) {
        q[i + e] = q[i] + 1;
        que.push(i + e);
      }
  }
  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;
    cout << p[n] << ' ' << q[n] << '\\n';
  }
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;

#define MAX_N 1000000

int N;
int inf = 1000000000;

int d[MAX_N + 1],
    d2[MAX_N + 1]; // ?????????j??¨?????????????????????????°??????°

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

int main() {
  d[0] = 0;
  for (int i = 1; i <= MAX_N; i++) {
    d[i] = inf;
    for (int j = 1;; j++) {
      if (i < calc_sn(j))
        break;
      d[i] = min(d[i], d[i - calc_sn(j)] + 1);
    }
  }
  d2[0] = 0;
  for (int i = 1; i <= MAX_N; i++) {
    d2[i] = inf;
    for (int j = 1;; j++) {
      if (i < calc_sn(j))
        break;
      if (calc_sn(j) % 2 == 0)
        continue;
      d2[i] = min(d2[i], d2[i - calc_sn(j)] + 1);
    }
  }
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    cout << d[N] << " " << d2[N] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;

#define MAX_N 1000000

int N;
int inf = 1000000000;

int d[MAX_N + 1]; // ?????????j??¨?????????????????????????°??????°

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

void solve() {
  d[0] = 0;
  for (int i = 1; i <= N; i++) {
    d[i] = inf;
    for (int j = 1;; j++) {
      if (i < calc_sn(j))
        break;
      d[i] = min(d[i], d[i - calc_sn(j)] + 1);
    }
  }
  cout << d[N] << " ";
  d[0] = 0;
  for (int i = 1; i <= N; i++) {
    d[i] = inf;
    for (int j = 1;; j++) {
      if (i < calc_sn(j))
        break;
      if (calc_sn(j) % 2 == 0)
        continue;
      d[i] = min(d[i], d[i - calc_sn(j)] + 1);
    }
  }
  cout << d[N] << endl;
}

int main() {
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    solve();
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

#define MAX 1000000
#define INF (1 << 24)
int n[100000], M;

int dp[1000005];
int od[1000005];

int main() {
  for (M = 1;; M++) {
    int v = M * (M + 1) * (M + 2) / 6;
    if (v > MAX)
      break;
    n[M] = v;
  }

  for (int i = 0; i <= MAX; i++) {
    dp[i] = INF;
    od[i] = INF;
  }

  dp[0] = od[0] = 0;

  for (int i = 1; i < M; i++)
    for (int j = 0; j <= MAX; j++) {
      if (j + n[i] > MAX)
        break;
      if (dp[j] != INF) {
        dp[j + n[i]] = min(dp[j + n[i]], dp[j] + 1);
      }
      if (od[j] != INF && n[i] % 2 == 1) {
        od[j + n[i]] = min(od[j + n[i]], od[j] + 1);
      }
    }
  int N;
  while (cin >> N, N) {

    cout << dp[N] << " " << od[N] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define MAX 100000
#define INF (1 << 24)
int n[100000], M;

int dp[1000005];
int od[1000005];

int main() {
  for (M = 1;; M++) {
    int v = M * (M + 1) * (M + 2) / 6;
    if (v > MAX)
      break;
    n[M] = v;
  }

  int N;
  while (cin >> N, N) {
    for (int i = 0; i <= N; i++) {
      dp[i] = INF;
      od[i] = INF;
    }

    dp[0] = od[0] = 0;

    for (int i = 1; i < M; i++)
      for (int j = 0; j <= N; j++) {
        if (j + n[i] > N)
          break;
        if (dp[j] != INF) {
          dp[j + n[i]] = min(dp[j + n[i]], dp[j] + 1);
        }
        if (od[j] != INF && n[i] % 2 == 1) {
          od[j + n[i]] = min(od[j + n[i]], od[j] + 1);
        }
      }

    cout << dp[N] << " " << od[N] << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int si[200];
int dp_k[1000000];
int dp[1000000];

void mk() {
  for (int i = 0; i < 200; i++) {
    si[i] = (i * (i + 1) * (i + 2)) / 6;
    if (si[i] > 1000000) {
      break;
    }
    dp[si[i]] = 1;
    if (si[i] % 2 == 1)
      dp_k[si[i]] = 1;
  }
}

int main() {
  int x, y;
  for (int i = 0; i < 200; i++)
    si[i] = 1000000;
  for (int i = 0; i < 1000000; i++) {
    dp[i] = 100000;
    dp_k[i] = 100000;
  }
  mk();

  dp[0] = 0;
  dp_k[0] = 0;
  for (int i = 1; i < 1000000; i++) {
    for (int j = 1; j < 200; j++) {
      if ((i + si[j]) < 1000000) {
        dp[i + si[j]] = min(dp[i + si[j]], dp[i] + 1);
        if (si[j] % 2 == 1)
          dp_k[i + si[j]] = min(dp_k[i + si[j]], dp_k[i] + 1);
      }
    }
  }

  for (;;) {
    cin >> x;
    if (x == 0)
      return 0;
    cout << dp[x] << " " << dp_k[x] << endl;
  }
}

Error: 0

Fixed submission:

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

int coin[100000];
int coin_ki[100000];
int dp[100000] = {1000000};
int dp2[100000];

int main(void) {

  int count = 0;
  int count_ki = 0;
  int tmp;
  int ans_tmp = 0;
  int ans2_tmp = 0;

  int tmp2 = 0;

  for (int i = 1; i < 100000; i++) {
    tmp = i * (i + 1) * (i + 2) / 6;
    if (tmp > 1000000)
      break;
    coin[i - 1] = tmp;
    count = i;
    if (coin[i - 1] % 2 == 1) {
      coin_ki[tmp2++] = coin[i - 1];
      count_ki = tmp2;
    }
  }

  for (;;) {
    int x;
    cin >> x;

    if (x == 0)
      break;

    for (int i = 0; i < 100000; i++)
      dp[i] = 100000;

    dp[0] = 0;
    for (int i = 0; i < x; i++) {
      for (int j = 0; j < count; j++) {
        if (i + coin[j] <= x) {
          dp[i + coin[j]] = min(dp[i + coin[j]], dp[i] + 1);
          // printf("dp[%d]=%d\\n",i+coin[j],dp[i+coin[j]]);
        }
      }
    }

    int ans1 = dp[x];

    for (int i = 0; i < 100000; i++)
      dp2[i] = 100000;

    dp2[0] = 0;
    for (int i = 0; i < x; i++) {
      for (int j = 0; j < count_ki; j++) {
        if (i + coin_ki[j] <= x) {
          ans2_tmp = min(dp2[i + coin_ki[j]], dp2[i] + 1);
          dp2[i + coin_ki[j]] = ans2_tmp;
          // printf("dp2[%d]=%d\\n",i+coin_ki[j],dp[i+coin_ki[j]]);
        }
      }
    }

    int ans2 = dp2[x];

    // for(int i=0;i<count;i++)
    // printf("coin[%d]=%d\\n",i,coin[i]);
    // for(int i=0;i<count_ki;i++)
    // printf("coin_ki[%d]=%d\\n",i,coin_ki[i]);

    printf("%d %d\\n", ans1, ans2);
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

int p[200], q[50];

int f(int n, int m, int c) {
  if (c == 4 && n > 0)
    return 5;
  if (n < 10)
    return min(c + n / 4 + n % 4, 5);
  int rec = 5, i = 0;
  while (p[i] < n / (4 - c))
    i++;
  for (i; p[i] <= m; i++)
    rec = min(rec, f(n - p[i], min(p[i], n - p[i]), c + 1));
  return rec;
}

int g(int n, int m, int c, int *M) {
  if (c == *M)
    return *M;
  if (n < 165)
    return c + n / 35 + n % 35;
  int i = 44;
  while (q[i] > m)
    i--;
  for (i; i > 0; i--) {
    *M = min(*M, g(n - q[i], min(q[i], n - q[i]), c + 1, M));
  }
  return *M;
}

int main() {
  for (int i = 0; (i + 1) * (i + 2) * (i + 3) / 6 <= 1000000; i++)
    p[i] = (i + 1) * (i + 2) * (i + 3) / 6;
  for (int i = 0; (4 * i + 1) * (4 * i + 2) * (4 * i + 3) / 6 <= 1000000; i++)
    q[i] = (4 * i + 1) * (4 * i + 2) * (4 * i + 3) / 6;
  int N;
  while (cin >> N) {
    if (!N)
      return 0;
    int ans = 9999;
    cout << f(N, N, 0) << " " << g(N, N, 0, &ans) << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

int p[200], q[100];

int f(int n, int m, int c) {
  if (c == 4 && n > 0)
    return 5;
  if (n < 10)
    return min(c + n / 4 + n % 4, 5);
  int rec = 5, i = 0;
  while (p[i] < n / (4 - c))
    i++;
  for (i; p[i] <= m; i++)
    rec = min(rec, f(n - p[i], min(p[i], n - p[i]), c + 1));
  return rec;
}

int g(int n, int m, int c) {
  if (n < 165)
    return c + n / 35 + n % 35;
  int rec = 99999;
  for (int i = 1; q[i] <= m; i++)
    rec = min(rec, f(n - q[i], min(q[i], n - q[i]), c + 1));
  return rec;
}

int main() {
  for (int i = 0; (i + 1) * (i + 2) * (i + 3) / 6 <= 1000000; i++)
    p[i] = (i + 1) * (i + 2) * (i + 3) / 6;
  for (int i = 0; (4 * i + 1) * (4 * i + 2) * (4 * i + 3) / 6 <= 1000000; i++)
    q[i] = (4 * i + 1) * (4 * i + 2) * (4 * i + 3) / 6;
  int N;
  while (cin >> N) {
    if (!N)
      return 0;
    cout << f(N, N, 0) << " " << g(N, N, 0) << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int dp[1000001];
int dp2[1000001];
vector<int> vi;

void solve() {
  for (int i = 1; i * (i + 1) * (i + 2) / 6 <= 1000000; i++) {
    vi.push_back(i * (i + 1) * (i + 2) / 6);
  }
  fill(dp, dp + 1000001, 9999999);
  fill(dp2, dp2 + 1000001, 9999999);

  dp[0] = 0;
  dp2[0] = 0;
  for (int i = 0; i < 1000000; i++) {
    if (dp[i] != 9999999) {
      for (int j = 0; j < vi.size(); j++) {
        if (i + vi[j] <= 1000000) {
          dp[i + vi[j]] = min(dp[i + vi[j]], dp[i] + 1);
        } else {
          break;
        }
      }
    }
    if (dp2[i] != 9999999) {
      for (int j = 0; j < vi.size(); j++) {
        if (i + vi[j] <= 1000000 && vi[j] % 2 == 1) {
          dp2[i + vi[j]] = min(dp2[i + vi[j]], dp2[i] + 1);
        }
      }
    }
  }
}

int main() {
  int n;
  solve();
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }

    cout << dp[n] << " " << dp2[n] << endl;
  }
}

Error: 0

Fixed submission:

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

using namespace std;

int dp[100001];
int dp2[100001];
vector<int> vi;

void solve() {
  for (int i = 1; i * (i + 1) * (i + 2) / 6 <= 100000; i++) {
    vi.push_back(i * (i + 1) * (i + 2) / 6);
  }
  fill(dp, dp + 100001, 9999999);
  fill(dp2, dp2 + 100001, 9999999);

  dp[0] = 0;
  dp2[0] = 0;
  for (int i = 0; i < 100000; i++) {
    if (dp[i] != 9999999) {
      for (int j = 0; j < vi.size(); j++) {
        if (i + vi[j] <= 100000) {
          dp[i + vi[j]] = min(dp[i + vi[j]], dp[i] + 1);
        } else {
          break;
        }
      }
    }
    if (dp2[i] != 9999999) {
      for (int j = 0; j < vi.size(); j++) {
        if (i + vi[j] <= 100000 && vi[j] % 2 == 1) {
          dp2[i + vi[j]] = min(dp2[i + vi[j]], dp2[i] + 1);
        }
      }
    }
  }
}

int main() {
  int n;
  solve();
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }

    cout << dp[n] << " " << dp2[n] << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>

using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const int SIZE = 300;
const int N = 1000000;
int p[SIZE + 1];
int dp1[N + 1], dp2[N + 1];

void make_pollock(void) {
  for (int i = 0; i <= SIZE; i++) {
    ll ans = (ll)i * (i + 1LL) * (i + 2LL) / 6LL;
    p[i] = (int)ans;
  } // end for
}

void make_dp(void) {
  dp1[0] = 0;
  for (int i = 1; i <= SIZE; i++) {
    int m = p[i];
    for (int j = m; j <= N; j++) {
      if (dp1[j] > dp1[j - m] + 1) {
        dp1[j] = dp1[j - m] + 1;
      } // end if
    }   // end for
  }     // end for

  dp2[0] = 0;
  for (int i = 1; i <= SIZE; i++) {
    if (p[i] % 2 == 0)
      continue;
    int m = p[i];
    for (int j = m; j <= N; j++) {
      if (dp2[j] > dp2[j - m] + 1) {
        dp2[j] = dp2[j - m] + 1;
      } // end if
    }   // end for
  }     // end for
}

int main() {
  memset(p, 0, sizeof(p));
  fill(dp1, dp1 + N + 1, INF);
  fill(dp2, dp2 + N + 1, INF);

  make_pollock();
  make_dp();

  int n;
  while (cin >> n && n) {
    cout << dp1[n] << ' ' << dp2[n] << endl;
  } // end while

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

const int INF = 1 << 30;
typedef pair<int, int> P;

vector<P> p;

void make_pollock(void) {
  for (int i = 1; i <= 85; i++) {
    ll ans = (ll)i * (i + 1LL) * (i + 2LL) / 6LL;
    p.push_back(P((int)ans, i));
  } // end rep
}

int main() {
  p.clear();
  make_pollock();

  int n;
  while (cin >> n && n) {
    P res = P(0, 0);
    vector<int> dp1(n + 1, INF), dp2(n + 1, INF);
    dp1[0] = 0;
    for (int i = 0; i < p.size(); i++) {
      int m = p[i].first;
      for (int j = m; j <= n; j++) {
        if (dp1[j] > dp1[j - m] + 1) {
          dp1[j] = dp1[j - m] + 1;
        } // end if
      }   // end for
    }     // end for
    res.first = dp1[n];
    dp2[0] = 0;
    for (int i = 0; i < p.size(); i++) {
      if (p[i].first % 2 == 0)
        continue;
      int m = p[i].first;
      for (int j = m; j <= n; j++) {
        if (dp2[j] > dp2[j - m] + 1) {
          dp2[j] = dp2[j - m] + 1;
        } // end if
      }   // end for
    }     // end for
    res.second = dp2[n];

    cout << res.first << ' ' << res.second << endl;
  } // end while

  return 0;
}