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

#include <bits/stdc++.h>

#define _USE_MATH_DEFINES
#define reb(i, b, n) for (int i = (int)(b); i < (int)(n); i++)
#define rep(i, n) reb(i, 0, n)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define reach(it, v)                                                           \\
  for (__typeof((v).rbegin()) it = (v).rbegin(); it != (v).rend(); it++)
#define vsort(v)           sort((x).begin(), (x).end()
#define rvsort(v)          sort((x).rbegin(), (x).rend()
#define mp(a, b) make_pair(a, b)
#define fi first
#define se second
#define pb push_back
#define dbg(x) cerr << __LINE__ << ": " << #x << " = [" << (x) << "]" << endl;
#define dbgArr(table, n)                                                       \\
  rep(i, n) { cout << table[i] << ' '; }                                       \\
  cout << '\\n';
#define dbg2Arr(table, W, H)                                                   \\
  rep(y, H) {                                                                  \\
    rep(x, W) cout << table[y][x] << ' ';                                      \\
    cout << '\\n';                                                              \\
  }

using namespace std;
using std::stringstream;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vP;
typedef vector<char> vc;
typedef vector<string> vs;

static const int INF = 0x3f3f3f3f;
static const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
static const double EPS = 1e-9;
static const ll MOD = 1000000007LL;

static const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main(int argc, char *argv[]) {
  ll X;
  cin >> X;

  for (int i = -120; i < 120; ++i) {
    for (int j = -120; j < 120; j++) {
      if (i * i * i * i * i - j * j * j * j * j == X) {
        cout << i << ' ' << j << endl;
        return 0;
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define _USE_MATH_DEFINES
#define reb(i, b, n) for (int i = (int)(b); i < (int)(n); i++)
#define rep(i, n) reb(i, 0, n)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define reach(it, v)                                                           \\
  for (__typeof((v).rbegin()) it = (v).rbegin(); it != (v).rend(); it++)
#define vsort(v)           sort((x).begin(), (x).end()
#define rvsort(v)          sort((x).rbegin(), (x).rend()
#define mp(a, b) make_pair(a, b)
#define fi first
#define se second
#define pb push_back
#define dbg(x) cerr << __LINE__ << ": " << #x << " = [" << (x) << "]" << endl;
#define dbgArr(table, n)                                                       \\
  rep(i, n) { cout << table[i] << ' '; }                                       \\
  cout << '\\n';
#define dbg2Arr(table, W, H)                                                   \\
  rep(y, H) {                                                                  \\
    rep(x, W) cout << table[y][x] << ' ';                                      \\
    cout << '\\n';                                                              \\
  }

using namespace std;
using std::stringstream;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vP;
typedef vector<char> vc;
typedef vector<string> vs;

static const int INF = 0x3f3f3f3f;
static const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
static const double EPS = 1e-9;
static const ll MOD = 1000000007LL;

static const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

vi v;

void fact(ll n) {
  if (n == 1) {
    v.pb(1);
    return;
  }
  while (n % 2 == 0) {
    n /= 2;
    v.pb(2);
    return;
  }
  for (int i = 3; i <= n; i++) {
    while (n % i == 0) {
      n /= i;
      v.pb(i);
      return;
    }
  }
}

int main(int argc, char *argv[]) {
  ll X;
  cin >> X;
  fact(X);
  // rep(i, v.size()) {
  //     cout << v[i] << endl;
  // }
  ll x = v[0], y = X / v[0];

  ll a = 0, b = 0;
  while (true) {
    static bool odd = true;
    ll ai, bi;

    if (odd) {
      odd = false;

      a++;

      ai = a;
      bi = ai - x;
    } else {
      odd = true;
      b--;

      bi = b;
      ai = x + bi;
    }

    // cout << a << ' ' << b << endl;

    if ((a * a * a * a + a * a * a * b + a * a * b * b + a * b * b * b +
         b * b * b * b) == y) {
      break;
    }
  }

  cout << a << ' ' << b << endl;

  return 0;
}