Problem p00748 (C++) — buggy submission, verdict: Runtime Error

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int MAX_N = 182;
int num[MAX_N];
VI odd;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  num[0] = 0;
  for (int i = 1; i < MAX_N; ++i) {
    num[i] = i * (i + 1) * (i + 2) / 6;
    if (num[i] % 2 == 1)
      odd.PB(num[i]); // i % 4 == 1
  }

  VI pr;
  REP(i, MAX_N) REP(j, MAX_N) pr.PB(num[i] + num[j]);
  SORT(pr);

  VI dp(1000000 + 1, 123456789);
  dp[0] = 0;
  for (int i = 1; i < 1000000 + 1; ++i) {
    for (int j = 0; j < SZ(odd); ++j) {
      if (i < odd[j])
        break;
      dp[i] = min(dp[i], dp[i - odd[j]] + 1);
    }
  }

  int N;
  while (cin >> N, N) {
    int cnt = 0;
    if (*lower_bound(num, num + MAX_N, N) == N)
      cnt = 1;
    else if (*lower_bound(ALL(pr), N) == N)
      cnt = 2;
    else {
      REP(i, SZ(pr)) {
        int x = N - pr[i];
        if (x < 0)
          break;
        if (*lower_bound(num, num + MAX_N, x) == x) {
          cnt = 3;
          break;
        }
        if (*lower_bound(ALL(pr), x) == x) {
          cnt = 4;
        }
      }
      if (!cnt)
        cnt = 5;
    }
    cout << cnt << " " << dp[N] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int MAX_N = 182;
int num[MAX_N];
VI odd;

int bfs(int x) {
  queue<int> q[2];
  int crt = 0, nxt = 1;
  q[crt].push(x);

  int ret = 0;
  while (true) {
    ++ret;
    while (!q[crt].empty()) {
      x = q[crt].front();
      q[crt].pop();
      REP(i, SZ(odd)) {
        if (x < odd[i])
          break;
        if (x == odd[i])
          return ret;
        q[nxt].push(x - odd[i]);
      }
    }

    swap(crt, nxt);
  }
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  num[0] = 0;
  for (int i = 1; i < MAX_N; ++i) {
    num[i] = i * (i + 1) * (i + 2) / 6;
    if (num[i] % 2 == 1)
      odd.PB(num[i]);
  }

  VI pr;
  REP(i, MAX_N) REP(j, MAX_N) pr.PB(num[i] + num[j]);
  SORT(pr);

  int N;
  while (cin >> N, N) {
    int cnt = 0;
    if (*lower_bound(num, num + MAX_N, N) == N)
      cnt = 1;
    else if (*lower_bound(ALL(pr), N) == N)
      cnt = 2;
    else {
      REP(i, SZ(pr)) {
        int x = N - pr[i];
        if (x < 0)
          break;
        if (*lower_bound(num, num + MAX_N, x) == x) {
          cnt = 3;
          break;
        }
        if (*lower_bound(ALL(pr), x) == x) {
          cnt = 4;
        }
      }
      if (!cnt)
        cnt = 5;
    }
    cout << cnt << " " << bfs(N) << endl;
  }

  return 0;
}