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

/* Manish Pant - insider_pants, hello_my_friend */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define F first
#define S second
#define endl "\\n"
#define pb push_back

typedef long long ll;
typedef long double ldob;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T i : v)
    os << i << " ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (T i : v)
    os << i << " ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const multiset<T> &v) {
  for (T i : v)
    os << i << " ";
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.F << ' ' << v.S;
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.F << " => " << i.S << ')' << ' ';
  return os;
}

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}

ll x;

ll f(ll a, ll b) {
  ll ans = (a * a * a * a) + (a * a * a * b) + (a * a * b * b) +
           (a * b * b * b) + (b * b * b * b);
  return ans;
}

void solve() {
  set<ll> factors;
  for (ll i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      factors.insert(i);
      factors.insert(x / i);
    }
  }

  ll sq = sqrt(x) + 5;
  for (auto &i : factors) {
    if (i > sq)
      break;
    ll prev = ll(1e16);
    for (ll j = 0;; j++) {
      ll temp = f(j, j - i);
      if (temp * i > prev && temp * i > x)
        break;

      if (temp * i == x) {
        cout << j << ' ' << j - i << endl;
        return;
      }
      prev = temp * i;
    }
  }
}

int main() {
  fast();
  cin >> x;
  solve();
}

Error: TLE

Fixed submission:

/* Manish Pant - insider_pants, hello_my_friend */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define F first
#define S second
#define endl "\\n"
#define pb push_back

typedef long long ll;
typedef long double ldob;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T i : v)
    os << i << " ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (T i : v)
    os << i << " ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const multiset<T> &v) {
  for (T i : v)
    os << i << " ";
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.F << ' ' << v.S;
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.F << " => " << i.S << ')' << ' ';
  return os;
}

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}

ll x;

ll f(ll a, ll b) {
  ll ans = (a * a * a * a) + (a * a * a * b) + (a * a * b * b) +
           (a * b * b * b) + (b * b * b * b);
  return ans;
}

void solve() {
  set<ll> factors;
  for (ll i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      factors.insert(i);
      factors.insert(x / i);
    }
  }

  for (auto &i : factors) {
    ll base = f(0, -i);
    if (i * base == x) {
      cout << 0 << ' ' << -i << endl;
      return;
    }

    for (ll j = 1; j < x; j++) {
      ll temp = f(j, j - i);

      if (temp * i == x) {
        cout << j << ' ' << j - i << endl;
        return;
      }
    }
  }
}

int main() {
  fast();
  cin >> x;
  solve();
}