Problem p02690 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// std::numeric_limits<int>::max(), min()
// std::setprecision(X)
// std::islower(), std::isupper(), std::tolower, std::toupper
// std::accumulate(m.begin(), m.end(), 0LL);
// std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less);
// std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt,
// less) : if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*"))
// ) std::cout << "YES" << std::endl;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll check(ll x) {
  ll l = 0, r = 100;
  while (r - l > 1) {
    ll mid = (l + r) / 2;
    ll c = std::pow(mid, 5);
    if (c < x)
      l = mid;
    else
      r = mid;
  }
  return r;
}

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  ll X;
  std::cin >> X;

  for (ll b = -1000; b <= 1000; b++) {
    ll a5 = b * b * b * b * b + X;
    ll a = check(a5);
    if (a5 == a * a * a * a * a) {
      std::cout << a << " " << b << std::endl;
      return 0;
    }
  }
  assert(0);
  return 0;
}


Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// std::numeric_limits<int>::max(), min()
// std::setprecision(X)
// std::islower(), std::isupper(), std::tolower, std::toupper
// std::accumulate(m.begin(), m.end(), 0LL);
// std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less);
// std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt,
// less) : if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*"))
// ) std::cout << "YES" << std::endl;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll check(ll x) {
  ll l = -1000, r = 1000;
  while (r - l > 1) {
    ll mid = (l + r) / 2;
    ll c = std::pow(mid, 5);
    if (c < x)
      l = mid;
    else
      r = mid;
  }
  return r;
}

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  ll X;
  std::cin >> X;

  for (ll b = -1000; b <= 1000; b++) {
    ll a5 = b * b * b * b * b + X;
    ll a = check(a5);
    if (a5 == a * a * a * a * a) {
      std::cout << a << " " << b << std::endl;
      return 0;
    }
  }
  assert(0);
  return 0;
}