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


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

typedef long long int ll;
#define ent cout << "\\n"
#define pb push_back
#define mp make_pair
#define sz(x) x.size()
#define pair <ll, ll> pp
#define f(a, b) for (auto i = a; i < b; i++)
#define rev_f(a, b) for (auto i = b; i >= a; i--)
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

#define st(x) sort(x.begin(), x.end())
#define rv(x) reverse(x.begin(), x.end())
#define cintc()                                                                \\
  ll tc;                                                                       \\
  while (tc--)
#define deb(x) cout << x << "\\n";
#define cin(a)                                                                 \\
  ll a;                                                                        \\
  cin >> a;
#define cin2()                                                                 \\
  ll a, b;                                                                     \\
  cin >> a >> b
#define cin3()                                                                 \\
  ll a, b, c;                                                                  \\
  cin >> a >> b >> c
#define cinn(vect, n)                                                          \\
  def_v vect(n);                                                               \\
  f(0, n) cin >> vect[i]
#define coutn(vect, n)                                                         \\
  f(0, n) { cout << vect[i] << ""; }                                           \\
  cout << " ";

typedef vector<ll> def_v;
typedef tuple<ll, ll, ll> def_tuple;
ll mod = 1e9 + 7, cnt = 0;

int main() {
  ll n;
  cin >> n;

  ll i = 0, j = 0, save;
  while (1) {
    ll x = ll(pow(i, 5)), y = ll(pow(j, 5));

    // cout << x << ' ' << y << '\\n';
    while (x - y < n) {
      j--;
      y = ll(pow(j, 5));
    }

    if (x - y == n) {
      cout << i << " " << j << "\\n";
      return 0;
    } else {
      j = -1;
      x = ll(pow(i, 5)), y = ll(pow(j, 5));
      while (x - y < n) {
        j--;
        y = ll(pow(j, 5));
      }

      if (x - y == n) {
        cout << i << " " << j << "\\n";
        return 0;
      } else {
        i = i + 1;
        j = i;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:


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

typedef long long int ll;
#define ent cout << "\\n"
#define pb push_back
#define mp make_pair
#define sz(x) x.size()
#define pair <ll, ll> pp
#define f(a, b) for (auto i = a; i < b; i++)
#define rev_f(a, b) for (auto i = b; i >= a; i--)
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

#define st(x) sort(x.begin(), x.end())
#define rv(x) reverse(x.begin(), x.end())
#define cintc()                                                                \\
  ll tc;                                                                       \\
  while (tc--)
#define deb(x) cout << x << "\\n";
#define cin(a)                                                                 \\
  ll a;                                                                        \\
  cin >> a;
#define cin2()                                                                 \\
  ll a, b;                                                                     \\
  cin >> a >> b
#define cin3()                                                                 \\
  ll a, b, c;                                                                  \\
  cin >> a >> b >> c
#define cinn(vect, n)                                                          \\
  def_v vect(n);                                                               \\
  f(0, n) cin >> vect[i]
#define coutn(vect, n)                                                         \\
  f(0, n) { cout << vect[i] << ""; }                                           \\
  cout << " ";

typedef vector<ll> def_v;
typedef tuple<ll, ll, ll> def_tuple;
ll mod = 1e9 + 7, cnt = 0;

int main() {
  ll n;
  cin >> n;

  ll i = 0, j = 0, save;
  while (1) {
    ll x = ll(pow(j, 5)), y = ll(pow(i, 5));

    // cout << x << ' ' << y << '\\n';
    save = i;
    while (y - x < n) {
      i++;
      y = ll(pow(i, 5));
    }

    if (y - x == n) {
      cout << i << " " << j << "\\n";
      return 0;
    } else {
      j = -1;
      x = ll(pow(save, 5)), y = ll(pow(j, 5));
      while (x - y < n) {
        j--;
        y = ll(pow(j, 5));
        // cout << x << " " << y << "\\n";
      }

      if (x - y == n) {
        cout << i << " " << j << "\\n";
        return 0;
      } else {

        i = save + 1;
      }
    }
  }

  return 0;
}