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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\\n" : "No\\n")
#define endl '\\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;
using vs = vector<string>;
using vb = vector<bool>;
using vc = vector<char>;
using vcc = vector<vc>;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 62) - 1;
ll ceil(const ll &a, const ll &b) { return ((a) + (b)-1) / b; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> T Mypow(T a, int n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0) {
    T res = Mypow(a, n / 2);
    return res * res;
  }
  return a * Mypow(a, n - 1);
}

const ll MX = 200;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll x;
  cin >> x;

  for (ll a = 1; a <= MX; a++) {
    for (ll b = -MX; b <= a; b++) {
      ll tmp = Mypow(a, 5) - Mypow(b, 5);
      if (tmp == x) {
        cout << a << ' ' << b << endl;
        return 0;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\\n" : "No\\n")
#define endl '\\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;
using vs = vector<string>;
using vb = vector<bool>;
using vc = vector<char>;
using vcc = vector<vc>;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 62) - 1;
ll ceil(const ll &a, const ll &b) { return ((a) + (b)-1) / b; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> T Mypow(T a, int n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0) {
    T res = Mypow(a, n / 2);
    return res * res;
  }
  return a * Mypow(a, n - 1);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll x;
  cin >> x;

  for (ll a = 1;; a++) {
    ll A = Mypow(a, 5);
    ll l = -x;
    ll r = a;
    while (l < r) {
      ll b = (l + r) / 2;
      ll tmp = Mypow(b, 5) + x;
      if (tmp == A) {
        cout << a << ' ' << b << endl;
        return 0;
      }
      if (tmp < A)
        l = b;
      else
        r = b;
    }
  }
}