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

#include <algorithm>
#include <iostream>

using namespace std;

int dp1[1000010] = {0};
int dp2[1000010] = {0};
int List[200];

void setpolloc() {
  int i, j, li;
  for (i = 1; i * (i + 1) * (i + 2) / 6 <= 1000000; i++) {
    List[i] = i * (i + 1) * (i + 2) / 6;
    dp1[i * (i + 1) * (i + 2) / 6] = 1;
    if ((i * (i + 1) * (i + 2) / 6) % 2)
      dp2[i * (i + 1) * (i + 2) / 6] = 1;
  }
  List[i] = i * (i + 1) * (i + 2) / 6;
  for (i = 1; i <= 1000000; i++) {
    if (dp2[i])
      continue;
    for (j = 1; List[j] <= i - 1; j += 2)
      if (!dp2[i] || dp2[List[j]] + dp2[i - List[j]] < dp2[i])
        dp2[i] = dp2[List[j]] + dp2[i - List[j]];
    if (dp1[i])
      continue;
    for (j = 1; List[j] <= i - 1; j++)
      if (!dp1[i] || dp1[List[j]] + dp1[i - List[j]] < dp1[i])
        dp1[i] = dp1[List[j]] + dp1[i - List[j]];
  }
}

int main() {
  int n;
  setpolloc();
  while (cin >> n, n) {
    cout << dp1[n] << ' ' << dp2[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int dp1[1000010] = {0};
int dp2[1000010] = {0};
int List[200];

void setpolloc() {
  int i, j, li;
  for (i = 1; i * (i + 1) * (i + 2) / 6 < 1000000; i++) {
    List[i] = i * (i + 1) * (i + 2) / 6;
    dp1[i * (i + 1) * (i + 2) / 6] = 1;
    if ((i * (i + 1) * (i + 2) / 6) % 2)
      dp2[i * (i + 1) * (i + 2) / 6] = 1;
  }
  for (i = 1; i < 1000000; i++) {
    if (dp2[i])
      continue;
    for (j = 1; List[j] <= i - 1; j += 2)
      if (!dp2[i] || dp2[List[j]] + dp2[i - List[j]] < dp2[i])
        dp2[i] = dp2[List[j]] + dp2[i - List[j]];
    if (dp1[i])
      continue;
    for (j = 1; List[j] <= i - 1; j++)
      if (!dp1[i] || dp1[List[j]] + dp1[i - List[j]] < dp1[i])
        dp1[i] = dp1[List[j]] + dp1[i - List[j]];
  }
}

int main() {
  int n;
  setpolloc();
  while (cin >> n, n) {
    cout << dp1[n] << ' ' << dp2[n] << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
const int N = 1000100;

int t1[200];
int t2[200];
int dp1[N] = {0};
int dp2[N] = {0};

int main() {
  int i, j, k = 2, s;

  t1[1] = t2[1] = dp1[1] = dp2[1] = 1;
  for (i = 2; i < 200; i++) {
    t1[i] = t1[i - 1] * (i + 2) / (i - 1);
    if (t1[i] > 1e+6)
      break;
    dp1[t1[i]] = 1;
    if (t1[i] % 2) {
      dp2[t1[i]] = 1;
      t2[k++] = t1[i];
    }
  }

  for (i = 2; i < N; i++) {
    if (dp2[i])
      continue;
    for (j = 1; t2[j] < i && j < 200; j++) {
      if (!dp2[i] || (1 + dp2[i - t2[j]]) < (dp2[i])) {
        dp2[i] = 1 + dp2[i - t2[j]];
      }
    }
    if (dp1[i])
      continue;
    for (j = 1; t1[j] < i; j++) {
      if (!dp1[i] || (1 + dp1[i - t1[j]]) < (dp1[i])) {
        dp1[i] = 1 + dp1[i - t1[j]];
      }
    }
  }

  while (cin >> s, s)
    cout << dp1[s] << ' ' << dp2[s] << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
const int N = 1000100;

int t1[200];
int t2[200];
int dp1[N] = {0};
int dp2[N] = {0};

int main() {
  int i, j, k = 2, s;

  t1[1] = t2[1] = 1;
  dp1[1] = dp2[1] = 1;
  for (i = 2; i < N; i++) {
    t1[i] = t1[i - 1] * (i + 2) / (i - 1);
    if (t1[i] > N)
      break;
    dp1[t1[i]] = 1;
    if (t1[i] % 2) {
      dp2[t1[i]] = 1;
      t2[k++] = t1[i];
    }
  }

  for (i = 2; i < N; i++) {
    if (dp2[i])
      continue;
    for (j = 1; t2[j] < i; j++) {
      if (!dp2[i] || (1 + dp2[i - t2[j]]) < (dp2[i])) {
        dp2[i] = 1 + dp2[i - t2[j]];
      }
    }
    if (dp1[i])
      continue;
    for (j = 1; t1[j] < i; j++) {
      if (!dp1[i] || (1 + dp1[i - t1[j]]) < (dp1[i])) {
        dp1[i] = 1 + dp1[i - t1[j]];
      }
    }
  }

  while (cin >> s, s)
    cout << dp1[s] << ' ' << dp2[s] << endl;

  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define SZ(a) (int)((a).size())
#define mp make_pair
#define pb push_back
#define F first
#define S second
typedef pair<int, int> PI;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

int dp[1200000];
int odp[1200000];
int n;
void solve() {}

int main() {
  rep(i, 1200000) dp[i] = 1 << 28;
  rep(i, 1200000) odp[i] = 1 << 28;

  dp[0] = 0;
  odp[0] = 0;

  for (int i = 1;; ++i) {
    int p = i * (i + 1) * (i + 2) / 6;
    if (p > 1100000)
      break;
    // cout <<  i << endl;
    for (int j = 0; j + p < 1200000; ++j)
      dp[j + p] = min(dp[j + p], dp[j] + 1);
    if (p & 1)
      for (int j = 0; j + p < 1200000; ++j)
        odp[j + p] = min(odp[j + p], odp[j] + 1);
    // for(int j=110000-p;j>=0;--j)
    //   odp[j+p]=min(odp[j+p],odp[j]+1);
  }

  // return 0;

  while (cin >> n, n)
    cout << dp[n] << " " << odp[n] << endl;
}

Error: 0

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define SZ(a) (int)((a).size())
#define mp make_pair
#define pb push_back
#define F first
#define S second
typedef pair<int, int> PI;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

int dp[120000];
int odp[120000];
int n;
void solve() {}

int main() {
  rep(i, 120000) dp[i] = 1 << 28;
  rep(i, 120000) odp[i] = 1 << 28;

  dp[0] = 0;
  odp[0] = 0;

  for (int i = 1;; ++i) {
    int p = i * (i + 1) * (i + 2) / 6;
    if (p > 110000)
      break;
    // cout <<  i << endl;
    for (int j = 0; j + p < 120000; ++j)
      dp[j + p] = min(dp[j + p], dp[j] + 1);
    if (p & 1)
      for (int j = 0; j + p < 120000; ++j)
        odp[j + p] = min(odp[j + p], odp[j] + 1);
    // for(int j=110000-p;j>=0;--j)
    //   odp[j+p]=min(odp[j+p],odp[j]+1);
  }

  // return 0;

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

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

int main(void) {
  int size;
  vector<ll> v(200);
  for (ll i = 1;; i++) {
    if ((i * (i + 1) * (i + 2)) / 6 > 1000000)
      break;
    v[i - 1] = (i * (i + 1) * (i + 2)) / 6;
    size = i - 1;
  }
  int dp1[1000000]; // dp[i] := number of tetrahedron with value i
  int dp2[1000000]; // dp[i] := number of tetrahedron with value i
  fill(dp1, dp1 + 1000000 + 1, INT_MAX);
  fill(dp2, dp2 + 1000000 + 1, INT_MAX);
  dp1[0] = dp2[0] = 0;

  for (int i = 0; i <= 1000000; i++) {
    for (int j = 0; j < size; j++) {
      if (i + v[j] > 1000000)
        continue;
      dp1[i + v[j]] = min(dp1[i + v[j]], dp1[i] + 1);
    }
  }

  for (int i = 0; i <= 1000000; i++) {
    for (int j = 0; j < size; j++) {
      if (i + v[j] > 1000000)
        continue;
      if (v[j] % 2 == 0)
        continue;
      dp2[i + v[j]] = min(dp2[i + v[j]], dp2[i] + 1);
    }
  }

  while (1) {
    int x;
    cin >> x;
    if (x == 0)
      break;
    int ans = 0;
    cout << dp1[x] << " ";
    cout << dp2[x] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int size;
  vector<ll> v(200);
  for (ll i = 1;; i++) {
    if ((i * (i + 1) * (i + 2)) / 6 > 1000000)
      break;
    v[i - 1] = (i * (i + 1) * (i + 2)) / 6;
    size = i - 1;
  }

  while (1) {
    int x;
    cin >> x;
    if (x == 0)
      break;
    int ans = 0;
    int dp[1000000]; // dp[i] := number of tetrahedron with value i
    fill(dp, dp + x + 1, INT_MAX);
    dp[0] = 0;

    for (int i = 0; i <= x; i++) {
      for (int j = 0; j < size; j++) {
        if (i + v[j] > x)
          continue;
        dp[i + v[j]] = min(dp[i + v[j]], dp[i] + 1);
      }
    }

    cout << dp[x] << " ";

    fill(dp, dp + x + 1, INT_MAX);
    dp[0] = 0;
    for (int i = 0; i <= x; i++) {
      for (int j = 0; j < size; j++) {
        if (i + v[j] > x)
          continue;
        if (v[j] % 2 == 0)
          continue;
        dp[i + v[j]] = min(dp[i + v[j]], dp[i] + 1);
      }
    }
    cout << dp[x] << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

vector<int> tetra;
vector<int> oddTetra;
int dp[1000000];
int oddDp[1000000];
int N;

const int MAX = 1000000;

void initialize() {
  for (int i = 1;; i++) {
    int n = i * (i + 1) * (i + 2) / 6;
    if (n > MAX) {
      break;
    }
    tetra.push_back(n);
    if (n % 2 == 1) {
      oddTetra.push_back(n);
    }
  }
  fill(dp, dp + 1000000, MAX);
  fill(oddDp, oddDp + 1000000, MAX);
  /*
  for (int i = 0; i < tetra.size(); i++) {
    printf("%d\\n", tetra[i]);
  }
  */
}

void solve() {
  dp[0] = oddDp[0] = 0;
  for (int i = 1; i < MAX; i++) {
    for (int j = 0; j < tetra.size(); j++) {
      if (tetra[j] > i) {
        break;
      }
      dp[i] = min(dp[i], 1 + dp[i - tetra[j]]);
    }
    for (int j = 0; j < oddTetra.size(); j++) {
      if (oddTetra[j] > i) {
        break;
      }
      oddDp[i] = min(oddDp[i], 1 + oddDp[i - oddTetra[j]]);
    }
  }
}

int main() {
  initialize();
  solve();
  while (true) {
    scanf("%d ", &N);
    if (N == 0) {
      break;
    }
    printf("%d %d\\n", dp[N], oddDp[N]);
  }
}

Error: 0

Fixed submission:

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

vector<int> table;
vector<int> otable;

static const int MAX = 1000000;
int memo[MAX];
int omemo[MAX];

int tri(int N) {
  // printf("N = %d\\n", N);
  if (N == 0) {
    return 0;
  }
  int mn = MAX;
  if (memo[N] < 0) {
    for (int i = 0; N - table[i] >= 0; i++) {
      mn = min(mn, tri(N - table[i]) + 1);
    }
    memo[N] = mn;
  }
  return memo[N];
}

int otri(int N) {
  // printf("N = %d\\n", N);
  if (N == 0) {
    return 0;
  }
  int mn = MAX;
  if (omemo[N] < 0) {
    for (int i = 0; N - otable[i] >= 0; i++) {
      mn = min(mn, otri(N - otable[i]) + 1);
    }
    omemo[N] = mn;
    return mn;
  }
  return omemo[N];
}

int main() {
  int N;
  for (int i = 1; i * (i + 1) * (i + 2) / 6 <= MAX; i++) {
    table.push_back(i * (i + 1) * (i + 2) / 6);
    if ((i * (i + 1) * (i + 2) / 6) % 2 == 1) {
      otable.push_back(i * (i + 1) * (i + 2) / 6);
    }
  }

  fill(memo, &memo[MAX], -1);
  fill(omemo, &omemo[MAX], -1);

  while (scanf("%d ", &N) == 1) {
    if (N == 0) {
      break;
    }
    // printf("%d\\n", N);
    // printf("\\ntri:\\n");
    printf("%d ", tri(N));
    // printf("\\notri:\\n");
    printf("%d\\n", otri(N));
  }
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

const int MAX_V = 1000010;
unsigned int dp[MAX_V];
unsigned int dpodd[MAX_V];
set<int> tetras{-1};
set<int> oddtetras{-1};
int main() {
  _;
  int n;
  REP(i, MAX_V) dp[i] = dpodd[i] = -1;
  dp[0] = dpodd[0] = 0;
  for (int i = 1;; i++) {
    int k = i * (i + 1) * (i + 2) / 6;
    tetras.insert(k);
    if ((k >> 1) << 1 != k) // odd
      oddtetras.insert(k);
    if (k > MAX_V)
      break;
  }
  for (int i = 1; i < MAX_V; i++) {
    auto tetra = tetras.upper_bound(i);
    auto oddtetra = oddtetras.upper_bound(i);
    for (int j = 0; j < 200; j++) {
      int k = *(--tetra);
      if (k < 0)
        break;
      dp[i] = min(dp[i], dp[i - k] + 1);
    }
    for (int j = 0; j < 200; j++) {
      int k = *(--oddtetra);
      if (k < 0)
        break;
      dpodd[i] = min(dpodd[i], dpodd[i - k] + 1);
    }
  }
  while (cin >> n, n != 0) {
    cout << dp[n] << " " << dpodd[n] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

unsigned int dp[1000010];
map<int, int> memo;
int main() {
  _;
  int n;
  while (cin >> n, n != 0) {
    REP(i, n + 1) dp[i] = -1;
    dp[0] = 0;
    REP(i, n) {
      for (int j = 1;; j++) {
        int &k = memo[j];
        if (k == 0)
          k = j * (j + 1) * (j + 2) / 6;
        if (i + k > n)
          break;
        else
          dp[i + k] = min(dp[i + k], dp[i] + 1);
      }
    }
    cout << dp[n] << " ";
    REP(i, n + 1) dp[i] = -1;
    dp[0] = 0;
    REP(i, n) {
      for (int j = 1;; j++) {
        int &k = memo[j];
        if (k == 0)
          k = j * (j + 1) * (j + 2) / 6;
        if (((k >> 1) << 1) == k)
          continue;
        if (i + k > n)
          break;
        else
          dp[i + k] = min(dp[i + k], dp[i] + 1);
      }
    }
    cout << dp[n] << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

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

int dp[2][1000001];

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

int main() {
  int n;
  for (int k = 0; k < 2; k++)
    fill(dp[k], dp[k] + 1000001, 1000000);

  dp[0][0] = dp[1][0] = 0;
  for (int i = 1; i <= 1000000; i++) {
    for (int k = 1;; k++) {
      int num = f(k);
      if (i - num >= 0) {
        dp[0][i] = min(dp[0][i], dp[0][i - num] + 1);
        if ((num % 2) == 1) {
          dp[1][i] = min(dp[1][i], dp[1][i - num] + 1);
        }
      } else {
        break;
      }
    }
  }

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

Error: TLE

Fixed submission:

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

int dp[2][1000001];

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

int main() {
  int n;
  for (int k = 0; k < 2; k++)
    fill(dp[k], dp[k] + 1000001, 1000000);

  dp[0][0] = dp[1][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int k = 1;; k++) {
      int num = f(k);
      if (i - num >= 0) {
        dp[0][i] = min(dp[0][i], dp[0][i - num] + 1);
        if ((num % 2) == 1) {
          dp[1][i] = min(dp[1][i], dp[1][i - num] + 1);
        }
      } else {
        break;
      }
    }
  }

  while (cin >> n && n) {
    cout << dp[0][n] << " " << dp[1][n] << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int x[8000000][2], a[999], b[999], s, n;
int main() {
  a[0] = 1;
  b[0] = 1;
  for (int i = 1; i < 300; i++) {
    a[i] = a[i - 1] + (i + 1) * (i + 2) / 2;
    if (a[i] % 2 == 1) {
      s++;
      b[s] = a[i];
    }
  }
  n = 1200000;
  memset(x, 127, sizeof(x));
  x[0][0] = 0;
  x[0][1] = 0;
  for (int i = 0; i < 200; i++) {
    for (int j = 0; j < n; j++) {
      x[j + a[i]][0] = min(x[j + a[i]][0], x[j][0] + 1);
    }
  }
  for (int i = 0; i < 50; i++) {
    for (int j = 0; j < n; j++) {
      x[j + b[i]][1] = min(x[j + b[i]][1], x[j][1] + 1);
    }
  }
  while (true) {
    cin >> n;
    if (!n) {
      break;
    }
    cout << x[n][0] << ' ' << x[n][1] << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int x[9999999][2], a[999], b[999], s, n;
int main() {
  a[0] = 1;
  b[0] = 1;
  for (int i = 1; i < 300; i++) {
    a[i] = a[i - 1] + (i + 1) * (i + 2) / 2;
    if (a[i] % 2 == 1) {
      s++;
      b[s] = a[i];
    }
  }
  n = 1200000;
  memset(x, 127, sizeof(x));
  x[0][0] = 0;
  x[0][1] = 0;
  for (int i = 0; i < 200; i++) {
    for (int j = 0; j < n; j++) {
      x[j + a[i]][0] = min(x[j + a[i]][0], x[j][0] + 1);
    }
  }
  for (int i = 0; i < 50; i++) {
    for (int j = 0; j < n; j++) {
      x[j + b[i]][1] = min(x[j + b[i]][1], x[j][1] + 1);
    }
  }
  while (true) {
    cin >> n;
    if (!n) {
      break;
    }
    cout << x[n][0] << x[n][1] << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

constexpr int LIMIT = 1000000;
int dp1[LIMIT], dp2[LIMIT];

void solve() {
  std::fill(dp1, dp1 + LIMIT, 100);
  std::fill(dp2, dp2 + LIMIT, 100);
  dp1[0] = dp2[0] = 0;
  for (int n = 1; n < 200; ++n) {
    int const tet = n * (n + 1) * (n + 2) / 6;
    for (int i = 0; i + tet < LIMIT; ++i) {
      dp1[i + tet] = std::min(dp1[i + tet], dp1[i] + 1);
      if (tet % 2) {
        dp2[i + tet] = std::min(dp2[i + tet], dp2[i] + 1);
      }
    }
  }
}

int main() {
  solve();
  int N;
  while (std::cin >> N && N) {
    std::cout << dp1[N] << " " << dp2[N] << std::endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

// AOJ 1167
#include <iostream>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

typedef long long int number;

const number NMAX = 10e6 + 8;

int dp[NMAX], odp[NMAX];

int main() {
  REP(i, NMAX) { dp[i] = odp[i] = i; }
  for (number i = 2; i * (i + 1) * (i + 2) / 6 < NMAX; i++) {
    int tetr = i * (i + 1) * (i + 2) / 6;
    int limit = min(NMAX, (i + 1) * (i + 2) * (i + 3) / 6 + 8);
    for (number j = tetr; j < limit; j++) {
      dp[j] = min(dp[j], dp[j - tetr] + 1);
    }

    if (tetr % 2 != 0) {
      int limit = min(NMAX, (i + 4) * (i + 5) * (i + 6) / 6 + 8);
      for (number j = tetr; j < limit; j++) {
        odp[j] = min(odp[j], odp[j - tetr] + 1);
      }
    }
  }

  int n;
  while (cin >> n and n) {
    cout << dp[n] << " " << odp[n] << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MAX = 1000000;
int tri[200];
int dp[MAX], o_dp[MAX];

int main() {
  for (int i = 0; i < 200; i++) {
    tri[i] = i * (i + 1) * (i + 2) / 6;
  }

  dp[0] = 0;
  o_dp[0] = 0;

  for (int i = 1; i < MAX; i++) {
    dp[i] = i;
    o_dp[i] = i;

    for (int j = 1; i - tri[j] >= 0; j++) {
      dp[i] = min(dp[i], dp[i - tri[j]] + 1);

      if (tri[j] % 2 == 1) {
        o_dp[i] = min(o_dp[i], o_dp[i - tri[j]] + 1);
      }
    }
  }

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

    if (n == 0) {
      break;
    }

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

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  vector<int> tri(200);

  for (int i = 0; i < 200; i++) {
    tri[i] = i * (i + 1) * (i + 2) / 6;
  }

  vector<int> ans1(1000000);
  vector<int> ans2(1000000);

  ans1[0] = 0;
  ans2[0] = 0;

  for (int i = 1; i < 1000000; i++) {
    ans1[i] = i;
    ans2[i] = i;

    for (int j = 0; i - tri[j] >= 0; j++) {
      ans1[i] = min(ans1[i], ans1[i - tri[j]] + 1);

      if (tri[j] % 2 == 1) {
        ans2[i] = min(ans2[i], ans2[i - tri[j]] + 1);
      }
    }
  }

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

    cout << ans1[n] << " " << ans2[n] << endl;
  }

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

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << S << endl
using namespace std;

#define M 1000000

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

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

int main() {
  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) {
      update(odd, x);
    }
  }

  int n;
  while (cin >> n, n) {
    printf("%d %d\\n", all[n], odd[n]);
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << S << endl
using namespace std;

void pol(vector<int> vt, set<int> s, int N) {
  int count = 1;
  while (s.find(N) == s.end()) {
    count++;
    set<int> tmps;
    for (auto itr = s.begin(); itr != s.end(); itr++) {
      for (auto itr2 = vt.begin(); itr2 != vt.end(); itr2++) {
        tmps.insert(*itr + *itr2);
      }
    }
    s = tmps;
  }

  cout << count << " ";

  return;
}

int main() {
  int N;
  while (cin >> N, N) {
    int num = 0, dnum = 0;
    set<int> s, sodd;
    vector<int> vt, vtodd;
    for (int i = 1; num <= N; i++) {
      dnum += i;
      num += dnum;
      if (num > N)
        break;
      vt.push_back(num);
      s.insert(num);
      if (num % 2) {
        sodd.insert(num);
        vtodd.push_back(num);
      }
    }
    pol(vt, s, N);
    pol(vtodd, sodd, N);
    p("");
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

const int MAX_N = 1e+6;
const int INF = 1e+8;

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

int dp[2][MAX_N + 1] = {0};

int main() {
  for (int i = 0; i < MAX_N; i++) {
    dp[0][i] = dp[1][i] = INF;
  }
  for (int i = 1; f(i) < MAX_N; i++) {
    dp[0][f(i)] = 1;
    if (f(i) % 2)
      dp[1][f(i)] = 1;
  }
  for (int i = 1; i < MAX_N; i++) {
    if (dp[0][i] != INF) {
      for (int j = 1; i + f(j) < MAX_N; j++) {
        dp[0][i + f(j)] = min(dp[0][i + f(j)], dp[0][i] + 1);
      }
    }
    if (dp[1][i] != INF) {
      for (int j = 1; i + f(j) < MAX_N; j++) {
        if (f(j) % 2)
          dp[1][i + f(j)] = min(dp[1][i + f(j)], dp[1][i] + 1);
      }
    }
  }

  int n;
  while (cin >> n, n) {
    cout << dp[0][n] << " " << dp[1][n] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

const int MAX_N = 1e+6;
const int INF = 1e+8;

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

int main() {
  int n;
  while (cin >> n, n) {
    int dp[MAX_N + 1];
    for (int i = 0; i <= n; i++) {
      dp[i] = INF;
    }
    for (int i = 1; f(i) <= n; i++) {
      dp[f(i)] = 1;
    }
    for (int i = 1; i <= n; i++) {
      if (dp[i] != INF) {
        for (int j = 1; dp[i] + f(j) <= n; j++) {
          dp[i + f(j)] = min(dp[i + f(j)], dp[i] + 1);
        }
      }
    }
    cout << dp[n] << " ";
    for (int i = 0; i <= n; i++) {
      dp[i] = INF;
    }
    for (int i = 1; f(i) <= n; i++) {
      if (f(i) % 2)
        dp[f(i)] = 1;
    }
    for (int i = 1; i <= n; i++) {
      if (dp[i] != INF) {
        for (int j = 1; dp[i] + f(j) <= n; j++) {
          if (f(j) % 2)
            dp[i + f(j)] = min(dp[i + f(j)], dp[i] + 1);
        }
      }
    }
    cout << dp[n] << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

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

const int MAX = 1000100;

vector<int> tetras;
vector<int> oddTetras;

int dp[MAX];
int oddDp[MAX];

int main() {
  for (int i = 1; i < 200; i++) {
    int n = i * (i + 1) * (i + 2) / 6;
    tetras.push_back(n);
    if (n % 2 == 1)
      oddTetras.push_back(n);
  }

  for (int i = 0; i < MAX; i++) {
    dp[i] = oddDp[i] = i;
  }
  for (int i = 1; i < MAX; i++) {
    for (int j = 0; tetras[j] <= i; j++) {
      dp[i] = min(dp[i], dp[i - tetras[j]] + 1);
    }
    for (int j = 0; oddTetras[j] <= i; j++) {
      oddDp[i] = min(oddDp[i], oddDp[i - oddTetras[j]] + 1);
    }
  }

  int n;
  while (cin >> n, n != 0) {
    cout << dp[n] << " " << oddDp[n] << endl;
  }
}

Error: 0

Fixed submission:

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

const int MAX = 100010;

vector<int> tetras;
vector<int> oddTetras;

int dp[MAX];
int oddDp[MAX];

int main() {
  for (int i = 1; i < 200; i++) {
    int n = i * (i + 1) * (i + 2) / 6;
    tetras.push_back(n);
    if (n % 2 == 1)
      oddTetras.push_back(n);
  }

  for (int i = 0; i < MAX; i++) {
    dp[i] = oddDp[i] = i;
  }
  for (int i = 1; i < MAX; i++) {
    for (int j = 0; tetras[j] <= i; j++) {
      dp[i] = min(dp[i], dp[i - tetras[j]] + 1);
    }
    for (int j = 0; oddTetras[j] <= i; j++) {
      oddDp[i] = min(oddDp[i], oddDp[i - oddTetras[j]] + 1);
    }
  }

  int n;
  while (cin >> n, n != 0) {
    cout << dp[n] << " " << oddDp[n] << endl;
  }
}