Problem p02690 (C++) — buggy submission, verdict: Time Limit Exceeded

// Mahir Ratanpara (DA-IICT)
#include <bits/stdc++.h>
using namespace std;
#define Ff(i, a, n) for (i = a; i < n; i++)
#define Fr(i, a, n) for (i = a; i > n; i--)
#define ll long long
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
typedef pair<ll, ll> pa;
typedef vector<ll> vc;
typedef vector<pa> vp;
typedef vector<vc> vvc;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================

vc g[N];

int main() {
  FAST;
  ll x;
  cin >> x;
  for (int i = -120; i < 121; i++) {
    for (int j = -120; j < 121; j++) {
      if (i * i * i * i * i - j * j * j * j * j == x) {
        cout << i << " " << j << endl;
        return 0;
      }
    }
  }
  return 0;
}

int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1)
      result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}

void ipgraph(int n, int m) {
  int i, u, v;
  while (m--) {
    cin >> u >> v;
    g[u - 1].pb(v - 1);
    g[v - 1].pb(u - 1);
  }
}

void dfs(int u, int par) {
  for (int v : g[u]) {
    if (v == par)
      continue;
    dfs(v, u);
  }
}

Error: TLE

Fixed submission:

// Mahir Ratanpara (DA-IICT)
#include <bits/stdc++.h>
using namespace std;
#define Ff(i, a, n) for (i = a; i < n; i++)
#define Fr(i, a, n) for (i = a; i > n; i--)
#define ll long long
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
typedef pair<ll, ll> pa;
typedef vector<ll> vc;
typedef vector<pa> vp;
typedef vector<vc> vvc;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================

vc g[N];

int floorRoot5(int n) {
  // Base cases
  if (n == 0 || n == 1)
    return n;

  // Do Binary Search for floor of 5th square root
  int low = 1, high = n, ans = 0;
  while (low <= high) {
    // Find the middle point and its power 5
    int mid = (low + high) / 2;
    long int mid5 = mid * mid * mid * mid * mid;

    // If mid is the required root
    if (mid5 == n)
      return mid;

    // Since we need floor, we update answer when
    // mid5 is smaller than n, and move closer to
    // 5'th root
    if (mid5 < n) {
      low = mid + 1;
      ans = mid;
    } else // If mid^5 is greater than n
      high = mid - 1;
  }
  return ans;
}

int main() {
  FAST;
  ll t, i, j, k, p, q, r, x, y, u, v, n, m;
  cin >> x;
  ll maxi = floorRoot5(x);
  for (int b = -maxi; b <= maxi; b++) {
    ll a = x + b * b * b * b * b;
    ll lol = floorRoot5(a);
    if (lol * lol * lol * lol * lol == a) {
      cout << lol << " " << b;
      break;
    }
  }
  return 0;
}

int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1)
      result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}

void ipgraph(int n, int m) {
  int i, u, v;
  while (m--) {
    cin >> u >> v;
    g[u - 1].pb(v - 1);
    g[v - 1].pb(u - 1);
  }
}

void dfs(int u, int par) {
  for (int v : g[u]) {
    if (v == par)
      continue;
    dfs(v, u);
  }
}