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

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

#define ll long long
#define inf 1000000007
#define mod 1000000007
#define pa pair<int, int>
#define pal pair<ll, ll>
#define ppa pair<int, pa>
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

class Point {
public:
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}
  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double a) { return Point(x * a, y * a); }
  Point operator/(double a) { return Point(x / a, y / a); }
  double absv() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; }
  bool operator==(const Point &p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};
typedef Point Vector;

struct Segment {
  Point p1, p2;
};

double hen(Vector a) {
  if (fabs(a.x) < EPS && a.y > 0)
    return acos(0);
  else if (fabs(a.x) < EPS && a.y < 0)
    return 3 * acos(0);
  else if (fabs(a.y) < EPS && a.x < 0)
    return 2 * acos(0);
  else if (fabs(a.y) < EPS && a.x > 0)
    return 0.0;
  else if (a.y > 0)
    return acos(a.x / a.absv());
  else
    return 2 * acos(0) + acos(-a.x / a.absv());
}

double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }
double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ru[11] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};

ll frac[100010];
ll inv[100010];

ll saikif(ll a, int n) {
  if (n == 1)
    return a % mod;
  else if (n % 2 == 0) {
    ll q = saikif(a, n / 2);
    return (q * q) % mod;
  } else
    return (a * saikif(a, n - 1)) % mod;
}

void gyakugen() {
  frac[0] = 1;
  for (int i = 1; i <= 100000; i++)
    frac[i] = (frac[i - 1] * i) % mod;
  for (int i = 0; i <= 100000; i++)
    inv[i] = saikif(frac[i], mod - 2);
}

//----------------kokomade temple------------
int dp[1000002];
int dp2[1000002];
vector<int> ve;
vector<int> ve2;
int num, num2;

int dfs(int n) {
  if (dp[n] < inf)
    return dp[n];
  else {
    //  cout<<n<<endl;
    int ans = inf;
    for (int j = 0; j < num; j++) {
      if (ve[j] >= n)
        break;

      ans = min(ans, 1 + dfs(n - ve[j]));
    }
    dp[n] = ans;
    return ans;
  }
}

int dfs2(int n) {
  if (dp2[n] < inf)
    return dp2[n];
  else {
    //  cout<<n<<endl;
    int ans = inf;
    for (int j = 0; j < num2; j++) {
      if (ve2[j] >= n)
        break;

      ans = min(ans, 1 + dfs2(n - ve2[j]));
    }
    dp2[n] = ans;
    return ans;
  }
}

int main() {
  fill(dp, dp + 1000002, inf);
  fill(dp2, dp2 + 1000002, inf);
  int z = 1, w;
  while (1) {
    w = z * (z + 1) * (z + 2) / 6;
    // cout<<w<<endl;
    if (w > 1000000)
      break;
    else {
      ve.push_back(w);
      if (w % 2 == 1) {
        ve2.push_back(w);
        dp2[w] = 1;
      }
      z++;
      dp[w] = 1;
    }
  }
  num = ve.size();
  num2 = ve2.size();
  for (int i = 1; i < 1000001; i++) {
    dfs(i);
    dfs2(i);
  }

  int n;

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

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

#define ll long long
#define inf 1000000007
#define mod 1000000007
#define pa pair<int, int>
#define pal pair<ll, ll>
#define ppa pair<int, pa>
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

class Point {
public:
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}
  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double a) { return Point(x * a, y * a); }
  Point operator/(double a) { return Point(x / a, y / a); }
  double absv() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; }
  bool operator==(const Point &p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};
typedef Point Vector;

struct Segment {
  Point p1, p2;
};

double hen(Vector a) {
  if (fabs(a.x) < EPS && a.y > 0)
    return acos(0);
  else if (fabs(a.x) < EPS && a.y < 0)
    return 3 * acos(0);
  else if (fabs(a.y) < EPS && a.x < 0)
    return 2 * acos(0);
  else if (fabs(a.y) < EPS && a.x > 0)
    return 0.0;
  else if (a.y > 0)
    return acos(a.x / a.absv());
  else
    return 2 * acos(0) + acos(-a.x / a.absv());
}

double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }
double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ru[11] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};

ll frac[100010];
ll inv[100010];

ll saikif(ll a, int n) {
  if (n == 1)
    return a % mod;
  else if (n % 2 == 0) {
    ll q = saikif(a, n / 2);
    return (q * q) % mod;
  } else
    return (a * saikif(a, n - 1)) % mod;
}

void gyakugen() {
  frac[0] = 1;
  for (int i = 1; i <= 100000; i++)
    frac[i] = (frac[i - 1] * i) % mod;
  for (int i = 0; i <= 100000; i++)
    inv[i] = saikif(frac[i], mod - 2);
}

//----------------kokomade temple------------
int dp[100002];
int dp2[100002];
vector<int> ve;
vector<int> ve2;
int num, num2;

int dfs(int n) {
  if (dp[n] < inf)
    return dp[n];
  else {
    //  cout<<n<<endl;
    int ans = inf;
    for (int j = 0; j < num; j++) {
      if (ve[j] >= n)
        break;

      ans = min(ans, 1 + dfs(n - ve[j]));
    }
    dp[n] = ans;
    return ans;
  }
}

int dfs2(int n) {
  if (dp2[n] < inf)
    return dp2[n];
  else {
    //  cout<<n<<endl;
    int ans = inf;
    for (int j = 0; j < num2; j++) {
      if (ve2[j] >= n)
        break;

      ans = min(ans, 1 + dfs2(n - ve2[j]));
    }
    dp2[n] = ans;
    return ans;
  }
}

int main() {
  fill(dp, dp + 100002, inf);
  fill(dp2, dp2 + 100002, inf);
  int z = 1, w;
  while (1) {
    w = z * (z + 1) * (z + 2) / 6;
    // cout<<w<<endl;
    if (w > 100000)
      break;
    else {
      ve.push_back(w);
      if (w % 2 == 1) {
        ve2.push_back(w);
        dp2[w] = 1;
      }
      z++;
      dp[w] = 1;
    }
  }
  num = ve.size();
  num2 = ve2.size();
  for (int i = 1; i < 100001; i++) {
    dfs(i);
    dfs2(i);
  }

  int n;

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

  return 0;
}