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

#include <iostream>
#include <vector>

using namespace std;

int n;
vector<int> vec, vec2;
int dp[1000005], dp2[1000005];

int main(void) {
  for (long long i = 1; i * (i + 1) * (i + 2) / 6 <= 1000000; i++) {
    long long x = i * (i + 1) * (i + 2) / 6;
    if (x % 2)
      vec2.push_back(x);
    vec.push_back(x);
  }
  // for(int i = 0 ;i < vec.size(); i++) cout << vec[i] << " "; cout << endl;

  for (int i = 0; i <= 1000000; i++)
    dp[i] = dp2[i] = 1e9;
  dp[0] = dp2[0] = 0;
  for (int i = 0; i < 1000000; i++) {
    for (int j = 0; j < vec.size(); j++) {
      if (i + vec[j] <= 1000000)
        dp[i + vec[j]] = min(dp[i + vec[j]], dp[i] + 1);
    }
    for (int j = 0; j < vec2.size(); j++) {
      if (i + vec2[j] <= 1000000)
        dp2[i + vec2[j]] = min(dp2[i + vec2[j]], dp2[i] + 1);
    }
  }

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


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int n;
vector<int> vec, vec2;
int dp[1000005], dp2[1000005];

int main(void) {
  for (long long i = 1; i * (i + 1) * (i + 2) / 6 <= 1000000; i++) {
    long long x = i * (i + 1) * (i + 2) / 6;
    if (x % 2)
      vec2.push_back(x);
    vec.push_back(x);
  }
  // for(int i = 0 ;i < vec.size(); i++) cout << vec[i] << " "; cout << endl;

  while (1) {
    cin >> n;
    if (n == 0)
      break;

    for (int i = 0; i <= n; i++)
      dp[i] = dp2[i] = 1e9;
    dp[0] = dp2[0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < vec.size(); j++) {
        if (i + vec[j] <= n)
          dp[i + vec[j]] = min(dp[i + vec[j]], dp[i] + 1);
      }
      for (int j = 0; j < vec2.size(); j++) {
        if (i + vec2[j] <= n)
          dp2[i + vec2[j]] = min(dp2[i + vec2[j]], dp2[i] + 1);
      }
    }
    // for(int i = 0; i <= n; i++) cout<< dp[i] << " "; cout << endl;
    cout << dp[n] << " " << dp2[n] << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;
static const int INF = 999999999;
vector<int> tetrahedral;
int min_depth;
int min_odd_depth;
int n;

void DFS(int pos, int depth, int sum) {
  if (depth >= min_depth) {
    return;
  }
  if (sum == n) {
    if (depth < min_depth) {
      min_depth = depth;
    }
    return;
  } else if (sum > n) {
    return;
  } else {
    for (int i = pos; i >= 0; --i) {
      DFS(i, depth + 1, sum + tetrahedral[i]);
    }
  }
}

void DFS2(int pos, int depth, int sum) {
  if (depth >= min_odd_depth) {
    return;
  }
  if (sum == n) {
    if (depth < min_odd_depth) {
      min_odd_depth = depth;
    }
    return;
  } else if (sum > n) {
    return;
  } else {
    for (int i = pos; i >= 0; --i) {
      if (tetrahedral[i] % 2) {
        DFS2(i, depth + 1, sum + tetrahedral[i]);
      }
    }
  }
}

void solve() {
  for (int i = 1; i * (i + 1) * (i + 2) / 6 <= 1000000; ++i) {
    tetrahedral.push_back(i * (i + 1) * (i + 2) / 6);
  }
  while (cin >> n, n) {
    min_depth = INF;
    min_odd_depth = INF;
    int pos = 0;
    while (tetrahedral[pos] < n) {
      ++pos;
    }
    for (int i = pos; i >= 0; --i) {
      DFS(i, 1, tetrahedral[i]);
    }
    while (tetrahedral[pos] % 2 == 0) {
      --pos;
    }
    for (int i = pos; i >= 0; --i) {
      if (tetrahedral[i] % 2) {
        DFS2(i, 1, tetrahedral[i]);
      }
    }
    cout << min_depth << " " << min_odd_depth << endl;
  }
}

int main() {
  solve();
  return (0);
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
static const int INF = 999999999;
vector<int> tetrahedral;
int min_depth;
int min_odd_depth;
int n;

void DFS(int pos, int depth, int sum) {
  if (sum == n) {
    if (depth < min_depth) {
      min_depth = depth;
    }
    return;
  } else if (sum > n) {
    return;
  } else {
    for (int i = pos; i >= 0; --i) {
      DFS(i, depth + 1, sum + tetrahedral[i]);
    }
  }
}

void DFS2(int pos, int depth, int sum) {
  if (sum == n) {
    if (depth < min_odd_depth) {
      min_odd_depth = depth;
    }
    return;
  } else if (sum > n) {
    return;
  } else {
    for (int i = pos; i >= 0; --i) {
      if (tetrahedral[i] % 2) {
        DFS2(i, depth + 1, sum + tetrahedral[i]);
      }
    }
  }
}

void solve() {
  for (int i = 1; i * (i + 1) * (i + 2) / 6 <= 1000000; ++i) {
    tetrahedral.push_back(i * (i + 1) * (i + 2) / 6);
  }
  while (cin >> n, n) {
    min_depth = INF;
    min_odd_depth = INF;
    int pos = 0;
    while (tetrahedral[pos] < n) {
      ++pos;
    }
    for (int i = pos; i >= 0; --i) {
      DFS(i, 1, tetrahedral[i]);
    }
    while (tetrahedral[pos] % 2 == 0) {
      --pos;
    }
    for (int i = pos; i >= 0; --i) {
      if (tetrahedral[i] % 2) {
        DFS2(i, 1, tetrahedral[i]);
      }
    }
    cout << min_depth << " " << min_odd_depth << endl;
  }
}

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

#include <iostream>
using namespace std;

int dpt1[1000001];
int dpt2[1000001];
bool dpb1[1000001];
bool dpb2[1000001];
int tr[1000];
int sq[1000];

int dp1(int s) {
  if (dpb1[s])
    return dpt1[s];

  int m = 1000111000;
  int r;
  for (int i = 1; i < 1000; i++) {
    if (s - sq[i] < 0)
      break;
    r = dp1(s - sq[i]);
    if (r < m)
      m = r;
  }

  dpt1[s] = m + 1;
  dpb1[s] = true;
  return m + 1;
}

int dp2(int s) {
  if (dpb2[s])
    return dpt2[s];

  int m = 1000111000;
  int r;
  for (int i = 1; i < 1000; i++) {
    if (s - sq[i] < 0)
      break;
    if (sq[i] % 2 == 0)
      continue;
    r = dp2(s - sq[i]);
    if (r < m)
      m = r;
  }

  dpt2[s] = m + 1;
  dpb2[s] = true;
  return m + 1;
}

int main() {

  for (int i = 1; i < 1000; i++) {
    tr[i] = tr[i - 1] + i;
    sq[i] = sq[i - 1] + tr[i];
  }
  dpt1[0] = 0;
  dpb1[0] = true;
  dpt2[0] = 0;
  dpb2[0] = true;
  int n;
  for (int i = 1; i <= 1000000; i++)
    dp1(i), dp2(i);
  while (cin >> n, n) {
    cout << dp1(n) << " " << dp2(n) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int dpt1[1000001];
int dpt2[1000001];
bool dpb1[1000001];
bool dpb2[1000001];
int tr[1000];
int sq[1000];

int dp1(int s) {
  if (dpb1[s])
    return dpt1[s];

  int m = 1000111000;
  int r;
  for (int i = 1; i < 1000; i++) {
    if (s - sq[i] < 0)
      break;
    r = dp1(s - sq[i]);
    if (r < m)
      m = r;
  }

  dpt1[s] = m + 1;
  dpb1[s] = true;
  return m + 1;
}

int dp2(int s) {
  if (dpb2[s])
    return dpt2[s];

  int m = 1000111000;
  int r;
  for (int i = 1; i < 1000; i++) {
    if (s - sq[i] < 0)
      break;
    if (sq[i] % 2 == 0)
      continue;
    r = dp2(s - sq[i]);
    if (r < m)
      m = r;
  }

  dpt2[s] = m + 1;
  dpb2[s] = true;
  return m + 1;
}

int main() {

  for (int i = 1; i < 1000; i++) {
    tr[i] = tr[i - 1] + i;
    sq[i] = sq[i - 1] + tr[i];
  }
  dpt1[0] = 0;
  dpb1[0] = true;
  dpt2[0] = 0;
  dpb2[0] = true;
  int n;
  while (cin >> n, n) {
    cout << dp1(n) << " " << dp2(n) << endl;
  }

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define chmin(a, b) ((a) = min((a), (b)))
#define chmax(a, b) ((a) = max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
const double eps = 1e-10;

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

int main() {
  vector<int> v(201);
  for (int i = 1; i <= 200; i++) {
    v[i] = i * (i + 1) * (i + 2) / 6;
  }

  vector<int> dp1(1000010, 1e9);
  dp1[0] = 0;
  auto dp2 = dp1;

  for (int i = 1; i <= 200; i++) {
    for (int j = 0; j <= 1000005; j++) {
      if (j - v[i] >= 0)
        dp1[j] = min(dp1[j], dp1[j - v[i]] + 1);

      if (v[i] % 2) {
        if (j - v[i] >= 0)
          dp2[j] = min(dp2[j], dp2[j - v[i]] + 1);
      }
    }
  }

  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define chmin(a, b) ((a) = min((a), (b)))
#define chmax(a, b) ((a) = max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
const double eps = 1e-10;

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

int main() {
  vector<int> v(201);
  for (int i = 1; i <= 200; i++) {
    v[i] = i * (i + 1) * (i + 2) / 6;
  }

  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    vector<int> dp1(n + 10, 1e9);
    dp1[0] = 0;
    auto dp2 = dp1;

    for (int i = 1; i <= 200; i++) {
      for (int j = 0; j <= n; j++) {
        if (j - v[i] >= 0)
          dp1[j] = min(dp1[j], dp1[j - v[i]] + 1);

        if (v[i] % 2) {
          if (j - v[i] >= 0)
            dp2[j] = min(dp2[j], dp2[j - v[i]] + 1);
        }
      }
    }

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

#include <cmath>
#include <iostream>
#include <string.h>

using namespace std;

int f1[1000001];
int f2[1000001];

int main() {
  int t_num[182];
  int t_num_odd[182];

  int k = 0;
  for (int i = 1; i < 182; ++i) {
    t_num[i - 1] = i * (i + 1) * (i + 2) / 6;
    if (t_num[i - 1] % 2 == 1) {
      t_num_odd[k] = t_num[i - 1];
      ++k;
    }
  }

  memset(f1, 1000001, sizeof(f1));
  f1[0] = 0;
  for (int j = 0; j < 181; ++j) {
    for (int i = t_num[j]; i <= 1000000; ++i) {
      if (i >= t_num[j])
        f1[i] = min(f1[i], f1[i - t_num[j]] + 1);
    }
  }

  memset(f2, 1000001, sizeof(f2));
  f2[0] = 0;
  for (int j = 0; j < k; ++j) {
    for (int i = t_num_odd[j]; i <= 1000000; ++i) {
      if (i >= t_num_odd[j])
        f2[i] = min(f2[i], f2[i - t_num_odd[j]] + 1);
    }
  }

  int n;
  cin >> n;

  while (n) {

    cout << f1[n] << " ";
    cout << f2[n] << endl;
    cin >> n;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <string.h>

using namespace std;

int f[1000001];

int main() {
  int t_num[182];
  int t_num_odd[182];

  int k = 0;
  for (int i = 1; i < 182; ++i) {
    t_num[i - 1] = i * (i + 1) * (i + 2) / 6;
    if (t_num[i - 1] % 2 == 1) {
      t_num_odd[k] = t_num[i - 1];
      ++k;
    }
  }

  int n;
  cin >> n;

  while (n) {
    memset(f, 1000001, sizeof(f));
    f[0] = 0;
    for (int j = 0; j < 181; ++j) {
      for (int i = t_num[j]; i <= n; ++i) {
        if (i >= t_num[j])
          f[i] = min(f[i], f[i - t_num[j]] + 1);
      }
    }
    cout << f[n] << " ";

    memset(f, 1000001, sizeof(f));
    f[0] = 0;
    for (int j = 0; j < k; ++j) {
      for (int i = t_num_odd[j]; i <= n; ++i) {
        if (i >= t_num_odd[j])
          f[i] = min(f[i], f[i - t_num_odd[j]] + 1);
      }
    }
    cout << f[n] << endl;

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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int inf = 1e9;
const int maxN = 1000000;

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

vector<int> makeS(int mod, int d) {
  vector<int> res;
  for (int n = 1; f(n) <= maxN; ++n) {
    if (f(n) % mod == d) {
      res.push_back(f(n));
    }
  }
  return res;
}
vector<int> solve(const vector<int> &s) {
  vector<int> dp(maxN + 1, inf);
  dp[0] = 0;
  for (int i = 0; i < s.size(); ++i) {
    for (int j = 0; j <= maxN; ++j) {
      int nj = j + s[i];
      if (nj <= maxN) {
        dp[nj] = min(dp[nj], dp[j] + 1);
      }
    }
  }
  return dp;
}

int main() {
  vector<int> s0 = makeS(1, 0);
  vector<int> s1 = makeS(2, 1);
  vector<int> dp1 = solve(s0);
  vector<int> dp2 = solve(s1);
  int N;
  while (cin >> N, N) {
    cout << dp1[N] << " " << dp2[N] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int N;

typedef long long ll;

const int inf = 1e9;
const int maxN = 1000000;
int dp[maxN + 1];

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

vector<int> makeS(int mod, int d) {
  vector<int> res;
  for (int n = 1; f(n) <= maxN; ++n) {
    if (f(n) % mod == d) {
      res.push_back(f(n));
    }
  }
  return res;
}
int solve(const vector<int> &s) {
  fill(dp, dp + N + 1, inf);
  dp[0] = 0;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] > N)
      break;
    for (int j = 0; j <= N; ++j) {
      int nj = j + s[i];
      if (nj <= N) {
        dp[nj] = min(dp[nj], dp[j] + 1);
      }
    }
  }
  return dp[N];
}

int main() {
  vector<int> s0 = makeS(1, 0);
  vector<int> s1 = makeS(2, 1);
  while (cin >> N, N) {
    cout << solve(s0) << " " << solve(s1) << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

int main() {
  int result[1000010] = {};
  int odd_result[1000010] = {};
  fill(result, result + 1000000, 1000000);
  fill(odd_result, odd_result + 1000000, 1000000);
  int trangular_sum = 0, tetrahedral_sum = 0;
  result[0] = 0;
  odd_result[0] = 0;
  for (int i = 1; i < 1000001; i++) {
    trangular_sum += i;
    tetrahedral_sum += trangular_sum;
    if (tetrahedral_sum > 1000000)
      break;
    // result[trangular_sum] = 1;
    result[tetrahedral_sum] = 1;
    if (tetrahedral_sum % 2 == 1)
      odd_result[tetrahedral_sum] = 1;
    for (int j = tetrahedral_sum + 1; j < 1000001; j++) {
      result[j] = min(result[j], result[j - tetrahedral_sum] + 1);
      if (tetrahedral_sum % 2 == 1)
        odd_result[j] = min(odd_result[j], odd_result[j - tetrahedral_sum] + 1);
    }
  }
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cout << result[n] << " " << odd_result[n] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

int main() {
  int result[1000001] = {};
  int odd_result[1000001] = {};
  fill(result, result + 1000000, 1000000);
  fill(odd_result, odd_result + 1000000, 1000000);
  int trangular_sum = 0, tetrahedral_sum = 0;
  result[0] = 0;
  odd_result[0] = 0;
  for (int i = 1; tetrahedral_sum < 1000000; i++) {
    trangular_sum += i;
    tetrahedral_sum += trangular_sum;
    // result[trangular_sum] = 1;
    result[tetrahedral_sum] = 1;
    if (tetrahedral_sum % 2 == 1)
      odd_result[tetrahedral_sum] = 1;
  }
  for (int i = 1; i < 1000000; i++) {
    for (int j = 1; j < i; j++) {
      result[i] = min(result[i], result[i - j] + result[j]);
      if (j % 2 == 1)
        odd_result[i] = min(odd_result[i], odd_result[i - j] + odd_result[j]);
    }
    // cout << i  << " " << result[i] << " " << odd_result[i] << endl;
  }
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cout << result[n] << " " << odd_result[n] << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define POL_MAX 200
#define N_MAX 1000000
using namespace std;

int N;
int dp[N_MAX + 1];
int odd_dp[N_MAX + 1];
const int INF = INT_MAX / 3;

int main() {

  int n = 1;
  int value = 0;
  vector<int> pol, kisu_pol;
  while (true) {
    value = n * (n + 1) * (n + 2) / 6;
    if (value >= N_MAX) {
      break;
    }
    pol.push_back(value);
    if (value % 2 != 0) {
      kisu_pol.push_back(value);
    }
    n++;
  }

  rep(j, N_MAX + 1) { dp[j] = j; }
  REP(j, 1, N_MAX + 1) {
    REP(i, 1, pol.size()) {
      if (j < pol[i])
        break;
      dp[j] = min(dp[j], dp[j - pol[i]] + 1);
    }
  }

  rep(j, N_MAX + 1) { odd_dp[j] = j; }
  REP(j, 1, N_MAX + 1) {
    REP(i, 1, kisu_pol.size()) {
      if (j < kisu_pol[i])
        break;
      odd_dp[j] = min(odd_dp[j], odd_dp[j - kisu_pol[i]] + 1);
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define POL_MAX 200
#define N_MAX 1000000
using namespace std;

int N;
int dp[N_MAX + 1];
const int INF = INT_MAX / 3;

int solve(vector<int> &my_pol) {
  rep(j, N + 1) { dp[j] = j; }

  int m = my_pol.size();
  int ans = INF;
  REP(j, 1, N + 1) {
    REP(i, 1, m) {
      if (j < my_pol[i])
        break;
      dp[j] = min(dp[j], dp[j - my_pol[i]] + 1);
    }
  }

  return dp[N];
}

int main() {

  int n = 1;
  int value = 0;
  vector<int> pol, kisu_pol;
  while (true) {
    value = n * (n + 1) * (n + 2) / 6;
    if (value >= N_MAX) {
      break;
    }
    pol.push_back(value);
    if (value % 2 != 0) {
      kisu_pol.push_back(value);
    }
    n++;
  }

  while (cin >> N) {
    if (N == 0) {
      break;
    }
    cout << solve(pol) << " " << solve(kisu_pol) << endl;
  }

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

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

using namespace std;
vector<int> P;
int memo[1001001];
int mome[1001001];

void hoge() {

  for (int i = 1;; i++) {
    int p = i * (i + 1) * (i + 2) / 6;
    if (p >= 1001001)
      break;
    P.push_back(p);
  }
  for (int i = 0; i < 1001001; i++) {
    memo[i] = mome[i] = i;
  }
  for (int i = 0; i < P.size(); i++) {
    memo[P[i]] = 1;
    if (P[i] % 2)
      mome[P[i]] = 1;
  }
  for (int i = 0; i < 1001001; i++) {
    for (int j = 0; j < P.size(); j++) {
      if (i + P[j] < 1001001) {
        memo[i + P[j]] = min(memo[i + P[j]], memo[i] + 1);
      }
      if ((P[j] % 2) && i + P[j] < 1001001) {
        mome[i + P[j]] = min(mome[i + P[j]], mome[i] + 1);
      }
    }
  }
}

int main() {

  hoge();
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;

    cout << memo[N] << " " << mome[N] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;
vector<int> P;
vector<int> oddP;
int memo[1001001];
int mome[1001001];

void hoge() {

  for (int i = 1;; i++) {
    int p = i * (i + 1) * (i + 2) / 6;
    if (p >= 1001001)
      break;
    P.push_back(p);
  }
  for (int i = 0; i < P.size(); i++) {
    memo[P[i]] = 1;
    if (P[i] % 2)
      mome[P[i]] = 1;
  }
}

int rec(int n, bool odd) {

  if (odd) {
    if (mome[n] != 0)
      return mome[n];
  } else {
    if (memo[n] != 0)
      return memo[n];
  }
  int ret = 1001001;
  for (int i = 0; n - P[i] > 0; i++) {
    if (odd && (P[i] % 2 == 0))
      continue;
    ret = min(ret, rec(n - P[i], odd) + 1);
  }
  if (odd) {
    return mome[n] = ret;
  } else {
    return memo[n] = ret;
  }
}

int main() {

  hoge();
  bool odd = true;
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;

    cout << rec(N, !odd) << " ";
    cout << rec(N, odd) << endl;
  }

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

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

int D[181] = {}; // 10^6 越え → 181
int dp_map[2][1000002] = {};

void t() {
  for (int i = 1; i < 181; i++) {
    D[i] = i * (i + 1) * (i + 2) / 6;
  }
}

void dp() {

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

  for (int i = 0; i <= 1000000; i++) {
    for (int j = 1; j < 181; j++) {
      if (i + D[j] <= 1000000)
        dp_map[0][i + D[j]] = min(dp_map[0][i + D[j]], dp_map[0][i] + 1);
      if (D[j] % 2 == 1 && i + D[j] <= 1000000)
        dp_map[1][i + D[j]] = min(dp_map[1][i + D[j]], dp_map[1][i] + 1);
    }
  }
}

int main() {

  t();
  dp();

  int n;

  while (cin >> n, n) {
    cout << dp_map[0][n] << ' ';
    cout << dp_map[1][n] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int D[182] = {}; // 10^6 越え → 181
int dp_map[2][1000002] = {};

void t() {
  for (int i = 1; i < 182; i++) {
    D[i] = i * (i + 1) * (i + 2) / 6;
  }
}

int cal_2(int x, int c) {
  if (x == 0)
    return 0;
  else {
    // if( dp_map[0][x] != 0 ) return dp_map[0][x] ;
    // else
    return x / D[c] + cal_2(x % D[c], c - 1);
  }
}

int cal(int m, int q) {
  int ans = m;
  for (int j = q; j > 1; j--) {
    ans = min(ans, cal_2(m, j));
  }
  return ans;
}

int cal_odd_2(int x, int c) {
  if (x == 0)
    return 0;
  else {
    if (D[c] % 2 == 0)
      return cal_odd_2(x, c - 1);
    else {
      // if( dp_map[1][x] != 0 ) return dp_map[1][x] ;
      // else
      return x / D[c] + cal_odd_2(x % D[c], c - 1);
    }
  }
}

int cal_odd(int m, int q) {
  int ans_2 = m;
  for (int j = q; j > 1; j--) {
    ans_2 = min(ans_2, cal_odd_2(m, j));
  }
  return ans_2;
}

void dp() {
  int p = 1;
  for (int i = 1; i <= 1000000; i++) {
    if (p < 182 && i >= D[p])
      p++;
    dp_map[0][i] = cal(i, p);
    dp_map[1][i] = cal_odd(i, p);
  }
}

int main() {

  t();
  dp();

  int n;

  while (cin >> n, n) {
    cout << dp_map[0][n] << ' ';
    cout << dp_map[1][n] << endl;
  }

  return 0;
}