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


/*
██████╗  ███████╗
██╔══██╗ ██╔════╝
██████╔╝ ███████╗
██╔══██╗ ╚════██║
██║  ██║ ███████║
╚═╝  ╚═╝ ╚══════╝
*/

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

#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define haha(j, a, n) for (int j = a; j < n; j++)
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define endl "\\n"
#define bc(v) v.begin(), v.end()
#define vl vector<ll>
#define vi vector<int>
#define mod 1000000007
#define NUM 100005
#define F first
#define S second
#define bhar(a) rep(i, a.size()) cin >> a[i];
#define show(a) rep(i, a.size()) cout << a[i] << " ";
#define sott(a) sort(bc(a))
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define mapp map<ll, ll>
#define dkac cout << fixed << setprecision(15);
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define yeah(n) while (n--)
ll gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll bitfun(ll x) { return (x & (-x)); }
void solve() {
  ll x;
  cin >> x;
  for (ll i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      ll cnt = 0;
      while (cnt <= 1000) {
        if (pow(cnt, 5) - pow(cnt - i, 5) == x) {
          cout << cnt << " " << cnt - i;
          return;
        }
        cnt++;
      }
    }
  }
}
int main() {
  ios int t = 1;
  // cin>>t;
  yeah(t) { solve(); }
}

Error: TLE

Fixed submission:


/*
██████╗  ███████╗
██╔══██╗ ██╔════╝
██████╔╝ ███████╗
██╔══██╗ ╚════██║
██║  ██║ ███████║
╚═╝  ╚═╝ ╚══════╝
*/

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

#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define haha(j, a, n) for (int j = a; j < n; j++)
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define endl "\\n"
#define bc(v) v.begin(), v.end()
#define vl vector<ll>
#define vi vector<int>
#define mod 1000000007
#define NUM 100005
#define F first
#define S second
#define bhar(a) rep(i, a.size()) cin >> a[i];
#define show(a) rep(i, a.size()) cout << a[i] << " ";
#define sott(a) sort(bc(a))
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define mapp map<ll, ll>
#define dkac cout << fixed << setprecision(15);
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define yeah(n) while (n--)
ll gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll bitfun(ll x) { return (x & (-x)); }
void solve() {
  ll x;
  cin >> x;
  vl fac;
  for (ll i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      fac.pb(i);
      if (x / i != i)
        fac.pb(x / i);
    }
  }
  sott(fac);
  for (auto i : fac) {
    ll a = 1, b = -i + 1;
    ll sm = pow(a, 5) - pow(b, 5);
    while (sm <= x) {
      if (sm == x) {
        cout << a << " " << b;
        return;
      }
      a++;
      b++;
      sm = pow(a, 5) - pow(b, 5);
    }
  }
}
int main() {
  ios int t = 1;
  // cin>>t;
  yeah(t) { solve(); }
}