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

// agrawal117
// chahatagrawal117
#include <bits/stdc++.h>
#define endl '\\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;
#define MAX 100005
void solve() {
  vector<ll> dp;
  ll k = -130;
  while (k < 130) {
    dp.push_back(k * k * k * k * k);
    k++;
  }
  ll t;
  cin >> t;
  bool flag = false;
  ll ans1 = 0;
  ll ans2 = 0;
  int cnt = 0;
  for (int i = 0; i <= dp.size(); ++i) {
    cnt++;
    for (int l = 0; l <= dp.size(); l++) {
      cnt++;
      ll diff = dp[i] - dp[l];
      if (diff == t) {
        ans1 = i - 130;
        ans2 = l - 130;
        flag = true;
        break;
      }
    }
    if (flag)
      break;
  }
  if (cnt < -1)
    return;
  cout << ans1 << " " << ans2 << endl;
}
int main() { solve(); }

Error: TLE

Fixed submission:

// nlather (Nishant Raj Lather)
// Chrollo_Lucifer

/* ------------------------------ TEMPLATE BEGIN ------------------------------
 */

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

/* ----------------- ORDERED SET (POLICY BASED DS) ----------------- */
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

/* -------------- BETTER RANDOM (RAND() IS DANGEROUS) -------------- */
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rnd(x, y) uniform_int_distribution<ll>(x, y)(rng)

/* ----------------- CUSTOM HASH FUNCTION FOR UMAP ----------------- */
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

/* ----------------------- DEBUGGING UTILITY ----------------------- */
#define cerr cout
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  cerr << t;
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)

#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
#define rep(i, x, n) for (ll i = (ll)(x); i < (ll)(n); i++)
#define rrep(i, x, n) for (ll i = (ll)(x); i >= (ll)(n); i--)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define bs binary_search
#define minSelf(a, b) a = min(a, b)
#define maxSelf(a, b) a = max(a, b)
#define mem(arr, x) memset(arr, x, sizeof(arr))
using ll = long long int;
using ld = long double;
template <typename T, typename V> using P = pair<T, V>;
template <typename T> using V = vector<T>;
ll MOD = 998244353LL;
const ll INF = 0x3f3f3f3f; // ~1e9 (1061109567)
const ld PI = 3.1415926535897932384626;
const ld EPS = 1e-9;
#define endl '\\n' // REMOVE FOR INTERACTIVE TASKS
const ll MAX = 1e5 + 5;

/* ------------------------------- TEMPLATE END -------------------------------
 */

ll power(ll a, ll b) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans *= a;
    a *= a, b >>= 1;
  }
  return ans;
}

ll nthRoot(ll A, ll N) {
  ld xPre = rand() % 10;
  ld eps = 1e-4;
  ld delX = INF * INF;
  ld xK;
  while (delX > eps) {
    xK = ((N - 1.0) * xPre + (ld)A / (ld)power(xPre, N - 1)) / (ld)N;
    delX = abs(xK - xPre);
    xPre = xK;
  }
  return xK;
}

int main() {
  boost ll x;
  cin >> x;
  rep(i, 0, 100) {
    ll a = power(i, 5);
    ll b = llabs(a - x), parity = (a < x ? -1 : 1);
    if (power(nthRoot(b, 5), 5) == b)
      return cout << i << ' ' << parity * nthRoot(b, 5) << endl, 0;
  }
}