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


#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

const int MAX_N = 1e6;
vector<int> dp1, dp2;
vector<int> tetra;

int main(void) {
  int n = 1;
  do {
    tetra.push_back(n * (n + 1) * (n + 2) / 6);
    ++n;
  } while (tetra.back() <= MAX_N);
  dp1.push_back(0);
  dp2.push_back(0);
  for (int i = 1; i <= MAX_N; ++i) {
    int a1 = INT_MAX, a2 = INT_MAX;
    for (int t : tetra) {
      if (t > i)
        break;
      a1 = min(a1, dp1[i - t] + 1);
      if (t % 2) {
        a2 = min(a2, dp2[i - t] + 1);
      }
    }
    dp1.push_back(a1);
    dp2.push_back(a2);
  }
  int x;
  while (cin >> x, x) {
    cout << dp1[x] << " " << dp2[x] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:


#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

const int MAX_N = 1e6;
vector<int> dp1, dp2;
vector<int> tetra;

int main(void) {
  int n = 1;
  do {
    tetra.push_back(n * (n + 1) * (n + 2) / 6);
    ++n;
  } while (tetra.back() <= MAX_N);
  dp1.push_back(0);
  dp2.push_back(0);
  for (int i = 1; i <= n; ++i) {
    int a1 = INT_MAX, a2 = INT_MAX;
    for (int t : tetra) {
      if (t > i)
        break;
      a1 = min(a1, dp1[i - t] + 1);
      if (t % 2) {
        a2 = min(a2, dp2[i - t] + 1);
      }
    }
    dp1.push_back(a1);
    dp2.push_back(a2);
  }
  int x;
  while (cin >> x, x) {
    cout << dp1[x] << " " << dp2[x] << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <utility>
#define pi 3.14159
#define Inf (int)pow(2., 12.)

using namespace std;
typedef std::pair<int, int> mypair;
queue<mypair> qu;
#define M 1000000

int odd[M + 10], all[M + 10];

void update(int *ar, int x) {
  for (int i = x; i <= M; ++i) {
    ar[i] = min(ar[i], ar[i - x] + 1);
  }
}

int main() {
  odd[0] = all[0] = 0;
  for (int i = 1; i <= M; ++i) {
    odd[i] = all[i] = 10000000;
  }

  for (int i = 1;; ++i) {
    int x = i * (i + 1) * (i + 2) / 6;
    if (x > M)
      break;
    update(all, x);
    if (x % 2 == 1)
      update(odd, x);
  }

  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cout << all[n] << " " << odd[n] << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <utility>
#define pi 3.14159
#define Inf (int)pow(2., 12.)

using namespace std;
typedef std::pair<int, int> mypair;
queue<mypair> qu;
#define M 1000000

int odd[M], all[M];

void update(int *ar, int x) {
  for (int i = x; i <= M; ++i) {
    ar[i] = min(ar[i], ar[i - x] + 1);
  }
}

int main() {
  odd[0] = all[0] = 0;
  for (int i = 1; i <= M; ++i) {
    odd[i] = all[i] = 10000000;
  }

  for (int i = 1;; ++i) {
    int x = i * (i + 1) * (i + 2) / 6;
    if (x > M)
      break;
    update(all, x);
    if (x % 2 == 1)
      update(odd, x);
  }

  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cout << all[n] << " " << odd[n] << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

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

const int INF = 1000000000;

int main() {
  int ans[1000000];
  int bns[1000000];
  for (int i = 0; i < 1000000; i++) {
    if (i == 0) {
      ans[i] = 0;
      bns[i] = 0;
      continue;
    }
    ans[i] = INF;
    bns[i] = INF;
    for (int j = 0; i - j * (j + 1) * (j + 2) / 6 >= 0 && j < 200; j++) {
      ans[i] = min(ans[i], ans[i - j * (j + 1) * (j + 2) / 6] + 1);
      if (j * (j + 1) * (j + 2) / 6 % 2 == 1)
        bns[i] = min(bns[i], bns[i - j * (j + 1) * (j + 2) / 6] + 1);
    }
  }
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    else
      printf("%d %d\\n", ans[n], bns[n]);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>
int list[181], dp[999999], dq[999999];
int main() {
  for (int i = 0; i < 999999; i++) {
    dp[i] = 1000000;
    dq[i] = 1000000;
  }
  for (int i = 0; i < 181; i++) {
    list[i] = (i + 1) * (i + 2) * (i + 3) / 6;
    dp[list[i] - 1] = 1;
    if (i % 4 == 0)
      dq[list[i] - 1] = 1;
  }
  for (int i = 0; i < 999999; i++) {
    for (int j = 0; list[j] <= i; j++) {
      if (dp[i] > dp[i - list[j]] + 1)
        dp[i] = dp[i - list[j]] + 1;
      if (j % 4 == 0 && dq[i] > dq[i - list[j]] + 1)
        dq[i] = dq[i - list[j]] + 1;
    }
  }
  while (1) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      return 0;
    printf("%d %d\\n", dp[n - 1], dq[n - 1]);
  }
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define all(vec) vec.begin(), vec.end()
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<P, P> PP;
const ll MOD = 10000;
const ll INF = 1000000010;
const int MAX = 100001;
int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int a[1000010], b[1000010];
int main() {
  a[0] = 0;
  b[0] = 0;
  for (int i = 1; i <= 1000010; i++) {
    a[i] = i;
    b[i] = i;
    int j = 1;
    while (j++) {
      int p = j * (j + 1) * (j + 2) / 6;
      if (i < p)
        break;
      a[i] = min(a[i], a[i - p] + 1);
      if (p % 2)
        b[i] = min(b[i], b[i - p] + 1);
    }
  }
  int n;
  while (cin >> n, n) {
    cout << a[n] << " " << b[n] << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(vec) vec.begin(), vec.end()
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<P, P> PP;
const ll MOD = 10000;
const ll INF = 1000000010;
const int MAX = 100001;
int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int main() {
  int s[110];
  for (int i = 0; i < 110; i++) {
    s[i] = (i + 1) * (i + 2) * (i + 3) / 6;
  }
  int n;
  while (cin >> n, n) {
    int nn = n;
    int c = 0;
    while (n) {
      c++;
      int ss;
      for (int i = 0; i < 110; i++) {
        if (s[i] > n) {
          ss = s[i - 1];
          break;
        }
      }
      n -= ss;
    }
    cout << c << " ";
    n = nn;
    c = 0;
    while (n) {
      c++;
      int ss;
      for (int i = 0; i < 110; i++) {
        if (s[i] > n) {
          ss = i - 1;
          break;
        }
      }
      for (int i = ss; i >= 0; i--) {
        if (s[i] % 2) {
          ss = s[i];
          break;
        }
      }
      n -= ss;
    }
    cout << c << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = a; i >= n; i--)
#define INF 999999999
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<pii> VP;

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

int main() {
  int n;
  const int N = 1000000;
  int dp1[1000000];
  int dp2[1000000];

  rep(i, 0, N) dp1[i] = dp2[i] = i;

  int val = 1;
  for (int i = 1; val <= N; i++) {
    for (int j = 0; j + val <= N; j++) {
      dp1[j + val] = min(dp1[j + val], dp1[j] + 1);
      if (val % 2 == 1)
        dp2[j + val] = min(dp2[j + val], dp2[j] + 1);
    }
    val = i * (i + 1) * (i + 2) / 6;
  }

  while (cin >> n) {
    if (n == 0)
      break;
    cout << dp1[n] << " " << dp2[n] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = a; i >= n; i--)
#define INF 999999999
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<pii> VP;

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

int main() {
  int n;
  int a[1000];
  int b[1000];
  int dp1[1000000];
  int dp2[1000000];
  while (cin >> n) {
    if (n == 0)
      break;
    rep(i, 0, 1000) {
      a[i] = 0;
      b[i] = 0;
    }

    int cnt = 0;
    for (int i = 1; i * (i + 1) * (i + 2) / 6 <= n; i++) {
      int t = i * (i + 1) * (i + 2) / 6;
      a[i - 1] = t;

      if (t % 2 == 1) {
        b[cnt] = t;
        cnt++;
      }
    }

    /*
            for(int i = 1; i * (i+1) * (i+2) / 6 <= n; i++) {
                cout << a[i-1] << endl;
            }

            cout << endl;

            for(int i = 0; b[i] != 0; i++) {
                cout << b[i] << endl;
            }
    */

    rep(i, 0, 1000000) {
      dp1[i] = i;
      dp2[i] = i;
    }

    int k = 0;
    while (1) {
      if (a[k] != 0 || b[k] != 0) {
        rep(j, 0, n + 1) {
          dp1[j + a[k]] = min(dp1[j + a[k]], dp1[j] + 1);
          dp2[j + b[k]] = min(dp2[j + b[k]], dp2[j] + 1);
        }
      } else
        break;
      k++;
    }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define INF (1 << 25)
int N;
int pr[182];
int dp[1000002][2];
void init() {
  for (int i = 0; i <= 1000000; i++)
    for (int j = 0; j < 2; j++)
      dp[i][j] = INF;
}
void solve() {
  dp[0][0] = 0;
  dp[0][1] = 0;
  for (int i = 0; i <= 1000000; i++) {
    if (dp[i][0] == INF)
      continue;
    for (int j = 0; i + pr[j] <= 1000000; j++) {
      dp[i + pr[j]][0] = min(dp[i + pr[j]][0], dp[i][0] + 1);
      if (pr[j] & 1)
        dp[i + pr[j]][1] = min(dp[i + pr[j]][1], dp[i][1] + 1);
    }
  }
}

int main() {
  for (int i = 0; i < 182; i++)
    pr[i] = (i + 1) * (i + 2) * (i + 3) / 6;
  //  for(int i=0;i<102;i++) printf("%d\\n",pr[i]);
  //  for(int i=0;i<102;i++) if(!(i%2)) printf("%d\\n",pr[i]);
  init();
  solve();
  while (1) {
    scanf("%d", &N);
    if (!N)
      break;
    printf("%d %d\\n", dp[N][0], dp[N][1]);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define INF (1 << 25)
int N;
int pr[182];
int dp[1000002];
void init() {
  for (int i = 0; i <= N; i++)
    dp[i] = INF;
}
int solve(int g) {
  dp[0] = 0;
  for (int i = 0; i <= N; i++) {
    if (dp[i] == INF)
      continue;
    for (int j = 0; i + pr[j] <= N; j++) {
      if (g == 2 && pr[j] % g == 0)
        continue;
      dp[i + pr[j]] = min(dp[i + pr[j]], dp[i] + 1);
    }
  }
  return dp[N];
}

int main() {
  for (int i = 0; i < 182; i++)
    pr[i] = (i + 1) * (i + 2) * (i + 3) / 6;
  //  for(int i=0;i<102;i++) printf("%d\\n",pr[i]);
  //  for(int i=0;i<102;i++) if(!(i%2)) printf("%d\\n",pr[i]);
  while (1) {
    scanf("%d", &N);
    if (!N)
      break;
    init();
    int res1 = solve(1);
    init();
    int res2 = solve(2);
    printf("%d %d\\n", res1, res2);
  }
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
typedef long long ll;
using namespace std;
ll MOD = 1e9 + 7;

ll dp[1000010];
ll dp2[1000010];
int main(void) {
  ll n, i, j, k, h;
  std::vector<ll> v(200), v2(200);
  h = 0;
  k = 0;
  for (i = 1; (i * (i + 1) * (i + 2) / 6) <= 1000000; i++) {
    v[k++] = (i * (i + 1) * (i + 2) / 6);
    if ((i * (i + 1) * (i + 2) / 6) % 2)
      v2[h++] = (i * (i + 1) * (i + 2) / 6);
  }
  ll vs = k;

  fill(dp, dp + (1000000 + 1), 1000000);
  // cout << dp[0][0] << endl;
  dp[0] = 0;

  for (i = 1; i <= vs; i++) {
    for (j = v[i - 1]; j <= 1000000; j++) {
      dp[j] = min(dp[j - v[i - 1]] + 1, dp[j]);
    }
  }

  vs = h;
  fill(dp2, dp2 + (1000000 + 1), 1000000);
  dp2[0] = 0;
  for (i = 1; i <= vs; i++) {
    for (j = v2[i - 1]; j <= 1000000; j++) {
      dp2[j] = min(dp2[j - v2[i - 1]] + 1, dp2[j]);
    }
  }
  while (cin >> n && n) {
    cout << dp[n] << " " << dp2[n] << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
#include <vector>
typedef long long ll;
using namespace std;
ll MOD = 1e9 + 7;

ll dp[100][1000010];
ll dp2[100][1000010];
int main(void) {
  ll n, i, j;
  while (cin >> n && n) {
    std::vector<ll> v, v2;
    for (i = 1; (i * (i + 1) * (i + 2) / 6) <= n; i++) {
      v.push_back(i * (i + 1) * (i + 2) / 6);
      if ((i * (i + 1) * (i + 2) / 6) % 2)
        v2.push_back(i * (i + 1) * (i + 2) / 6);
    }
    ll vs = v.size();

    fill(dp[0], dp[0] + (vs + 1) * (n + 1), 1000000);
    // cout << dp[0][0] << endl;
    dp[0][0] = 0;

    for (i = 1; i <= vs; i++) {
      for (j = 0; j <= n; j++) {
        if (j - v[i - 1] < 0)
          dp[i][j] = dp[i - 1][j];
        else
          dp[i][j] = min(dp[i][j - v[i - 1]] + 1, dp[i - 1][j]);
      }
    }

    ll ret1 = dp[vs][n]; // 直にやるほう
    vs = v2.size();
    fill(dp2[0], dp2[0] + (vs + 1) * (n + 1), 1000000);
    dp2[0][0] = 0;
    for (i = 1; i <= vs; i++) {
      for (j = 0; j <= n; j++) {
        if (j - v2[i - 1] < 0)
          dp2[i][j] = dp2[i - 1][j];
        else
          dp2[i][j] = min(dp2[i][j - v2[i - 1]] + 1, dp2[i - 1][j]);
      }
    }
    ll ret2 = dp2[vs][n];
    cout << ret1 << " " << ret2 << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

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

#define NUM 200
vector<long long int> data1(NUM);
vector<long long int> data2;
vector<long long int> memo1;
vector<long long int> memo2;

long long int saiki1(long long int N) {
  if (N < 0) {
    return INT_MAX / 6;
  }
  if (memo1[N] != INT_MAX / 6) {
    return memo1[N];
  }
  long long int ret = INT_MAX / 6;
  auto ite = lower_bound(data1.begin(), data1.end(), N);
  while (ite != data1.begin()) {
    ret = min(ret, saiki1(N - (*ite)) + 1);
    ite--;
  }
  if (ret == INT_MAX / 6) {
    ret = N;
  }
  return memo1[N] = ret;
}

long long int saiki2(long long int N) {
  if (N < 0) {
    return INT_MAX / 6;
  }
  if (memo2[N] != INT_MAX / 6) {
    return memo2[N];
  }
  long long int ret = INT_MAX / 6;
  auto ite = lower_bound(data2.begin(), data2.end(), N);
  while (ite != data2.begin()) {
    ret = min(ret, saiki2(N - (*ite)) + 1);
    ite--;
  }

  if (ret == INT_MAX / 6) {
    ret = N;
  }
  return memo2[N] = ret;
}

int main() {
  memo1.resize(1e6, INT_MAX / 6);
  memo2.resize(1e6, INT_MAX / 6);
  iota(data1.begin(), data1.end(), 1);
  for (size_t i = 1; i < NUM; i++) {
    data1[i] += data1[i - 1];
  }
  for (size_t i = 1; i < NUM; i++) {
    data1[i] += data1[i - 1];
  }
  for (size_t i = 0; i < NUM; i++) {
    if (data1[i] % 2) {
      data2.emplace_back(data1[i]);
    }
  }
  long long int N;
  while (cin >> N, N) {
    cout << saiki1(N) << " " << saiki2(N) << endl;
  }
}

Error: 0

Fixed submission:

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

typedef pair<long long int, long long int> PLL;

vector<long long int> A(1000001, LLONG_MAX / 6), B(1000001, LLONG_MAX / 6);

long long int saikiA(long long int N) {
  if (A[N] == LLONG_MAX / 6) {
    for (size_t i = 1; i * 2 <= N; i++) {
      A[N] = min(A[N], saikiA(i) + saikiA(N - i));
    }
  }
  return A[N];
}

long long int saikiB(long long int N) {
  if (B[N] == LLONG_MAX / 6) {
    for (size_t i = 1; i * 2 <= N; i++) {
      B[N] = min(B[N], saikiB(i) + saikiB(N - i));
    }
  }
  return B[N];
}

int main() {
  vector<long long int> X, Y;
  for (size_t i = 1; i < 100; i++) {
    long long int now = i * (i + 1) * (i + 2) / 6;
    A[now] = 1;
    X.emplace_back(now);
    for (auto x : X) {
      A[now + x] = min(A[now + x], 2LL);
    }
    if (now % 2) {
      B[now] = 1;
      Y.emplace_back(now);
      for (auto x : Y) {
        A[now + x] = min(A[now + x], 2LL);
      }
    }
  }
  long long int N;
  while (cin >> N, N) {
    cout << saikiA(N) << " " << saikiB(N) << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define BORDER (int)1e6
#define INF (int)1e7

using namespace std;

int main() {
  vector<int> t(1, 0), t1(1, 0);
  int c = 1;
  while (t[c - 1] <= BORDER) {
    int temp;
    temp = c * (c + 1) * (c + 2) / 6;
    t.push_back(temp);
    if (temp % 2 == 1) {
      t1.push_back(temp);
    }
    c++;
  }

  vector<int> table(BORDER, 0), table1(BORDER, 0);

  for (int i = 1; i < BORDER; i++) {
    int ans = INF;
    for (int j = 1; j < t.size(); j++) {
      if (t[j] > i)
        break;
      if (i - t[j] >= 0) {
        ans = min(ans, table[i - t[j]] + 1);
      }
    }
    table[i] = ans;
    ans = INF;
    for (int j = 1; j < t1.size(); j++) {
      if (t1[j] > i)
        break;
      if (i - t[j] >= 0) {
        ans = min(ans, table1[i - t1[j]] + 1);
      }
    }
    table1[i] = ans;
  }

  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    cout << table[n] << " " << table1[n] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define INF 1e7

using namespace std;

int dp(int n, vector<int> tetra) {
  int num;
  num = distance(tetra.begin(), upper_bound(tetra.begin(), tetra.end(), n) - 1);

  vector<int> pre(n + 1, INF), now(n + 1, INF);

  for (int i = 0; i <= n; i++) {
    pre[i] = i;
  }
  now[0] = 0;
  for (int i = 2; i <= num; i++) {
    for (int j = 1; j <= n; j++) {
      if (j - tetra[i] >= 0) {
        now[j] = min(pre[j], now[j - tetra[i]] + 1);
      } else
        now[j] = pre[j];
    }
    now.swap(pre);
  }
  return pre[n];
}

int main() {
  vector<int> t(1, 0), t1(1, 0);
  int c = 1;
  while (t[c - 1] <= (int)1e6) {
    int temp;
    temp = c * (c + 1) * (c + 2) / 6;
    t.push_back(temp);
    if (temp % 2 == 1) {
      t1.push_back(temp);
    }
    c++;
  }
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    /*
    for(int i=0;i<s.size();i++){
            for(int j=0;j<s[i].size();j++){
                    if(s[i][j]==INF) cout << "0";
                    else cout << s[i][j];
                    cout << " ";
            }
            cout << endl;
    }
    */
    cout << dp(n, t) << " " << dp(n, t1) << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
#define INF 810114514
#define MAX 1000000

int dp[MAX];
int dp_odd[MAX];
int main() {
  dp[0] = 0;
  dp_odd[0] = 0;
  for (int i = 1; i < MAX; i++) {
    dp[i] = MAX + 1;
    dp_odd[i] = MAX + 1;
  }
  for (int n = 1; n * (n + 1) * (n + 2) / 6 < MAX; n++) {
    int p = n * (n + 1) * (n + 2) / 6;
    for (int i = 0; i + p < MAX; i++) {
      dp[i + p] = std::min(dp[i + p], dp[i] + 1);
      if (p & 1)
        dp_odd[i + p] = std::min(dp_odd[i + p], dp_odd[i] + 1);
    }
  }
  int x;
  scanf("%d", &x);
  while (x != 0) {
    printf("%d %d\\n", dp[x], dp_odd[x]);
    scanf("%d", &x);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>

int dp[100000];
int dp_odd[100000];
int main() {
  dp[0] = 0;
  dp_odd[0] = 0;
  for (int i = 1; i < 100000; i++) {
    dp[i] = -1;
    dp_odd[i] = -1;
  }
  for (int n = 1; n * (n + 1) * (n + 2) / 6 < 100000; n++) {
    bool isodd = false;
    int p = n * (n + 1) * (n + 2) / 6;
    if (p % 2 == 1)
      isodd = true;
    for (int k = 1; p * k < 100000; k++) {
      for (int i = 0; i + p * k < 100000; i++) {
        if (dp[i] != -1) {
          if (dp[i + p * k] != -1) {
            dp[i + p * k] = std::min(dp[i + p * k], dp[i] + k);
            if (isodd)
              dp_odd[i + p * k] = std::min(dp_odd[i + p * k], dp_odd[i] + k);
          } else {
            dp[i + p * k] = dp[i] + k;
            if (isodd)
              dp_odd[i + p * k] = dp_odd[i] + k;
          }
        }
      }
    }
  }
  int x;
  scanf("%d", &x);
  while (x != 0) {
    printf("%d %d\\n", dp[x], dp_odd[x]);
    scanf("%d", &x);
  }
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded


// Pollock's conjecture
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1167&lang=jp

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

const int MAX = 1000010;

int t(int n) { return n * (n + 1) * (n + 2) / 6; }
int main() {
  vector<int> dp(MAX, 0);
  vector<int> odd_dp(MAX, 0);
  dp[0] = 0;
  odd_dp[0] = 0;
  for (int n = 1; n <= MAX; ++n) {
    dp[n] = n;
    odd_dp[n] = n;
    for (int k = 1; t(k) <= n; ++k) {
      dp[n] = min(dp[n - t(k)] + 1, dp[n]);
      if (t(k) % 2) {
        odd_dp[n] = min(odd_dp[n - t(k)] + 1, odd_dp[n]);
      }
    }
  }

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

Error: TLE

Fixed submission:


// Pollock's conjecture
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1167&lang=jp

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

const int MAX = 1000010;
int N;

vector<int> dp;
vector<int> odd_dp;

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

int main() {
  while (cin >> N) {
    if (N == 0)
      break;
    dp.assign(MAX, 0);
    odd_dp.assign(MAX, 0);
    dp[0] = 0;
    odd_dp[0] = 0;
    for (int n = 1; n <= N; ++n) {
      dp[n] = n;
      odd_dp[n] = n;
      for (int k = 1; t(k) <= n; ++k) {
        dp[n] = min(dp[n - t(k)] + 1, dp[n]);
        if (t(k) % 2) {
          odd_dp[n] = min(odd_dp[n - t(k)] + 1, odd_dp[n]);
        }
      }
    }
    cout << dp[N] << " " << odd_dp[N] << endl;
  }
  return 0;
}