Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second
#define Pqaz(T) priority_queue<T, vector<T>, greater<T>>
#define Pqza(T) priority_queue<T>
#define put(x) cout << x;
#define putsp(x) cout << x << ' ';
#define putbd cout << "---------------------------------------------" << endl;
#define putln(x) cout << x << endl;
#define debug(x) cerr << #x << "=" << x << endl;
#define ENJYU std::ios::sync_with_stdio(false) std::cin.tie(0);

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;
typedef complex<double> comp;
typedef vector<int> vec;
typedef vector<ll> vecll;
typedef vector<double> vecd;
typedef vector<vec> mat;
typedef vector<vecll> matll;
typedef vector<vecd> matd;

template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}

const int MAX = 1000000;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1 << 29;
const ll INFL = 1e18;
const ll MOD = 1000000007;

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

//********************************template
//END****************************************//

void solve() {
  vec memo, odds;
  int n = 1, res = -1;
  while (res <= MAX) {
    res = n * (n + 1) * (n + 2) / 6;
    memo.pb(res);
    if (res & 1)
      odds.pb(res);
    n++;
  }

  vec dp1(MAX + 1, INF), dp2;
  dp1[0] = 0;
  dp2 = dp1;

  rep(MAX, m) {
    dp1[m] = min(dp1[m], m);
    dp2[m] = min(dp2[m], m);
    for (int x : memo) {
      if (m < x)
        break;
      dp1[m] = min(dp1[m - x] + 1, dp1[m]);
    }
    for (int x : odds) {
      if (m < x)
        break;
      dp2[m] = min(dp2[m - x] + 1, dp2[m]);
    }
  }

  while (~scanf("%d", &n)) {
    if (n == 0)
      break;
    cout << dp1[n] << ' ' << dp2[n] << endl;
  }
}

int main() {
  solve();
  // cout << "yui(*-v・)yui" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define uniq(x) (x).erase(unique(all(x)), (x).end())

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;
typedef std::priority_queue<int> IntPrioQueue; // Z->A
typedef std::priority_queue<int, std::vector<int>, std::greater<int>>
    IntReversePrioQueue; // A->Z

const int MAX = 1000010;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1 << 29;
const int MOD = 1000000007;

vector<int> sq, odsq;
int N, mn[MAX], od[MAX];

void solve(void) {
  int n = 1;
  fill(mn, mn + MAX, INF);
  fill(od, od + MAX, INF);
  mn[0] = od[0] = 0;
  while (n * (n + 1) * (n + 2) / 6 < MAX) {
    sq.pb(n * (n + 1) * (n + 2) / 6);
    mn[n * (n + 1) * (n + 2) / 6] = 1;
    if (n * (n + 1) * (n + 2) / 6 % 2) {
      odsq.pb(n * (n + 1) * (n + 2) / 6);
      od[n * (n + 1) * (n + 2) / 6] = 1;
    }
    n++;
  }
  Rep(1, MAX - 1, k) {
    int mini = INF, miniod = INF;
    for (int q : sq) {
      if (q >= k)
        break;
      mini = min(mini, k / q + mn[k % q]);
    }
    for (int q : odsq) {
      if (q >= k)
        break;
      miniod = min(miniod, k / q + od[k % q]);
    }
    mn[k] = min(mini, mn[k]);
    od[k] = min(miniod, od[k]);
  }
  while (1) {
    scanf("%d\\n", &N);
    if (N == 0)
      break;
    printf("%d %d\\n", mn[N], od[N]);
  }
}

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