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

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

using ll = long long;
using P = pair<int, int>;
#define reps(i, n) for (int i = 1; i <= n; i++)
#define zarray(x, n)                                                           \\
  x[n + 1];                                                                    \\
  reps(i, n) { x[i] = 0; }
#define zarray2(x, n, m)                                                       \\
  x[n + 1][m + 1];                                                             \\
  reps(i, n) {                                                                 \\
    reps(j, m) { x[i][j] = 0; }                                                \\
  }

const ll l10_9 = 10L * 10L * 10L * 10L * 10L * 10L * 10L * 10L * 10L;
// const ll l10_9 = 10L;

bool over(ll a, ll b) {
  ll a5 = a * a * a * a * a;
  ll b5 = b * b * b * b * b;
  // cerr << a << "," << b << " - " << a5 - b5 << "," << a5 + b5 << "," << -a5 +
  // b5 << "," <<  -a5 - b5 << " > " << l10_9 << endl;
  return (abs(a5 - b5) > l10_9 && abs(a5 + b5) > l10_9);
}

int main() {
  int x = 0;
  cin >> x;
  // cerr << x << endl;

  ll a = 0;
  ll b = 0;
  while (!over(a, a - 1)) {
    ll a5 = a * a * a * a * a;
    while (!over(b, b - 1)) {
      ll b5 = b * b * b * b * b;
      // cerr << a << "," << b << " - " << a5 - b5 << "," << a5 + b5 << "=" << x
      // << endl;
      if (a5 - b5 == x) {
        goto exit;
      }
      if (a5 + b5 == x) {
        b = -b;
        goto exit;
      }
      if (-a5 - b5 == x) {
        a = -a;
        goto exit;
      }
      if (-a5 + b5 == x) {
        a = -a;
        b = -b;
        goto exit;
      }
      b++;
    }
    b = 0;
    a++;
  }
exit:
  cout << a << " " << b << endl;
}


Error: TLE

Fixed submission:

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

using ll = long long;
using P = pair<int, int>;
#define reps(i, n) for (int i = 1; i <= n; i++)
#define zarray(x, n)                                                           \\
  x[n + 1];                                                                    \\
  reps(i, n) { x[i] = 0; }
#define zarray2(x, n, m)                                                       \\
  x[n + 1][m + 1];                                                             \\
  reps(i, n) {                                                                 \\
    reps(j, m) { x[i][j] = 0; }                                                \\
  }

const ll l10_9 = 10L * 10L * 10L * 10L * 10L * 10L * 10L * 10L * 10L;
// const ll l10_9 = 10L;

bool over(ll a, ll b) {
  ll a5 = a * a * a * a * a;
  ll b5 = b * b * b * b * b;
  return (a5 - b5 > l10_9 || a5 + b5 > l10_9 || -a5 + b5 > l10_9 ||
          -a5 - b5 > l10_9);
}

int main() {
  int x = 0;
  cin >> x;
  // cerr << x << endl;

  ll a = 0;
  ll b = 0;
  while (true) {
    ll a5 = a * a * a * a * a;
    while (!over(a, b)) {
      ll b5 = b * b * b * b * b;
      // cerr << a5 << "," << b5 << "=" << x << endl;
      if (a5 - b5 == x) {
        goto exit;
      }
      if (a5 + b5 == x) {
        b = -b;
        goto exit;
      }
      if (-a5 + b5 == x) {
        a = -a;
        goto exit;
      }
      if (-a5 - b5 == x) {
        a = -a;
        b = -b;
        goto exit;
      }
      b++;
    }
    b = 0;
    a++;
  }
exit:
  cout << a << " " << b << endl;
}