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

#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
const double EPS = 1e-9;
#define INF (1LL << 60)
#define D double
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl;
#define SP << " " <<
typedef pair<int, int> P;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

template <int mod = MOD> class modint {
private:
  int mMOD = mod, mVal;

public:
  constexpr modint(int v = 0) noexcept : mVal(v % mMOD) {
    if (mVal < 0)
      mVal += mMOD;
  }

  constexpr int getMOD() { return mMOD; }

  constexpr modint operator-() const noexcept { return mVal ? mMOD - mVal : 0; }

  constexpr modint operator+(const modint &r) const noexcept {
    return modint(*this) += r;
  }

  constexpr modint operator-(const modint &r) const noexcept {
    return modint(*this) -= r;
  }

  constexpr modint operator*(const modint &r) const noexcept {
    return modint(*this) *= r;
  }

  modint operator/(const modint &r) const noexcept {
    return modint(*this) /= r;
  }

  constexpr modint &operator+=(const modint &r) noexcept {
    mVal += r.mVal;
    if (mVal > mMOD)
      mVal -= mMOD;
    return *this;
  }

  constexpr modint &operator-=(const modint &r) noexcept {
    mVal -= r.mVal;
    if (mVal < 0)
      mVal += mMOD;
    return *this;
  }

  constexpr modint &operator*=(const modint &r) noexcept {
    mVal = mVal * r.mVal % mMOD;
    return *this;
  }

  modint &operator/=(const modint &r) noexcept {
    int a = r.mVal, b = mMOD, u = 1, v = 0;
    while (b) {
      int t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    mVal = mVal * u % mMOD;
    if (mVal < 0)
      mVal += mMOD;
    return *this;
  }

  constexpr bool operator==(const modint &r) const noexcept {
    return this->mVal == r.mVal;
  }

  constexpr bool operator!=(const modint &r) const noexcept {
    return this->mVal != r.mVal;
  }

  constexpr bool operator>(const modint &r) const noexcept {
    return this->mVal > r.mVal;
  }

  constexpr bool operator<(const modint &r) const noexcept {
    return this->mVal < r.mVal;
  }

  constexpr bool operator>=(const modint &r) const noexcept {
    return this->mVal >= r.mVal;
  }

  constexpr bool operator<=(const modint &r) const noexcept {
    return this->mVal <= r.mVal;
  }

  friend ostream &operator<<(ostream &os, const modint &x) noexcept {
    return os << x.mVal;
  }

  friend istream &operator>>(istream &is, modint &x) noexcept {
    return is >> x.mVal;
  }

  constexpr operator int() const noexcept { return mVal; }

  constexpr modint operator+(const int &r) const noexcept {
    return modint(*this) += r;
  }

  constexpr modint operator-(const int &r) const noexcept {
    return modint(*this) -= r;
  }

  constexpr modint operator*(const int &r) const noexcept {
    return modint(*this) *= r;
  }

  modint operator/(const int &r) const noexcept { return modint(*this) /= r; }

  constexpr modint &operator+=(const int &r) noexcept {
    mVal += r;
    if (mVal > mMOD)
      mVal -= mMOD;
    return *this;
  }

  constexpr modint &operator-=(const int &r) noexcept {
    mVal -= r;
    if (mVal < 0)
      mVal += mMOD;
    return *this;
  }

  constexpr modint &operator*=(const int &r) noexcept {
    mVal = mVal * r % mMOD;
    return *this;
  }

  modint &operator/=(const int &r) noexcept {
    int a = r, b = mMOD, u = 1, v = 0;
    while (b) {
      int t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    mVal = mVal * u % mMOD;
    if (mVal < 0)
      mVal += mMOD;
    return *this;
  }

  constexpr bool operator==(const int &r) const noexcept {
    return this->mVal == r;
  }

  constexpr bool operator!=(const int &r) const noexcept {
    return this->mVal != r;
  }

  constexpr bool operator>(const int &r) const noexcept {
    return this->mVal > r;
  }

  constexpr bool operator<(const int &r) const noexcept {
    return this->mVal < r;
  }

  constexpr bool operator>=(const int &r) const noexcept {
    return this->mVal >= r;
  }

  constexpr bool operator<=(const int &r) const noexcept {
    return this->mVal <= r;
  }
};

using mint = modint<INF>; // MODを入力

constexpr mint modpow(const mint &a, int n) noexcept {
  if (n == 0)
    return 1;
  auto t = modpow(a, n / 2);
  t = t * t;
  if (n & 1)
    t = t * a;
  return t;
}

mint modconv(mint n, mint r) noexcept {
  if (n < r)
    return 0;
  else {
    mint res = 1;
    for (int i = 0; i < r; i++) {
      res *= n - i;
    }
    for (int i = 0; i < r; i++) {
      res /= r - i;
    }
    return res;
  }
}

constexpr int mpow(const int &a, int n) noexcept {
  if (n == 0)
    return 1;
  auto t = mpow(a, n / 2);
  t = t * t;
  if (n & 1)
    t = t * a;
  return t;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);

  int x;
  cin >> x;

  FOR(a, -118, 120) FOR(b, -119, 119) {
    if (mpow(a, 5) - mpow(b, 5) == x) {
      cout << a SP b << endl;
      return 0;
    }
  }

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
const double EPS = 1e-9;
#define INF (1LL << 60)
#define D double
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl;
#define SP << " " <<
typedef pair<int, int> P;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

template <int mod = MOD> class modint {
private:
  int mMOD = mod, mVal;

public:
  constexpr modint(int v = 0) noexcept : mVal(v % mMOD) {
    if (mVal < 0)
      mVal += mMOD;
  }

  constexpr int getMOD() { return mMOD; }

  constexpr modint operator-() const noexcept { return mVal ? mMOD - mVal : 0; }

  constexpr modint operator+(const modint &r) const noexcept {
    return modint(*this) += r;
  }

  constexpr modint operator-(const modint &r) const noexcept {
    return modint(*this) -= r;
  }

  constexpr modint operator*(const modint &r) const noexcept {
    return modint(*this) *= r;
  }

  modint operator/(const modint &r) const noexcept {
    return modint(*this) /= r;
  }

  constexpr modint &operator+=(const modint &r) noexcept {
    mVal += r.mVal;
    if (mVal > mMOD)
      mVal -= mMOD;
    return *this;
  }

  constexpr modint &operator-=(const modint &r) noexcept {
    mVal -= r.mVal;
    if (mVal < 0)
      mVal += mMOD;
    return *this;
  }

  constexpr modint &operator*=(const modint &r) noexcept {
    mVal = mVal * r.mVal % mMOD;
    return *this;
  }

  modint &operator/=(const modint &r) noexcept {
    int a = r.mVal, b = mMOD, u = 1, v = 0;
    while (b) {
      int t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    mVal = mVal * u % mMOD;
    if (mVal < 0)
      mVal += mMOD;
    return *this;
  }

  constexpr bool operator==(const modint &r) const noexcept {
    return this->mVal == r.mVal;
  }

  constexpr bool operator!=(const modint &r) const noexcept {
    return this->mVal != r.mVal;
  }

  constexpr bool operator>(const modint &r) const noexcept {
    return this->mVal > r.mVal;
  }

  constexpr bool operator<(const modint &r) const noexcept {
    return this->mVal < r.mVal;
  }

  constexpr bool operator>=(const modint &r) const noexcept {
    return this->mVal >= r.mVal;
  }

  constexpr bool operator<=(const modint &r) const noexcept {
    return this->mVal <= r.mVal;
  }

  friend ostream &operator<<(ostream &os, const modint &x) noexcept {
    return os << x.mVal;
  }

  friend istream &operator>>(istream &is, modint &x) noexcept {
    return is >> x.mVal;
  }

  constexpr operator int() const noexcept { return mVal; }

  constexpr modint operator+(const int &r) const noexcept {
    return modint(*this) += r;
  }

  constexpr modint operator-(const int &r) const noexcept {
    return modint(*this) -= r;
  }

  constexpr modint operator*(const int &r) const noexcept {
    return modint(*this) *= r;
  }

  modint operator/(const int &r) const noexcept { return modint(*this) /= r; }

  constexpr modint &operator+=(const int &r) noexcept {
    mVal += r;
    if (mVal > mMOD)
      mVal -= mMOD;
    return *this;
  }

  constexpr modint &operator-=(const int &r) noexcept {
    mVal -= r;
    if (mVal < 0)
      mVal += mMOD;
    return *this;
  }

  constexpr modint &operator*=(const int &r) noexcept {
    mVal = mVal * r % mMOD;
    return *this;
  }

  modint &operator/=(const int &r) noexcept {
    int a = r, b = mMOD, u = 1, v = 0;
    while (b) {
      int t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    mVal = mVal * u % mMOD;
    if (mVal < 0)
      mVal += mMOD;
    return *this;
  }

  constexpr bool operator==(const int &r) const noexcept {
    return this->mVal == r;
  }

  constexpr bool operator!=(const int &r) const noexcept {
    return this->mVal != r;
  }

  constexpr bool operator>(const int &r) const noexcept {
    return this->mVal > r;
  }

  constexpr bool operator<(const int &r) const noexcept {
    return this->mVal < r;
  }

  constexpr bool operator>=(const int &r) const noexcept {
    return this->mVal >= r;
  }

  constexpr bool operator<=(const int &r) const noexcept {
    return this->mVal <= r;
  }
};

using mint = modint<INF>; // MODを入力

constexpr mint modpow(const mint &a, int n) noexcept {
  if (n == 0)
    return 1;
  auto t = modpow(a, n / 2);
  t = t * t;
  if (n & 1)
    t = t * a;
  return t;
}

mint modconv(mint n, mint r) noexcept {
  if (n < r)
    return 0;
  else {
    mint res = 1;
    for (int i = 0; i < r; i++) {
      res *= n - i;
    }
    for (int i = 0; i < r; i++) {
      res /= r - i;
    }
    return res;
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);

  int x;
  cin >> x;

  vec list;
  vector<bool> b(10e9, false);
  vec c(x);
  int a = 0;
  int t = modpow(a, 5);
  while (t <= x) {
    list.push_back(a);
    c[t] = a;
    b[t] = true;
    a++;
    t = modpow(a, 5);
  }

  REP(i, list.size()) {
    if (b[x + modpow(list[i], 5)]) {
      cout << c[x + modpow(list[i], 5)] SP list[i] << endl;
      break;
    } else if (x - modpow(list[i], 5) >= 0 && b[x - modpow(list[i], 5)]) {
      cout << c[x - modpow(list[i], 5)] SP - list[i] << endl;
      break;
    }
  }

  return 0;
}