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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

#define pb push_back
#define mk make_pair
#define f0(i, b) for (int i = 0; i < (b); i++)
#define f1(i, b) for (int i = 1; i <= (b); i++)
#define f2(i, a, b) for (int i = (a); i <= (b); i++)
#define sf(a) scanf("%d", &a);
#define sfl(a) scanf("%lld", &a);
#define pf(a) printf("%d\\n", a);
#define pfl(a) printf("%lld\\n", a);
#define TEST                                                                   \\
  int test;                                                                    \\
  cin >> test;                                                                 \\
  while (test--)
#define ii pair<int, int>
#define iil pair<ll, ll>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define INF 2147483647

int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
const double pi = acos(-1.0);

ll power(ll x, unsigned long long y) {
  ll res = 1; // Initialize result

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x);

    // y must be even now
    y = y >> 1;  // y = y/2
    x = (x * x); // Change x to x^2
  }
  return res;
}

void solve() {
  ll n;
  cin >> n;
  for (ll i = -1000; i <= 1000; i++) {
    for (ll j = -1000; j <= 1000; j++) {
      if (power(i, 5) - power(j, 5) == n) {
        cout << i << " " << j << endl;
        return;
      }
      //            else if(power(i,5) - power(-j,5) == n)
      //            {
      //                cout << i << " -" << j << endl;
      //                return;
      //            }
      //            else if(power(-i,5) - power(j,5) == n)
      //            {
      //                cout << "-" << i << " " << j << endl;
      //                return;
      //            }
      //            else if(power(-i,5) - power(-j,5) == n)
      //            {
      //                cout << "-" << i << " -" << j << endl;
      //                return;
      //            }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // TEST
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

#define pb push_back
#define mk make_pair
#define f0(i, b) for (int i = 0; i < (b); i++)
#define f1(i, b) for (int i = 1; i <= (b); i++)
#define f2(i, a, b) for (int i = (a); i <= (b); i++)
#define sf(a) scanf("%d", &a);
#define sfl(a) scanf("%lld", &a);
#define pf(a) printf("%d\\n", a);
#define pfl(a) printf("%lld\\n", a);
#define TEST                                                                   \\
  int test;                                                                    \\
  cin >> test;                                                                 \\
  while (test--)
#define ii pair<int, int>
#define iil pair<ll, ll>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define INF 2147483647

int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
const double pi = acos(-1.0);

ll power(ll x, unsigned long long y) {
  ll res = 1; // Initialize result

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x);

    // y must be even now
    y = y >> 1;  // y = y/2
    x = (x * x); // Change x to x^2
  }
  return res;
}

void solve() {
  ll n;
  cin >> n;
  for (ll i = 0; i <= 110; i++) {
    for (ll j = -110; j <= 110; j++) {
      if (power(i, 5) - power(j, 5) == n) {
        cout << i << " " << j << endl;
        return;
      }
      //            else if(power(i,5) - power(-j,5) == n)
      //            {
      //                cout << i << " -" << j << endl;
      //                return;
      //            }
      //            else if(power(-i,5) - power(j,5) == n)
      //            {
      //                cout << "-" << i << " " << j << endl;
      //                return;
      //            }
      //            else if(power(-i,5) - power(-j,5) == n)
      //            {
      //                cout << "-" << i << " -" << j << endl;
      //                return;
      //            }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // TEST
  solve();
  main();
  return 0;
}