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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(var, begin, end) for (int var = (begin); var <= (end); var++)
#define RFOR(var, begin, end) for (int var = (begin); var >= (end); var--)
#define REP(var, length) FOR(var, 0, length - 1)
#define RREP(var, length) RFOR(var, length - 1, 0)
#define EACH(value, var) for (auto value : var)
#define SORT(var) sort(var.begin(), var.end())
#define REVERSE(var) reverse(var.begin(), var.end())
#define RSORT(var)                                                             \\
  SORT(var);                                                                   \\
  REVERSE(var)
#define OPTIMIZE_STDIO                                                         \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.precision(10);                                                          \\
  cout << fixed
#define endl '\\n'

const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
struct mint {
  ll x;
  mint() : x(0) {}
  mint(ll x) : x((x % MOD + MOD) % MOD) {}
  mint &operator+=(const mint &r) {
    if ((x += r.x) >= MOD) {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &r) {
    if ((x -= r.x) < 0) {
      x += MOD;
    }
    return *this;
  }
  mint &operator*=(const mint &r) {
    if ((x *= r.x) > MOD) {
      x %= MOD;
    }
    return *this;
  }
  mint &operator++() {
    if ((++x) >= MOD) {
      x -= MOD;
    }
    return *this;
  }
  mint operator++(int) {
    mint ret = x;
    if ((++x) >= MOD) {
      x -= MOD;
    }
    return ret;
  }
  mint &operator--() {
    if ((--x) < 0) {
      x += MOD;
    }
    return *this;
  }
  mint operator--(int) {
    mint ret = x;
    if ((--x) < 0) {
      x += MOD;
    }
    return ret;
  }
  mint operator+(const mint &r) {
    mint ret;
    return ret = x + r.x;
  }
  mint operator-(const mint &r) {
    mint ret;
    return ret = x - r.x;
  }
  mint operator*(const mint &r) {
    mint ret;
    return ret = x * r.x;
  }
  mint operator-() { return mint() - *this; }
  bool operator<(const mint &a) { return x < a.x; }
  bool operator>(const mint &a) { return x > a.x; }
  bool operator<=(const mint &a) { return x <= a.x; }
  bool operator>=(const mint &a) { return x >= a.x; }
  bool operator==(const mint &a) { return x == a.x; }
  bool operator!=(const mint &a) { return x != a.x; }
};
ostream &operator<<(ostream &os, const mint &r) {
  os << r.x;
  return os;
}
ostream &operator>>(ostream &os, const mint &r) {
  os >> r.x;
  return os;
}

void solve(istream &cin, ostream &cout) {
  int x;
  cin >> x;
  int a, b;
  if (x == 0) {
    cout << 0 << " " << 0 << endl;
    return;
  }
  FOR(i, -118, 119) {
    FOR(j, -119, 118) {
      int now = pow(i, 5) - pow(j, 5);
      if (now == x) {
        a = i;
        b = j;
        break;
      }
    }
  }
  cout << a << " " << b << endl;
}

#ifndef TEST
int main() {
  OPTIMIZE_STDIO;
  solve(cin, cout);
  return 0;
}
#endif


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(var, begin, end) for (int var = (begin); var <= (end); var++)
#define RFOR(var, begin, end) for (int var = (begin); var >= (end); var--)
#define REP(var, length) FOR(var, 0, length - 1)
#define RREP(var, length) RFOR(var, length - 1, 0)
#define EACH(value, var) for (auto value : var)
#define SORT(var) sort(var.begin(), var.end())
#define REVERSE(var) reverse(var.begin(), var.end())
#define RSORT(var)                                                             \\
  SORT(var);                                                                   \\
  REVERSE(var)
#define OPTIMIZE_STDIO                                                         \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.precision(10);                                                          \\
  cout << fixed
#define endl '\\n'

const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
struct mint {
  ll x;
  mint() : x(0) {}
  mint(ll x) : x((x % MOD + MOD) % MOD) {}
  mint &operator+=(const mint &r) {
    if ((x += r.x) >= MOD) {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &r) {
    if ((x -= r.x) < 0) {
      x += MOD;
    }
    return *this;
  }
  mint &operator*=(const mint &r) {
    if ((x *= r.x) > MOD) {
      x %= MOD;
    }
    return *this;
  }
  mint &operator++() {
    if ((++x) >= MOD) {
      x -= MOD;
    }
    return *this;
  }
  mint operator++(int) {
    mint ret = x;
    if ((++x) >= MOD) {
      x -= MOD;
    }
    return ret;
  }
  mint &operator--() {
    if ((--x) < 0) {
      x += MOD;
    }
    return *this;
  }
  mint operator--(int) {
    mint ret = x;
    if ((--x) < 0) {
      x += MOD;
    }
    return ret;
  }
  mint operator+(const mint &r) {
    mint ret;
    return ret = x + r.x;
  }
  mint operator-(const mint &r) {
    mint ret;
    return ret = x - r.x;
  }
  mint operator*(const mint &r) {
    mint ret;
    return ret = x * r.x;
  }
  mint operator-() { return mint() - *this; }
  bool operator<(const mint &a) { return x < a.x; }
  bool operator>(const mint &a) { return x > a.x; }
  bool operator<=(const mint &a) { return x <= a.x; }
  bool operator>=(const mint &a) { return x >= a.x; }
  bool operator==(const mint &a) { return x == a.x; }
  bool operator!=(const mint &a) { return x != a.x; }
};
ostream &operator<<(ostream &os, const mint &r) {
  os << r.x;
  return os;
}
ostream &operator>>(ostream &os, const mint &r) {
  os >> r.x;
  return os;
}

int x;
int a, b;
int diff;

void dfs(int s) {
  int now = pow(a, 5) - pow(b, 5);
  diff = now - x;
  if (diff == 0)
    return;
  if (diff > s) {
    a *= 2;
    dfs(diff);
  } else {
    b--;
    dfs(diff);
  }
}

void solve(istream &cin, ostream &cout) {
  cin >> x;
  a = 0;
  b = 0;
  int y;
  if (x != 0) {
    y = sqrt(x);
    y = sqrt(y);
    y = sqrt(y);
  }
  if (x > 0) {
    // a > b
    a = y;
    int s = pow(a, 5);
    dfs(s);
  } else if (x < 0) {
    // b > a
    b = y;
    int s = 0 - pow(b, 5);
    dfs(s);
  }
  cout << a << " " << b << endl;
}

#ifndef TEST
int main() {
  OPTIMIZE_STDIO;
  solve(cin, cout);
  return 0;
}
#endif