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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

int main() {

  ll x;
  cin >> x;
  for (ll i = -1; i <= 1000; i++) {

    ll xi = pow(i, 5);
    //  cout<<xi<<"xi"<<endl;
    ll k = xi - x;
    bool f = false;
    if (k < 0) {
      f = true;
      k *= -1;
    }
    //  cout<<k<<" k"<<endl;

    if (k == 0) {
      cout << i << " " << 0 << endl;
      return 0;
    }
    map<ll, ll> m;

    if (k == 1) {
      cout << i << " " << (f ? -1 : 1) << endl;
      return 0;
    }

    for (int j = 2; j <= sqrt(k); j++) {
      while (k % j == 0) {
        // cout<<k<<endl;
        k /= j;
        m[j]++;
      }
    }

    ll ans = 1;

    bool ff = true;

    if (m.size() == 0 || k != 1)
      ff = false;
    for (auto &a : m) {
      // cout<<a.first<<" "<<a.second<<"a";
      if (a.second % 5 == 0) {
        ll ji = a.second / 5;
        ans *= pow(a.first, ji);
      } else {
        ff = false;
        break;
      }
    }

    // cout<<endl;

    if (ff == true) {
      cout << i << " " << (f ? -1 * ans : ans) << endl;
      return 0;
    }
  }

  return 0;
}


Error: -8

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

int main() {

  ll x;
  cin >> x;
  for (ll i = 0; i <= 10000; i++) {
    ll xi = pow(i, 5);
    ll k = xi - x;
    bool f = false;
    if (k < 0) {
      f = true;
      k *= -1;
    }
    map<ll, ll> m;
    // cout<<k<<endl;

    if (k == 1) {
      cout << i << " " << (f ? -1 : 1) << endl;
      return 0;
    }

    for (int j = 2; j <= sqrt(k); j++) {
      while (k % i == 0) {
        k /= j;
        m[j]++;
      }
    }

    ll ans = 0;
    bool ff = true;

    if (m.size() == 0)
      ff = false;

    for (auto &a : m) {
      // cout<<a.first<<" "<<a.second;
      if (a.second % 5 == 0) {
        ans += a.first * (a.second / 5);
      } else {
        ff = false;
        break;
      }
    }

    if (ff == true) {
      cout << i << " " << (f ? -1 * ans : ans) << endl;
      return 0;
    }
  }

  return 0;
}